PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/src/Developer_org.rst

https://gitlab.com/exaalt/lammps
ReStructuredText | 259 lines | 222 code | 37 blank | 0 comment | 0 complexity | b659de1e43fc6609c96661a5cff65273 MD5 | raw file
  1. Source files
  2. ------------
  3. The source files of the LAMMPS code are found in two directories of the
  4. distribution: ``src`` and ``lib``. Most of the code is written in C++
  5. but there are small a number of files in several other languages like C,
  6. Fortran, Shell script, or Python.
  7. The core of the code is located in the ``src`` folder and its
  8. sub-directories. A sizable number of these files are in the ``src``
  9. directory itself, but there are plenty of :doc:`packages <Packages>`,
  10. which can be included or excluded when LAMMPS is built. See the
  11. :doc:`Include packages in build <Build_package>` section of the manual
  12. for more information about that part of the build process. LAMMPS
  13. currently supports building with :doc:`conventional makefiles
  14. <Build_make>` and through :doc:`CMake <Build_cmake>`. Those procedures
  15. differ in how packages are enabled or disabled for inclusion into a
  16. LAMMPS binary so they cannot be mixed. The source files for each
  17. package are in all-uppercase sub-directories of the ``src`` folder, for
  18. example ``src/MOLECULE`` or ``src/EXTRA-MOLECULE``. The ``src/STUBS``
  19. sub-directory is not a package but contains a dummy MPI library, that is
  20. used when building a serial version of the code. The ``src/MAKE``
  21. directory and its sub-directories contain makefiles with settings and
  22. flags for a variety of configuration and machines for the build process
  23. with traditional makefiles.
  24. The ``lib`` directory contains the source code for several supporting
  25. libraries or files with configuration settings to use globally installed
  26. libraries, that are required by some of the optional packages. They may
  27. include python scripts that can transparently download additional source
  28. code on request. Each sub-directory, like ``lib/poems`` or ``lib/gpu``,
  29. contains the source files, some of which are in different languages such
  30. as Fortran or CUDA. These libraries included in the LAMMPS build,
  31. if the corresponding package is installed.
  32. LAMMPS C++ source files almost always come in pairs, such as
  33. ``src/run.cpp`` (implementation file) and ``src/run.h`` (header file).
  34. Each pair of files defines a C++ class, for example the
  35. :cpp:class:`LAMMPS_NS::Run` class which contains the code invoked by the
  36. :doc:`run <run>` command in a LAMMPS input script. As this example
  37. illustrates, source file and class names often have a one-to-one
  38. correspondence with a command used in a LAMMPS input script. Some
  39. source files and classes do not have a corresponding input script
  40. command, e.g. ``src/force.cpp`` and the :cpp:class:`LAMMPS_NS::Force`
  41. class. They are discussed in the next section.
  42. The names of all source files are in lower case and may use the
  43. underscore character '_' to separate words. Outside of bundled libraries
  44. which may have different conventions, all C and C++ header files have a
  45. ``.h`` extension, all C++ files have a ``.cpp`` extension, and C files a
  46. ``.c`` extension. A small number of C++ classes and utility functions
  47. are implemented with only a ``.h`` file. Examples are the Pointers and
  48. Commands classes or the MathVec functions.
  49. Class topology
  50. --------------
  51. Though LAMMPS has a lot of source files and classes, its class topology
  52. is not very deep, which can be seen from the :ref:`class-topology`
  53. figure. In that figure, each name refers to a class and has a pair of
  54. associated source files in the ``src`` folder, for example the class
  55. :cpp:class:`LAMMPS_NS::Memory` corresponds to the files ``memory.cpp``
  56. and ``memory.h``, or the class :cpp:class:`LAMMPS_NS::AtomVec`
  57. corresponds to the files ``atom_vec.cpp`` and ``atom_vec.h``. Full
  58. lines in the figure represent compositing: that is the class at the base
  59. of the arrow holds a pointer to an instance of the class at the tip.
  60. Dashed lines instead represent inheritance: the class to the tip of the
  61. arrow is derived from the class at the base. Classes with a red boundary
  62. are not instantiated directly, but they represent the base classes for
  63. "styles". Those "styles" make up the bulk of the LAMMPS code and only
  64. a few representative examples are included in the figure so it remains
  65. readable.
  66. .. _class-topology:
  67. .. figure:: JPG/lammps-classes.png
  68. LAMMPS class topology
  69. This figure shows some of the relations of the base classes of the
  70. LAMMPS simulation package. Full lines indicate that a class holds an
  71. instance of the class it is pointing to; dashed lines point to
  72. derived classes that are given as examples of what classes may be
  73. instantiated during a LAMMPS run based on the input commands and
  74. accessed through the API define by their respective base classes. At
  75. the core is the :cpp:class:`LAMMPS <LAMMPS_NS::LAMMPS>` class, which
  76. holds pointers to class instances with specific purposes. Those may
  77. hold instances of other classes, sometimes directly, or only
  78. temporarily, sometimes as derived classes or derived classes of
  79. derived classes, which may also hold instances of other classes.
  80. The :cpp:class:`LAMMPS_NS::LAMMPS` class is the topmost class and
  81. represents what is generally referred to an "instance" of LAMMPS. It is
  82. a composite holding pointers to instances of other core classes
  83. providing the core functionality of the MD engine in LAMMPS and through
  84. them abstractions of the required operations. The constructor of the
  85. LAMMPS class will instantiate those instances, process the command line
  86. flags, initialize MPI (if not already done) and set up file pointers for
  87. input and output. The destructor will shut everything down and free all
  88. associated memory. Thus code for the standalone LAMMPS executable in
  89. ``main.cpp`` simply initializes MPI, instantiates a single instance of
  90. LAMMPS while passing it the command line flags and input script. It
  91. deletes the LAMMPS instance after the method reading the input returns
  92. and shuts down the MPI environment before it exits the executable.
  93. The :cpp:class:`LAMMPS_NS::Pointers` is not shown in the
  94. :ref:`class-topology` figure for clarity. It holds references to many
  95. of the members of the `LAMMPS_NS::LAMMPS`, so that all classes derived
  96. from :cpp:class:`LAMMPS_NS::Pointers` have direct access to those
  97. reference. From the class topology all classes with blue boundary are
  98. referenced in the Pointers class and all classes in the second and third
  99. columns, that are not listed as derived classes are instead derived from
  100. :cpp:class:`LAMMPS_NS::Pointers`. To initialize the pointer references
  101. in Pointers, a pointer to the LAMMPS class instance needs to be passed
  102. to the constructor and thus all constructors for classes derived from it
  103. must do so and pass this pointer to the constructor for Pointers.
  104. Since all storage is supposed to be encapsulated (there are a few
  105. exceptions), the LAMMPS class can also be instantiated multiple times by
  106. a calling code. Outside of the aforementioned exceptions, those LAMMPS
  107. instances can be used alternately. As of the time of this writing
  108. (early 2021) LAMMPS is not yet sufficiently thread-safe for concurrent
  109. execution. When running in parallel with MPI, care has to be taken,
  110. that suitable copies of communicators are used to not create conflicts
  111. between different instances.
  112. The LAMMPS class currently (early 2021) holds instances of 19 classes
  113. representing the core functionality. There are a handful of virtual
  114. parent classes in LAMMPS that define what LAMMPS calls ``styles``. They
  115. are shaded red in the :ref:`class-topology` figure. Each of these are
  116. parents of a number of child classes that implement the interface
  117. defined by the parent class. There are two main categories of these
  118. ``styles``: some may only have one instance active at a time (e.g. atom,
  119. pair, bond, angle, dihedral, improper, kspace, comm) and there is a
  120. dedicated pointer variable for each of them in the composite class.
  121. Setups that require a mix of different such styles have to use a
  122. *hybrid* class that takes the place of the one allowed instance and then
  123. manages and forwards calls to the corresponding sub-styles for the
  124. designated subset of atoms or data. The composite class may also have
  125. lists of class instances, e.g. Modify handles lists of compute and fix
  126. styles, while Output handles a list of dump class instances.
  127. The exception to this scheme are the ``command`` style classes. These
  128. implement specific commands that can be invoked before, after, or in
  129. between runs. For these an instance of the class is created, its
  130. command() method called and then, after completion, the class instance
  131. deleted. Examples for this are the create_box, create_atoms, minimize,
  132. run, set, or velocity command styles.
  133. For all those ``styles`` certain naming conventions are employed: for
  134. the fix nve command the class is called FixNVE and the source files are
  135. ``fix_nve.h`` and ``fix_nve.cpp``. Similarly for fix ave/time we have
  136. FixAveTime and ``fix_ave_time.h`` and ``fix_ave_time.cpp``. Style names
  137. are lower case and without spaces or special characters. A suffix or
  138. words are appended with a forward slash '/' which denotes a variant of
  139. the corresponding class without the suffix. To connect the style name
  140. and the class name, LAMMPS uses macros like: ``AtomStyle()``,
  141. ``PairStyle()``, ``BondStyle()``, ``RegionStyle()``, and so on in the
  142. corresponding header file. During configuration or compilation files
  143. with the pattern ``style_<name>.h`` are created that consist of a list
  144. of include statements including all headers of all styles of a given
  145. type that are currently active (or "installed).
  146. More details on individual classes in the :ref:`class-topology` are as
  147. follows:
  148. - The Memory class handles allocation of all large vectors and arrays.
  149. - The Error class prints all (terminal) error and warning messages.
  150. - The Universe class sets up one or more partitions of processors so
  151. that one or multiple simulations can be run, on the processors
  152. allocated for a run, e.g. by the mpirun command.
  153. - The Input class reads and processes input input strings and files,
  154. stores variables, and invokes :doc:`commands <Commands_all>`.
  155. - Command style classes are derived from the Command class. They provide
  156. input script commands that perform one-time operations
  157. before/after/between simulations or which invoke a simulation. They
  158. are usually instantiated from within the Input class, its ``command``
  159. method invoked, and then immediately destructed.
  160. - The Finish class is instantiated to print statistics to the screen
  161. after a simulation is performed, by commands like run and minimize.
  162. - The Special class walks the bond topology of a molecular system to
  163. find first, second, third neighbors of each atom. It is invoked by
  164. several commands, like :doc:`read_data <read_data>`,
  165. :doc:`read_restart <read_restart>`, or :doc:`replicate <replicate>`.
  166. - The Atom class stores per-atom properties associated with atom styles.
  167. More precisely, they are allocated and managed by a class derived from
  168. the AtomVec class, and the Atom class simply stores pointers to them.
  169. The classes derived from AtomVec represent the different atom styles
  170. and they are instantiated through the :doc:`atom_style <atom_style>`
  171. command.
  172. - The Update class holds instances of an integrator and a minimizer
  173. class. The Integrate class is a parent style for the Verlet and
  174. r-RESPA time integrators, as defined by the :doc:`run_style
  175. <run_style>` command. The Min class is a parent style for various
  176. energy minimizers.
  177. - The Neighbor class builds and stores neighbor lists. The NeighList
  178. class stores a single list (for all atoms). A NeighRequest class
  179. instance is created by pair, fix, or compute styles when they need a
  180. particular kind of neighbor list and use the NeighRequest properties
  181. to select the neighbor list settings for the given request. There can
  182. be multiple instances of the NeighRequest class and the Neighbor class
  183. will try to optimize how they are computed by creating copies or
  184. sub-lists where possible.
  185. - The Comm class performs inter-processor communication, typically of
  186. ghost atom information. This usually involves MPI message exchanges
  187. with 6 neighboring processors in the 3d logical grid of processors
  188. mapped to the simulation box. There are two :doc:`communication styles
  189. <comm_style>` enabling different ways to do the domain decomposition.
  190. Sometimes the Irregular class is used, when atoms may migrate to
  191. arbitrary processors.
  192. - The Domain class stores the simulation box geometry, as well as
  193. geometric Regions and any user definition of a Lattice. The latter
  194. are defined by the :doc:`region <region>` and :doc:`lattice <lattice>`
  195. commands in an input script.
  196. - The Force class computes various forces between atoms. The Pair
  197. parent class is for non-bonded or pairwise forces, which in LAMMPS
  198. also includes many-body forces such as the Tersoff 3-body potential if
  199. those are computed by walking pairwise neighbor lists. The Bond,
  200. Angle, Dihedral, Improper parent classes are styles for bonded
  201. interactions within a static molecular topology. The KSpace parent
  202. class is for computing long-range Coulombic interactions. One of its
  203. child classes, PPPM, uses the FFT3D and Remap classes to redistribute
  204. and communicate grid-based information across the parallel processors.
  205. - The Modify class stores lists of class instances derived from the
  206. :doc:`Fix <fix>` and :doc:`Compute <compute>` base classes.
  207. - The Group class manipulates groups that atoms are assigned to via the
  208. :doc:`group <group>` command. It also has functions to compute
  209. various attributes of groups of atoms.
  210. - The Output class is used to generate 3 kinds of output from a LAMMPS
  211. simulation: thermodynamic information printed to the screen and log
  212. file, dump file snapshots, and restart files. These correspond to the
  213. :doc:`Thermo <thermo_style>`, :doc:`Dump <dump>`, and
  214. :doc:`WriteRestart <write_restart>` classes respectively. The Dump
  215. class is a base class with several derived classes implementing
  216. various dump style variants.
  217. - The Timer class logs timing information, output at the end
  218. of a run.
  219. .. TODO section on "Fixes, Computes, and Variables"
  220. .. how and when data is computed and provided and how it is
  221. .. referenced. flags in Fix/Compute/Variable classes tell
  222. .. style and amount of available data.