Bsoft  2.1.0
Bernard's software package
bmd.cpp
Go to the documentation of this file.
1 
9 #include "rwmd.h"
10 #include "mol_md.h"
11 #include "rwmolecule.h"
12 #include "mol_bonds.h"
13 #include "mol_util.h"
14 #include "Matrix.h"
15 #include "options.h"
16 #include "linked_list.h"
17 #include "utilities.h"
18 #include "timer.h"
19 
20 // Declaration of global variables
21 extern int verbose; // Level of output to the screen
22 
23 // Function prototypes
24 
25 
26 /* Usage assistance */
27 const char* use[] = {
28 " ",
29 "Usage: bmd [options] in.pdb out.pdb",
30 "-----------------------------------",
31 "Calculates forces on an atomic structure and adjust coordinates.",
32 " ",
33 "Actions:",
34 "-dynamics 4000 Number of iterations for molecular dynamics.",
35 " ",
36 "Parameters:",
37 "-verbose 7 Verbose output.",
38 //"-origin 0,0,0 Origin (angstrom).",
39 "-size 10,10,10 Simulation box size (angstrom).",
40 "-bonds intra Generate all or intramolecular bonds only.",
41 //"-length 1.6 Bond length (angstrom).",
42 "-wrap Wrap around (periodic boundaries).",
43 "-timestep 0.01 Integration time step (default 0.001).",
44 "-velocitylimit 0.01 Limit on the velocity (default 0.1 per time step).",
45 "-friction 0.2 Friction constant (default 1 = no friction).",
46 "-Kbond 150 Bond strength (default 1).",
47 "-Kangle 4 Angle strength (default 1).",
48 "-Kvdw 0.1 Van der Waals strength (default 0).",
49 "-Kelectrostatic 0.4 Electrostatic strength (default 0).",
50 "-cutoff 7.8 Distance cutoff for non-bonded forces (default 5 A).",
51 " ",
52 "Input:",
53 "-parameters md.star Molecular dynamics parameter file (default md_param.star).",
54 " ",
55 "Output:",
56 "-output param.star Output parameter file.",
57 " ",
58 NULL
59 };
60 
61 int main(int argc, char **argv)
62 {
63  /* Initialize variables */
64 // int calcrdf(0); // Flag to calculate RDF
65  int wrap(0); // No wrapping as default
66  Vector3<double> size; // Size for generating water
67  int bondtype(0); // Bond types to consider: 0=all, 1=intramolecular only
68  double bondlength(1); // Bond length
69  int max_iter(0); // Number of iterations/cycles for minimization
70  double timestep(0.001); // Integration time step
71  double velocitylimit(0.1); // Limit on velocity per time step
72  double Kfriction(1); // Friction constant, 1=no friction
73  double Kbond(1); // Bond strength
74  double Kangle(1); // Angle strength
75  double Kelec(0); // Electrostatic strength
76  double Kvdw(0); // Van der Waals strength
77  double cutoff(5); // Distance cutoff for non-bonded forces
78  Bstring atom_select("all");
79  Bstring paramfile("md_param.star"); // Default parameter file
80  Bstring paramout; // Output parameter file
81 
82  int optind;
83  Boption* option = get_option_list(use, argc, argv, optind);
84  Boption* curropt;
85  for ( curropt = option; curropt; curropt = curropt->next ) {
86  if ( curropt->tag == "size" ) {
87  size = curropt->size();
88  if ( size.volume() < 3 )
89  cerr << "-size: All three dimensions must be specified" << endl;
90  }
91  if ( curropt->tag == "wrap" ) wrap = 1;
92  if ( curropt->tag == "bonds" ) {
93  if ( curropt->value.contains("int") ) bondtype = 1;
94  if ( curropt->value.contains("val") ) bondtype = 2;
95  }
96  if ( curropt->tag == "length" )
97  if ( ( bondlength = curropt->value.real() ) < 1e-6 )
98  cerr << "-length: A bond length must be specified!" << endl;
99  if ( curropt->tag == "dynamics" )
100  if ( ( max_iter = curropt->value.integer() ) < 1 )
101  cerr << "-dynamics: The number of iterations must be specified!" << endl;
102  if ( curropt->tag == "timestep" )
103  if ( ( timestep = curropt->value.real() ) < 1e-6 )
104  cerr << "-timestep: The time step must be specified!" << endl;
105  if ( curropt->tag == "velocitylimit" )
106  if ( ( velocitylimit = curropt->value.real() ) < 1e-6 )
107  cerr << "-velocitylimit: The velocity limit must be specified!" << endl;
108  if ( curropt->tag == "friction" )
109  if ( ( Kfriction = curropt->value.real() ) < 1e-6 )
110  cerr << "-friction: The friction constant must be specified!" << endl;
111  if ( curropt->tag == "Kbond" )
112  if ( ( Kbond = curropt->value.real() ) < 1e-6 )
113  cerr << "-Kbond: The bond strength must be specified!" << endl;
114  if ( curropt->tag == "Kangle" )
115  if ( ( Kangle = curropt->value.real() ) < 1e-6 )
116  cerr << "-Kangle: The angle strength must be specified!" << endl;
117  if ( curropt->tag == "Kelectrostatic" )
118  if ( ( Kelec = curropt->value.real() ) < 1e-6 )
119  cerr << "-Kelectrostatic: The electrostatic strength must be specified!" << endl;
120  if ( curropt->tag == "Kvdw" )
121  if ( ( Kvdw = curropt->value.real() ) < 1e-6 )
122  cerr << "-Kvdw: The Van der Waals strength must be specified!" << endl;
123  if ( curropt->tag == "cutoff" )
124  if ( ( cutoff = curropt->value.real() ) < 1e-6 )
125  cerr << "-cutoff: The cutoff distance must be specified!" << endl;
126  if ( curropt->tag == "parameters" )
127  paramfile = curropt->filename();
128  if ( curropt->tag == "output" )
129  paramout = curropt->filename();
130  }
131  option_kill(option);
132 
133  double ti = timer_start();
134 
135  Bstring filename(argv[optind++]);
136  Bmolgroup* molgroup = read_molecule(filename, atom_select, paramfile);
137  if ( !molgroup ) {
138  cerr << "Error: No molecule to work with!" << endl;
139  bexit(-1);
140  }
141 
142  if ( size[0] > 0 && size[1] > 0 && size[2] > 0 )
143  molgroup->box = size;
144  else
145  molgroup->box = molgroup->max - molgroup->min;
146 
147  Bmd* md = read_md_parameters(paramfile);
148  if ( !md ) {
149  cerr << "Error: File " << paramfile << " not read!" << endl;
150  bexit(-1);
151  }
152 
153  md->timestep = timestep;
154  md->Kfriction = Kfriction;
155  md->Kbond = Kbond;
156  md->Kangle = Kangle;
157  md->Kelec = Kelec;
158  md->Kvdw = Kvdw;
159  md->cutoff = cutoff;
160  md->wrap = wrap;
161 
162  md_show_bonds(molgroup);
163 
164  Bbond* bondlist = NULL;
165  if ( bondtype == 1 )
166  bondlist = md_generate_molecular_bond_list(molgroup, md);
167  else if ( bondtype == 2 )
168  bondlist = md_generate_bond_list_with_valence(molgroup, md, 3);
169  else
170  bondlist = md_generate_bond_list(molgroup, md);
171 
172  md_bond_list_set_parameters(bondlist, md->bond);
173 
174  md_show_bond_stats(molgroup);
175 
176  md_generate_angle_list(molgroup, md);
177 
178  if ( verbose & VERB_PROCESS )
179  molgroup_density(molgroup);
180 
181  if ( verbose ) {
182  md_calculate_deviations(molgroup, md->wrap);
184  md_show_bond_types(molgroup, md->bond);
185  }
186 
187  if ( molgroup->mol && max_iter ) {
188  md_leapfrog(molgroup, md, max_iter, velocitylimit);
189  if ( verbose & VERB_PROCESS )
190  molgroup_density(molgroup);
191  if ( verbose ) {
192  md_calculate_deviations(molgroup, md->wrap);
194  md_show_bond_types(molgroup, md->bond);
195  }
196  }
197 
198  if ( paramout.length() )
199  write_md_parameters(paramout, md);
200 
201  if ( optind < argc ) {
202  molecule_update_comment(molgroup, argc, argv);
203  write_molecule(argv[optind], molgroup);
204  }
205 
206  molgroup_kill(molgroup);
207  md_kill(md);
208 
209  if ( verbose & VERB_TIME )
210  timer_report(ti);
211 
212  bexit(0);
213 }
214 
Boption
A class for a command line input option.
Definition: options.h:26
md_show_bond_types
int md_show_bond_types(Bmolgroup *molgroup, Bbondtype *bondtype)
Shows the bond type composition.
Definition: mol_bonds.cpp:580
molgroup_kill
int molgroup_kill(Bmolgroup *molgroup)
Destroys a molecule group.
Definition: rwmolecule.cpp:374
md_generate_bond_list_with_valence
Bbond * md_generate_bond_list_with_valence(Bmolgroup *molgroup, Bmd *md, int valence)
Generates a bond list based on a single valence and the shortest current bond lengths.
Definition: mol_bonds.cpp:208
molecule_update_comment
int molecule_update_comment(Bmolgroup *molgroup, int n, char **strings)
Puts a set of strings and time in the main comment of a molecule group.
Definition: rwmolecule.cpp:1467
Bstring
Definition: Bstring.h:29
md_kill
int md_kill(Bmd *md)
Deallocates a molecular dynamics structure.
Definition: rwmd.cpp:417
md_bond_list_set_parameters
double md_bond_list_set_parameters(Bbond *bondlist, Bbondtype *bondtype)
Assigns covalent bond lengths to bonds.
Definition: mol_bonds.cpp:299
Bstring::integer
long integer()
Definition: Bstring.cpp:547
verbose
int verbose
Boption::next
Boption * next
Definition: options.h:28
Bmolgroup::mol
Bmolecule * mol
Definition: rwmolecule.h:189
Vector3< double >
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:280
rwmd.h
Header to read and write molecular dynamics parameters in STAR format.
get_option_list
Boption * get_option_list(const char *use[], int argc, char *argv[], int &optind)
Parses command line arguments based on a template.
Definition: options.cpp:40
Bstring::real
double real()
Definition: Bstring.cpp:553
read_molecule
Bmolgroup * read_molecule(const char *filename, const char *select, const char *paramfile)
The generalized function for reading molecular files.
Definition: rwmolecule.cpp:818
options.h
Header file for option handlers.
Bmd
Definition: rwmd.h:56
Bmd::Kbond
double Kbond
Definition: rwmd.h:59
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:111
Boption::tag
Bstring tag
Definition: options.h:29
mol_bonds.h
Header for molecular dynamics.
rwmolecule.h
Header file for reading reflection files.
use
const char * use[]
Definition: bmd.cpp:27
Bbond
Definition: rwmolecule.h:134
Boption::value
Bstring value
Definition: options.h:30
Bmolgroup::box
Vector3< double > box
Definition: rwmolecule.h:184
timer.h
Utilities for timing functions.
Bmd::Kelec
double Kelec
Definition: rwmd.h:61
VERB_TIME
#define VERB_TIME
Definition: utilities.h:114
timer_start
double timer_start()
Starts timer and prints the time.
Definition: timer.cpp:61
utilities.h
Header file for general utilities.
molgroup_density
double molgroup_density(Bmolgroup *molgroup)
Calculates the number and mass density of a molecular system.
Definition: mol_util.cpp:666
md_generate_angle_list
Bangle * md_generate_angle_list(Bmolgroup *molgroup, Bmd *md)
Generates an angle list.
Definition: mol_bonds.cpp:415
Bmd::Kangle
double Kangle
Definition: rwmd.h:60
Boption::size
Vector3< long > size()
Definition: options.h:163
Bmd::Kfriction
double Kfriction
Definition: rwmd.h:58
Bstring::length
long length() const
Definition: Bstring.cpp:263
timer_report
double timer_report(double lasttime)
Reports the time elapsed since a given time.
Definition: timer.cpp:76
Bmolgroup::max
Vector3< double > max
Definition: rwmolecule.h:183
write_molecule
int write_molecule(char *filename, Bmolgroup *molgroup)
Writes a molecule group.
Definition: rwmolecule.cpp:975
md_show_bonds
int md_show_bonds(Bmolgroup *molgroup)
Shows the bonds and bond lengths.
Definition: mol_bonds.cpp:329
Vector3::volume
double volume()
Definition: Vector3.h:237
md_calculate_radial_deviation
int md_calculate_radial_deviation(Bmolgroup *molgroup)
Calculates the radial deviations from the center of the box.
Definition: mol_bonds.cpp:809
Bmolgroup::min
Vector3< double > min
Definition: rwmolecule.h:182
md_generate_bond_list
Bbond * md_generate_bond_list(Bmolgroup *molgroup, Bmd *md)
Generates a distance-based bond list.
Definition: mol_bonds.cpp:30
Matrix.h
Generalized matrix class.
Bmd::bond
Bbondtype * bond
Definition: rwmd.h:84
main
int main(int argc, char **argv)
Definition: bmd.cpp:61
linked_list.h
Header file for generalized linked list functions.
Boption::filename
Bstring filename()
Definition: options.h:85
option_kill
int option_kill(Boption *option)
Deallocates a linked list of option structures.
Definition: options.cpp:111
Bmd::Kvdw
double Kvdw
Definition: rwmd.h:62
md_leapfrog
double md_leapfrog(Bmolgroup *molgroup, Bmd *md, int max_iter, double velocitylimit)
Molecular dynamics using the leapfrog integrator.
Definition: mol_md.cpp:38
md_generate_molecular_bond_list
Bbond * md_generate_molecular_bond_list(Bmolgroup *molgroup, Bmd *md)
Generates an intramolecular distance-based bond list.
Definition: mol_bonds.cpp:105
Bmolgroup
Definition: rwmolecule.h:170
Bstring::contains
bool contains(const Bstring &s)
Definition: Bstring.cpp:592
Bmd::wrap
int wrap
Definition: rwmd.h:72
md_show_bond_stats
int md_show_bond_stats(Bmolgroup *molgroup)
Shows the number of bonds and the valency per atom.
Definition: mol_bonds.cpp:371
Bmd::cutoff
double cutoff
Definition: rwmd.h:69
read_md_parameters
Bmd * read_md_parameters(Bstring &filename)
Reading molecular dynamics parameters from STAR files.
Definition: rwmd.cpp:80
Bmd::timestep
double timestep
Definition: rwmd.h:57
mol_md.h
Header for molecular dynamics.
write_md_parameters
int write_md_parameters(Bstring &filename, Bmd *md)
Writing molecular dynamics parameters to a STAR file.
Definition: rwmd.cpp:237
mol_util.h
Library routines used for atomic coordinates.
md_calculate_deviations
double md_calculate_deviations(Bmolgroup *molgroup, int wrap)
Calculates deviations from the reference bond lengths and angles.
Definition: mol_bonds.cpp:742