Merge branch 'maint'
[hoomd-blue.git] / doc / user / examples.dox
blobf0618e2e249c424840f62555b2ffe59932f59a59
1 /*
2 Highly Optimized Object-oriented Many-particle Dynamics -- Blue Edition
3 (HOOMD-blue) Open Source Software License Copyright 2009-2014 The Regents of
4 the University of Michigan All rights reserved.
6 HOOMD-blue may contain modifications ("Contributions") provided, and to which
7 copyright is held, by various Contributors who have granted The Regents of the
8 University of Michigan the right to modify and/or distribute such Contributions.
10 You may redistribute, use, and create derivate works of HOOMD-blue, in source
11 and binary forms, provided you abide by the following conditions:
13 * Redistributions of source code must retain the above copyright notice, this
14 list of conditions, and the following disclaimer both in the code and
15 prominently in any materials provided with the distribution.
17 * Redistributions in binary form must reproduce the above copyright notice, this
18 list of conditions, and the following disclaimer in the documentation and/or
19 other materials provided with the distribution.
21 * All publications and presentations based on HOOMD-blue, including any reports
22 or published results obtained, in whole or in part, with HOOMD-blue, will
23 acknowledge its use according to the terms posted at the time of submission on:
24 http://codeblue.umich.edu/hoomd-blue/citations.html
26 * Any electronic documents citing HOOMD-Blue will link to the HOOMD-Blue website:
27 http://codeblue.umich.edu/hoomd-blue/
29 * Apart from the above required attributions, neither the name of the copyright
30 holder nor the names of HOOMD-blue's contributors may be used to endorse or
31 promote products derived from this software without specific prior written
32 permission.
34 Disclaimer
36 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS'' AND
37 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND/OR ANY
39 WARRANTIES THAT THIS SOFTWARE IS FREE OF INFRINGEMENT ARE DISCLAIMED.
41 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
42 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
43 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 /*! \page page_example_scripts Example Scripts
51 \b Examples:
53 1. \ref dump_dcd "Writing DCD trajectory files" - demonstrates \link hoomd_script.dump.dcd dump.dcd\endlink<br>
54     <i>write a file which can be viewed in visualization
55     software such as VMD</i>\n
56     \n
57 2. \ref analyze_imd "Visualizing simulations in real time" - demonstrates \link hoomd_script.analyze.imd analyze.imd\endlink<br>
58     <i>use VMD to view a running simulation update in real time</i>\n
59     \n
60 3. \ref create_random_polymers "Initialize a system with randomly generated polymers" - demonstrates \link hoomd_script.init.create_random_polymers init.create_random_polymers\endlink<br>
61     <i>initialize a random system of polymers given configurable parameters</i>\n
62     \n
63 4. \ref init_xml "Initialize from an XML file" - demonstrates \link hoomd_script.init.read_xml init.read_xml\endlink<br>
64     <i>initialize a system with any configuration from a file</i>\n
65     \n
66 5. \ref box_resize "Shrinking/expanding the simulation box" - demonstrates \link hoomd_script.update.box_resize update.box_resize\endlink<br>
67     <i>slowly change the simulation box dimensions</i>\n
68     \n
69 6. \ref box_resize_rigid "Shrinking/expanding the simulation box with rigid bodies" - demonstrates \link hoomd_script.update.box_resize update.box_resize\endlink<br>
70     <i>behavior of update.box_resize with rigid bodies in the system</i>\n
71     \n
72 7. \ref init_reset "Run multiple simulations in one job script" - demonstrates \link hoomd_script.init.reset init.reset\endlink<br>
73     <i>use a python loop to run many simulations at different parameters in a single script</i>\n
74     \n
75 8. \ref init_create_empty "Set initial conditions directly from python code" - demonstrates \link hoomd_script.init.create_empty init.create_empty\endlink<br>
76     <i>use python code to create custom initial configurations without needing to write an xml file</i>\n
77     \n
78 9. \ref pair_table "Use any functional form for pair interactions" - demonstrates \link hoomd_script.pair.table pair.table\endlink<br>
79     <i>specify an abritrary pair interaction without needing to write C++ code</i>\n
80     \n
81 10. \ref pair_table_file "Use any tabulated data for pair interactions" - demonstrates \link hoomd_script.pair.table pair.table\endlink<br>
82     <i>specify an abritrary pair interaction, read from a file</i>\n
83 11. \ref bond_table "Use any tabulated data or functional form for bond interactions" - demonstrates \link hoomd_script.bond.table bond.table\endlink<br>
84     <i>specify an abritrary bond interaction, read from a file or an evaluated function</i>\n
86 12. \ref angle_table "Use any tabulated data or functional form for angle interactions" - demonstrates \link hoomd_script.angle.table angle.table\endlink<br>
87     <i>specify an abritrary angle interaction, read from a file or an evaluated function</i>\n
89 13. \ref dihedral_table "Use any tabulated data or functional form for dihedral interactions" - demonstrates \link hoomd_script.dihedral.table dihedral.table\endlink<br>
90     <i>specify an abritrary dihedral interaction, read from a file or an evaluated function</i>\n
93 /*! \example dump_dcd
94 This simple example is an adaptation of the quick start script. It performs a simulation of a Lennard-Jones liquid,
95 dumping snapshots of the system every 100 time steps.
97 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
98 \code
99 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
100 cd ~/hoomd-examples
101 \endcode
103 Then run:
104 \code
105 $ hoomd -x dump_dcd
106 \endcode
108 Running this quick simulation will result in two output files being generated
109 in the current working directory: \c dump_dcd.xml and \c dump_dcd.dcd. The
110 \c .xml file generated by \link hoomd_script.dump.xml dump.xml \endlink
111 contains the particle names and coordinates at time step 0. If there were
112 any bonds specified, they would be included too. VMD or other applications
113 can read in the \c .xml to obtain this information.
115 \c dump.dcd includes snapshots of the system state (particle position
116 coordinates only) written every 100 time steps. This file can be loaded into
117 visualization software such as VMD and played as a movie or read for
118 analysis purposes.
120 If you have VMD installed, you can load up the entire simulation trajectory
121 by running
122 \code
123 $ vmd -hoomd dump_dcd.xml dump_dcd.dcd
124 \endcode
125 on the command line or by loading these files using VMD's GUI. For the best
126 visualization, open VMD's <i>Graphical Representation</i> menu and set the
127 <i>Drawing Method</i> to VDW.
129 <hr>
133 /*! \example analyze_imd
134 Here is the same simulation as \ref dump_dcd, this time configured for real-time display in VMD using the IMD
135 interface.
137 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
138 \code
139 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
140 cd ~/hoomd-examples
141 \endcode
143 Then run:
144 \code
145 $ hoomd -x analyze_imd
146 \endcode
148 The simulation is now running in hoomd (you may get a prompt from your firewall asking if hoomd is allowed to act as
149 a server, allow it). In another terminal, cd into the examples directory and load the initial configuration in vmd:
150 \code
151 $ vmd -hoomd analyze_imd.xml
152 \endcode
154 Then go to the VMD command window and run the command
155 \code
156 imd connect localhost 54321
157 \endcode
158 The particles in the display window should begin moving. The display
159 is of the current state of the simulation, updated in \b real-time.
160 The best visualization is obtained by setting the <i>Drawing Method</i> to VDW in
161 VMD's <i>Graphical Representation</i> menu.
163 Switch back to the terminal where hoomd is running and press CTRL-C to kill the
164 simulation. It is set to run for an extremely long time on purpose to allow
165 ample time to launch VMD and issue the \c imd command.
167 <hr>
170 /*! \example create_random_polymers
171 Here is a script that generates a system of bead-spring polymers that self-assemble
172 into a hex phase when run for a few million time steps. The polymers are A6B7A6 block copolymers
173 in an implicit solvent. The script also shows a few examples of how writing python
174 code in the script can be handy: here the concentration \c phi_P is a parameter and
175 math operations are performed to calculate the length of the box.
177 For more information on the model in this script, see<br>
178 "Micellar crystals in solution from molecular dynamics simulations"<br>
179 J. Chem. Phys. \b 128, 184906 (2008); DOI:10.1063/1.2913522<br>
180 http://link.aip.org/link/?JCPSA6/128/184906/1
182 Any of the polymer systems in the paper could be easily run just by changing a few parameters in
183 this script.
185 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
186 \code
187 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
188 cd ~/hoomd-examples
189 \endcode
191 Then run:
192 \code
193 $ hoomd -x create_random_polymers
194 \endcode
196 The results of this example may be visualized with VMD:
197 \code
198 $ vmd -hoomd create_random_polymers.xml create_random_polymers.dcd
199 \endcode
201 <hr>
205 /*! \example init_xml
207 HOOMD-blue is not limited by its built-in random initial condition generators. You can load in an arbitrary initial
208 condition from a formatted xml file. Here is a simple example demonstrating most of the types of data that can be
209 input (see \ref page_xml_file_format for full documentation of this format):
211 \include init_xml.xml
213 The initial conditions can be read into a simulation
214 using the command \link hoomd_script.init.read_xml init.read_xml\endlink as shown in the example script below.
216 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
217 \code
218 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
219 cd ~/hoomd-examples
220 \endcode
222 Then run:
223 \code
224 $ hoomd -x init_xml
225 \endcode
227 The results of this example may be visualized with VMD:
228 \code
229 $ vmd -hoomd init_xml.xml init_xml.dcd
230 \endcode
232 <hr>
236 /*! \example box_resize
237 \link hoomd_script.update.box_resize update.box_resize\endlink can be used to slowly resize the simulation box
238 dimensions during a run(). To use it, specify a \link hoomd_script.variant variant\endlink mapping time steps to
239 box lengths and give it to the update.box_resize command. It can optionally rescale all particle coordinates into
240 the new box, or just leave all particle coordinates alone.
242 \note An update period of 1 will result in extremely slow simulations (box resizing is performed on the CPU, not
243 the GPU). Use a slow box resize and a larger update period (i.e. 100) for faster simulations.
245 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
246 \code
247 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
248 cd ~/hoomd-examples
249 \endcode
251 Then run:
252 \code
253 $ hoomd -x box_resize
254 \endcode
256 The results of this example may be visualized with VMD:
257 \code
258 $ vmd -hoomd box_resize.xml box_resize.dcd
259 \endcode
261 <hr>
264 /*! \example box_resize_rigid
265 \link hoomd_script.update.box_resize update.box_resize\endlink can be used to slowly resize the simulation box
266 dimensions during a run(). To use it, specify a \link hoomd_script.variant variant\endlink mapping time steps to
267 box lengths and give it to the update.box_resize command. It operates properly on systems of particles, rigid
268 bodies, and systems with both. Rigid bodies are handled by scaling (if scaling is enabled) the center of mass
269 position of each body while keeping the size of the body fixed.
271 \note An update period of 1 will result in extremely slow simulations (box resizing is performed on the CPU, not
272 the GPU). Use a slow box resize and a larger update period (i.e. 100) for faster simulations.
274 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
275 \code
276 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
277 cd ~/hoomd-examples
278 \endcode
280 Then run:
281 \code
282 $ hoomd -x box_resize_rigid
283 \endcode
285 The results of this example may be visualized with VMD:
286 \code
287 $ vmd -hoomd box_resize_rigid.xml box_resize_rigid.dcd
288 \endcode
290 <hr>
294 /*! \example init_reset
296 In a loop, this example performs a simulation of a Lennard-Jones liquid
297 at different system sizes one after the other. It shows how the power of python
298 code in a hoomd script can be utilized to accomplish complicated tasks.
299 For more information on python and a tutorial on the python language,
300 check out http://www.python.org.
302 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
303 \code
304 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
305 cd ~/hoomd-examples
306 \endcode
308 Then run:
309 \code
310 $ hoomd -x init_reset
311 \endcode
313 <hr>
317 /*! \example init_create_empty
319 This example performs a simulation of a Lennard-Jones liquid of two types of particles A and B. The initial condition
320 is a simple cubic lattice, with the top half of the box containing particles of type B and the bottom half type A.
322 It demonstrates several use-cases for accessing particle data from within python that could be easily copied
323 and used in other scripts.
324  - Initialization of all particle positions
325  - Initialization of a thermal velocity distribution
326  - Changing the type of a group of particles
328 For more information on python and a tutorial on the python language,
329 check out http://www.python.org . Details about the relevant commands in hoomd_script can be found here:
330  - \link hoomd_script.init.create_empty init.create_empty\endlink
331  - hoomd_script.data
333 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
334 \code
335 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
336 cd ~/hoomd-examples
337 \endcode
339 Then run:
340 \code
341 $ hoomd -x init_create_empty
342 \endcode
344 <hr>
348 /*! \example pair_table
350 This example demonstrates how to use \link hoomd_script.pair.table pair.table\endlink to specify an arbitrary
351 particle pair interaction, given a functional form for \f$ V(r) \f$ and \f$ F(r) = -\frac{\partial V}{\partial r}\f$.
353 First, write a python function which evaluates \f$ V(r) \f$ and \f$ F(r) \f$ in the following form
354 ~~~~~~~~~~~~~{.py}
355 def my_potential(r, rmin, rmax, coeff1, coeff2, coeff3):
356     V = # function of r, rmin, rmax, coeff1, ...
357     F = # function of r, rmin, rmax, coeff1, ... (-dV/dr)
358     return (V, F)
359 ~~~~~~~~~~~~~
361 Then, initialize the table potential with a chosen number of points:
362 ~~~~~~~~~~~~~{.py}
363 table = pair.table(width=1000)
364 ~~~~~~~~~~~~~
366 The function, parameters *rmin* and *rmax*, and coefficients can be set individually for each type pair:
367 ~~~~~~~~~~~~~{.py}
368 table.pair_coeff.set('A', 'A', func=my_potential, rmin=0.8, rmax=3.0, coeff=dict(coeff1=1.5, coeff2=1.0, coeff3=-4.5))
369 ~~~~~~~~~~~~~
370 Any number of coefficients of any name may be present in your function. The function is evaluated at *width* points in
371 in the range `[rmin,rmax]`. Outside of the range `[rmin,rmax]`, both the force and energy evaluate to zero.
372 In the range, the given *r* in a pair interaction is used to interpolate linearly between the two nearest points in
373 the table.
375 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
376 \code
377 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
378 cd ~/hoomd-examples
379 \endcode
381 Then run:
382 \code
383 $ hoomd -x pair_table
384 \endcode
386 <hr>
390 /*! \example pair_table_file
392 This example demonstrates how to use \link hoomd_script.pair.table pair.table\endlink to specify an arbitrary
393 particle pair interaction, where \f$ V(r) \f$ and \f$ F(r) = -\frac{\partial V}{\partial r}\f$ are provided in a file.
395 First, write a file that lists \f$ V(r) \f$ and \f$ F(r) \f$ in the following form:
396 \snippet pair_table_file.dat start
398 \snippet pair_table_file.dat end
400 The first column lists all the *r* values. The first *r* value in the file is taken to be *rmin*. The last
401 is *rmax*. All values in-between must be evenly spaced and monotonically increasing. The second column specifies
402 \f$ V(r) \f$ and the third specifies \f$ F(r) \f$.
404 Then, initialize the table potential with a chosen number of points. The number of points must match the number of
405 rows in the data file.
406 ~~~~~~~~~~~~~{.py}
407 table = pair.table(width=171)
408 ~~~~~~~~~~~~~
410 Use one individual file for each type pair. When using more than one file, all must have the same number of rows.
411 ~~~~~~~~~~~~~{.py}
412 table.set_from_file('A', 'A', filename='pair_table_file.dat')
413 ~~~~~~~~~~~~~
414 Outside of the range `[rmin,rmax]`, both the force and energy evaluate to zero. In the range, the given *r* in a pair
415 interaction is used to interpolate linearly between the two nearest points in the provided table.
417 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
418 \code
419 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
420 cd ~/hoomd-examples
421 \endcode
423 Then run:
424 \code
425 $ hoomd -x pair_table_file
426 \endcode
428 <hr>
432 /*! \example bond_table
434 This example demonstrates how to use \link hoomd_script.bond.table bond.table\endlink to specify an arbitrary
435 bond interaction, where \f$ V(r) \f$ and \f$ F(r) = -\frac{\partial V}{\partial r}\f$ are provided in a file or
436 via function evaluation.
438 To specify a potential in a file, first write a file that lists \f$ V(r) \f$ and \f$ F(r) \f$ in the following form:
439 \snippet bond_table.dat start
442 The first column lists all the *r* values. The first *r* value in the file is taken to be *rmin*. The last
443 is *rmax*. All values in-between must be evenly spaced and monotonically increasing. The second column specifies
444 \f$ V(r) \f$ and the third specifies \f$ F(r) \f$.
446 Then, initialize the table potential with a chosen number of points. The number of points must match the number of
447 rows in the data file.
448 ~~~~~~~~~~~~~{.py}
449 btable = bond.table(width=1000)
450 ~~~~~~~~~~~~~
452 Use one individual file for each bond type. When using more than one file, all must have the same number of rows.
453 ~~~~~~~~~~~~~{.py}
454 btable.set_from_file('bond1', 'bond_table.dat')
455 ~~~~~~~~~~~~~
456 Outside of the range `[rmin,rmax]`, both the force and energy evaluate to zero. In the range, the given *r* in a pair
457 interaction is used to interpolate linearly between the two nearest points in the provided table.
459 To specify a tabulated function, write a python function which evaluates \f$ V(r) \f$ and \f$ F(r) \f$ in the following form
460 ~~~~~~~~~~~~~{.py}
461 def my_potential(r, rmin, rmax, coeff1, coeff2, coeff3):
462     V = # function of r, rmin, rmax, coeff1, ...
463     F = # function of r, rmin, rmax, coeff1, ... (-dV/dr)
464     return (V, F)
465 ~~~~~~~~~~~~~
467 The function, parameters *rmin* and *rmax*, and coefficients can be set individually for each bond type:
468 ~~~~~~~~~~~~~{.py}
469 btable.bond_coeff.set('bond2', func=my_potential, rmin=0.2, rmax=5.0, coeff=dict(coeff1=1.5, coeff2=1.0, coeff3=-4.5))
470 ~~~~~~~~~~~~~
471 Any number of coefficients of any name may be present in your function. The function is evaluated at *width* points in
472 in the range `[rmin,rmax]`. Outside of the range `[rmin,rmax]`, both the force and energy evaluate to zero.
473 In the range, the given *r* in a pair interaction is used to interpolate linearly between the two nearest points in
474 the table.
476 To run this example, copy the examples directory from share/hoomd/examples your hoomd installation directory.
477 \code
478 cp -R HOOMD-INSTALL-DIR/share/hoomd/examples ~/hoomd-examples
479 cd ~/hoomd-examples
480 \endcode
482 Then run:
483 \code
484 $ hoomd -x bond_table
485 \endcode
487 <hr>
489 /*! \example angle_table */
491 /*! \example dihedral_table */