ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/interface_implementation/SimSetup.cpp
Revision: 10
Committed: Tue Jul 9 18:40:59 2002 UTC (21 years, 11 months ago) by mmeineke
Original Path: branches/mmeineke/mdtools/interface_implementation/SimSetup.cpp
File size: 16415 byte(s)
Log Message:
everything you need to make libmdtools

File Contents

# User Rev Content
1 mmeineke 10 #include <cstdlib>
2     #include <iostream>
3     #include <cmath>
4    
5     #include "SimSetup.hpp"
6     #include "parse_me.h"
7     #include "LRI.hpp"
8     #include "Integrator.hpp"
9    
10     SimSetup::SimSetup(){
11     stamps = new MakeStamps();
12     globals = new Globals();
13     }
14    
15     SimSetup::~SimSetup(){
16     delete stamps;
17     delete globals;
18     }
19    
20     void SimSetup::parseFile( char* fileName ){
21    
22     inFileName = fileName;
23     set_interface_stamps( stamps, globals );
24     yacc_BASS( fileName );
25     }
26    
27     void SimSetup::createSim( void ){
28    
29     MakeStamps *the_stamps;
30     Globals* the_globals;
31     int i;
32    
33     // get the stamps and globals;
34     the_stamps = stamps;
35     the_globals = globals;
36    
37     // set the easy ones first
38     simnfo->target_temp = the_globals->getTargetTemp();
39     simnfo->dt = the_globals->getDt();
40     simnfo->run_time = the_globals->getRunTime();
41    
42     // get the ones we know are there, yet still may need some work.
43     n_components = the_globals->getNComponents();
44     strcpy( force_field, the_globals->getForceField() );
45     strcpy( ensemble, the_globals->getEnsemble() );
46    
47     if( !strcmp( force_field, "TraPPE" ) ) the_ff = new TraPPEFF();
48     else if( !strcmp( force_field, "DipoleTest" ) ) the_ff = new DipoleTestFF();
49     else if( !strcmp( force_field, "TraPPE_Ex" ) ) the_ff = new TraPPE_ExFF();
50     else{
51     std::cerr<< "SimSetup Error. Unrecognized force field -> "
52     << force_field << "\n";
53     exit(8);
54     }
55    
56     // get the components and calculate the tot_nMol and indvidual n_mol
57     the_components = the_globals->getComponents();
58     components_nmol = new int[n_components];
59     comp_stamps = new MoleculeStamp*[n_components];
60    
61     if( !the_globals->haveNMol() ){
62     // we don't have the total number of molecules, so we assume it is
63     // given in each component
64    
65     tot_nmol = 0;
66     for( i=0; i<n_components; i++ ){
67    
68     if( !the_components[i]->haveNMol() ){
69     // we have a problem
70     std::cerr << "SimSetup Error. No global NMol or component NMol"
71     << " given. Cannot calculate the number of atoms.\n";
72     exit( 8 );
73     }
74    
75     tot_nmol += the_components[i]->getNMol();
76     components_nmol[i] = the_components[i]->getNMol();
77     }
78     }
79     else{
80     std::cerr << "NOT A SUPPORTED FEATURE\n";
81    
82     // tot_nmol = the_globals->getNMol();
83    
84     // //we have the total number of molecules, now we check for molfractions
85     // for( i=0; i<n_components; i++ ){
86    
87     // if( !the_components[i]->haveMolFraction() ){
88    
89     // if( !the_components[i]->haveNMol() ){
90     // //we have a problem
91     // std::cerr << "SimSetup error. Neither molFraction nor "
92     // << " nMol was given in component
93    
94     }
95    
96     // make an array of molecule stamps that match the components used.
97    
98     for( i=0; i<n_components; i++ ){
99    
100     comp_stamps[i] =
101     the_stamps->getMolecule( the_components[i]->getType() );
102     }
103    
104    
105    
106     // caclulate the number of atoms, bonds, bends and torsions
107    
108     tot_atoms = 0;
109     tot_bonds = 0;
110     tot_bends = 0;
111     tot_torsions = 0;
112     for( i=0; i<n_components; i++ ){
113    
114     tot_atoms += components_nmol[i] * comp_stamps[i]->getNAtoms();
115     tot_bonds += components_nmol[i] * comp_stamps[i]->getNBonds();
116     tot_bends += components_nmol[i] * comp_stamps[i]->getNBends();
117     tot_torsions += components_nmol[i] * comp_stamps[i]->getNTorsions();
118     }
119    
120     tot_SRI = tot_bonds + tot_bends + tot_torsions;
121    
122     simnfo->n_atoms = tot_atoms;
123     simnfo->n_bonds = tot_bonds;
124     simnfo->n_bends = tot_bends;
125     simnfo->n_torsions = tot_torsions;
126     simnfo->n_SRI = tot_SRI;
127    
128     // create the atom and short range interaction arrays
129    
130     the_atoms = new Atom*[tot_atoms];
131     // the_molecules = new Molecule[tot_nmol];
132    
133    
134     if( tot_SRI ){
135     the_sris = new SRI*[tot_SRI];
136     the_excludes = new ex_pair[tot_SRI];
137     }
138    
139     // set the arrays into the SimInfo object
140    
141     simnfo->atoms = the_atoms;
142     simnfo->sr_interactions = the_sris;
143     simnfo->n_exclude = tot_SRI;
144     simnfo->excludes = the_excludes;
145    
146     // initialize the arrays
147    
148     the_ff->setSimInfo( simnfo );
149    
150     makeAtoms();
151    
152     if( tot_bonds ){
153     makeBonds();
154     }
155    
156     if( tot_bends ){
157     makeBends();
158     }
159    
160     if( tot_torsions ){
161     makeTorsions();
162     }
163    
164     // makeMolecules();
165    
166     // get some of the tricky things that may still be in the globals
167    
168     if( simnfo->n_dipoles ){
169    
170     if( !the_globals->haveRRF() ){
171     std::cerr << "SimSetup Error, system has dipoles, but no rRF was set.\n";
172     exit(8);
173     }
174     if( !the_globals->haveDielectric() ){
175     std::cerr << "SimSetup Error, system has dipoles, but no"
176     << " dielectric was set.\n";
177     exit(8);
178     }
179    
180     simnfo->rRF = the_globals->getRRF();
181     simnfo->dielectric = the_globals->getDielectric();
182     }
183    
184     if( the_globals->haveBox() ){
185     simnfo->box_x = the_globals->getBox();
186     simnfo->box_y = the_globals->getBox();
187     simnfo->box_z = the_globals->getBox();
188     }
189     else if( the_globals->haveDensity() ){
190    
191     double vol;
192     vol = (double)tot_nmol / the_globals->getDensity();
193     simnfo->box_x = pow( vol, ( 1.0 / 3.0 ) );
194     simnfo->box_y = simnfo->box_x;
195     simnfo->box_z = simnfo->box_x;
196     }
197     else{
198     if( !the_globals->haveBoxX() ){
199     std::cerr << "SimSetup error, no periodic BoxX size given.\n";
200     exit(8);
201     }
202     simnfo->box_x = the_globals->getBoxX();
203    
204     if( !the_globals->haveBoxY() ){
205     std::cerr << "SimSetup error, no periodic BoxY size given.\n";
206     exit(8);
207     }
208     simnfo->box_y = the_globals->getBoxY();
209    
210     if( !the_globals->haveBoxZ() ){
211     std::cerr << "SimSetup error, no periodic BoxZ size given.\n";
212     exit(8);
213     }
214     simnfo->box_z = the_globals->getBoxZ();
215     }
216    
217     if( the_globals->haveInitialConfig() ){
218     InitializeFromFile* fileInit;
219     fileInit = new InitializeFromFile( the_globals->getInitialConfig() );
220    
221     fileInit->read_xyz( simnfo ); // default velocities on
222    
223     delete fileInit;
224     }
225     else{
226     initFromBass();
227     }
228    
229     if( the_globals->haveFinalConfig() ){
230     strcpy( simnfo->finalName, the_globals->getFinalConfig() );
231     }
232     else{
233     strcpy( simnfo->finalName, inFileName );
234     char* endTest;
235     int nameLength = strlen( simnfo->finalName );
236     endTest = &(simnfo->finalName[nameLength - 5]);
237     if( !strcmp( endTest, ".bass" ) ){
238     strcpy( endTest, ".eor" );
239     }
240     else if( !strcmp( endTest, ".BASS" ) ){
241     strcpy( endTest, ".eor" );
242     }
243     else{
244     endTest = &(simnfo->finalName[nameLength - 4]);
245     if( !strcmp( endTest, ".bss" ) ){
246     strcpy( endTest, ".eor" );
247     }
248     else if( !strcmp( endTest, ".mdl" ) ){
249     strcpy( endTest, ".eor" );
250     }
251     else{
252     strcat( simnfo->finalName, ".eor" );
253     }
254     }
255     }
256    
257     // make the sample and status out names
258    
259     strcpy( simnfo->sampleName, inFileName );
260     char* endTest;
261     int nameLength = strlen( simnfo->sampleName );
262     endTest = &(simnfo->sampleName[nameLength - 5]);
263     if( !strcmp( endTest, ".bass" ) ){
264     strcpy( endTest, ".dump" );
265     }
266     else if( !strcmp( endTest, ".BASS" ) ){
267     strcpy( endTest, ".dump" );
268     }
269     else{
270     endTest = &(simnfo->sampleName[nameLength - 4]);
271     if( !strcmp( endTest, ".bss" ) ){
272     strcpy( endTest, ".dump" );
273     }
274     else if( !strcmp( endTest, ".mdl" ) ){
275     strcpy( endTest, ".dump" );
276     }
277     else{
278     strcat( simnfo->sampleName, ".dump" );
279     }
280     }
281    
282     strcpy( simnfo->statusName, inFileName );
283     nameLength = strlen( simnfo->statusName );
284     endTest = &(simnfo->statusName[nameLength - 5]);
285     if( !strcmp( endTest, ".bass" ) ){
286     strcpy( endTest, ".stat" );
287     }
288     else if( !strcmp( endTest, ".BASS" ) ){
289     strcpy( endTest, ".stat" );
290     }
291     else{
292     endTest = &(simnfo->statusName[nameLength - 4]);
293     if( !strcmp( endTest, ".bss" ) ){
294     strcpy( endTest, ".stat" );
295     }
296     else if( !strcmp( endTest, ".mdl" ) ){
297     strcpy( endTest, ".stat" );
298     }
299     else{
300     strcat( simnfo->statusName, ".stat" );
301     }
302     }
303    
304     // set the status, sample, and themal kick times
305    
306     if( the_globals->haveSampleTime() ){
307     simnfo->sampleTime = the_globals->getSampleTime();
308     simnfo->statusTime = simnfo->sampleTime;
309     simnfo->thermalTime = simnfo->sampleTime;
310     }
311     else{
312     simnfo->sampleTime = the_globals->getRunTime();
313     simnfo->statusTime = simnfo->sampleTime;
314     simnfo->thermalTime = simnfo->sampleTime;
315     }
316    
317     if( the_globals->haveStatusTime() ){
318     simnfo->statusTime = the_globals->getStatusTime();
319     }
320    
321     if( the_globals->haveThermalTime() ){
322     simnfo->thermalTime = the_globals->getThermalTime();
323     }
324    
325     // check for the temperature set flag
326    
327     if( the_globals->haveTempSet() ) simnfo->setTemp = the_globals->getTempSet();
328    
329    
330     // make the longe range forces and the integrator
331    
332     new AllLong( simnfo );
333    
334     if( !strcmp( force_field, "TraPPE" ) ) new Verlet( *simnfo );
335     if( !strcmp( force_field, "DipoleTest" ) ) new Symplectic( simnfo );
336     if( !strcmp( force_field, "TraPPE_Ex" ) ) new Symplectic( simnfo );
337     }
338    
339     void SimSetup::makeAtoms( void ){
340    
341     int i, j, k, index;
342     double ux, uy, uz, uSqr, u;
343     AtomStamp* current_atom;
344     DirectionalAtom* dAtom;
345    
346     index = 0;
347     for( i=0; i<n_components; i++ ){
348    
349     for( j=0; j<components_nmol[i]; j++ ){
350    
351     for( k=0; k<comp_stamps[i]->getNAtoms(); k++ ){
352    
353     current_atom = comp_stamps[i]->getAtom( k );
354     if( current_atom->haveOrientation() ){
355    
356     dAtom = new DirectionalAtom;
357     simnfo->n_oriented++;
358     the_atoms[index] = dAtom;
359    
360     ux = current_atom->getOrntX();
361     uy = current_atom->getOrntY();
362     uz = current_atom->getOrntZ();
363    
364     uSqr = (ux * ux) + (uy * uy) + (uz * uz);
365    
366     u = sqrt( uSqr );
367     ux = ux / u;
368     uy = uy / u;
369     uz = uz / u;
370    
371     dAtom->setSUx( ux );
372     dAtom->setSUy( uy );
373     dAtom->setSUz( uz );
374     }
375     else{
376     the_atoms[index] = new GeneralAtom;
377     }
378     the_atoms[index]->setType( current_atom->getType() );
379     the_atoms[index]->setIndex( index );
380    
381     // increment the index and repeat;
382     index++;
383     }
384     }
385     }
386    
387     the_ff->initializeAtoms();
388     }
389    
390     void SimSetup::makeBonds( void ){
391    
392     int i, j, k, index, offset;
393     bond_pair* the_bonds;
394     BondStamp* current_bond;
395    
396     the_bonds = new bond_pair[tot_bonds];
397     index = 0;
398     offset = 0;
399     for( i=0; i<n_components; i++ ){
400    
401     for( j=0; j<components_nmol[i]; j++ ){
402    
403     for( k=0; k<comp_stamps[i]->getNBonds(); k++ ){
404    
405     current_bond = comp_stamps[i]->getBond( k );
406     the_bonds[index].a = current_bond->getA() + offset;
407     the_bonds[index].b = current_bond->getB() + offset;
408    
409     the_excludes[index].i = the_bonds[index].a;
410     the_excludes[index].j = the_bonds[index].b;
411    
412     // increment the index and repeat;
413     index++;
414     }
415     offset += comp_stamps[i]->getNAtoms();
416     }
417     }
418    
419     the_ff->initializeBonds( the_bonds );
420     }
421    
422     void SimSetup::makeBends( void ){
423    
424     int i, j, k, index, offset;
425     bend_set* the_bends;
426     BendStamp* current_bend;
427    
428     the_bends = new bend_set[tot_bends];
429     index = 0;
430     offset = 0;
431     for( i=0; i<n_components; i++ ){
432    
433     for( j=0; j<components_nmol[i]; j++ ){
434    
435     for( k=0; k<comp_stamps[i]->getNBends(); k++ ){
436    
437     current_bend = comp_stamps[i]->getBend( k );
438     the_bends[index].a = current_bend->getA() + offset;
439     the_bends[index].b = current_bend->getB() + offset;
440     the_bends[index].c = current_bend->getC() + offset;
441    
442     the_excludes[index + tot_bonds].i = the_bends[index].a;
443     the_excludes[index + tot_bonds].j = the_bends[index].c;
444    
445     // increment the index and repeat;
446     index++;
447     }
448     offset += comp_stamps[i]->getNAtoms();
449     }
450     }
451    
452     the_ff->initializeBends( the_bends );
453     }
454    
455     void SimSetup::makeTorsions( void ){
456    
457     int i, j, k, index, offset;
458     torsion_set* the_torsions;
459     TorsionStamp* current_torsion;
460    
461     the_torsions = new torsion_set[tot_torsions];
462     index = 0;
463     offset = 0;
464     for( i=0; i<n_components; i++ ){
465    
466     for( j=0; j<components_nmol[i]; j++ ){
467    
468     for( k=0; k<comp_stamps[i]->getNTorsions(); k++ ){
469    
470     current_torsion = comp_stamps[i]->getTorsion( k );
471     the_torsions[index].a = current_torsion->getA() + offset;
472     the_torsions[index].b = current_torsion->getB() + offset;
473     the_torsions[index].c = current_torsion->getC() + offset;
474     the_torsions[index].d = current_torsion->getD() + offset;
475    
476     the_excludes[index + tot_bonds + tot_bends].i = the_torsions[index].a;
477     the_excludes[index + tot_bonds + tot_bends].j = the_torsions[index].d;
478    
479     // increment the index and repeat;
480     index++;
481     }
482     offset += comp_stamps[i]->getNAtoms();
483     }
484     }
485    
486     the_ff->initializeTorsions( the_torsions );
487     }
488    
489     void SimSetup::makeMolecules( void ){
490    
491     //empy for now
492     }
493    
494     void SimSetup::initFromBass( void ){
495    
496     int i, j, k;
497     int n_cells;
498     double cellx, celly, cellz;
499     double temp1, temp2, temp3;
500     int n_per_extra;
501     int n_extra;
502     int have_extra, done;
503    
504     temp1 = (double)tot_nmol / 4.0;
505     temp2 = pow( temp1, ( 1.0 / 3.0 ) );
506     temp3 = ceil( temp2 );
507    
508     have_extra =0;
509     if( temp2 < temp3 ){ // we have a non-complete lattice
510     have_extra =1;
511    
512     n_cells = (int)temp3 - 1;
513     cellx = simnfo->box_x / temp3;
514     celly = simnfo->box_y / temp3;
515     cellz = simnfo->box_z / temp3;
516     n_extra = tot_nmol - ( 4 * n_cells * n_cells * n_cells );
517     temp1 = ((double)n_extra) / ( pow( temp3, 3.0 ) - pow( n_cells, 3.0 ) );
518     n_per_extra = (int)ceil( temp1 );
519    
520     if( n_per_extra > 4){
521     std::cerr << "THere has been an error in constructing the non-complete lattice.\n";
522     exit(8);
523     }
524     }
525     else{
526     n_cells = (int)temp3;
527     cellx = simnfo->box_x / temp3;
528     celly = simnfo->box_y / temp3;
529     cellz = simnfo->box_z / temp3;
530     }
531    
532     current_mol = 0;
533     current_comp_mol = 0;
534     current_comp = 0;
535     current_atom_ndx = 0;
536    
537     for( i=0; i < n_cells ; i++ ){
538     for( j=0; j < n_cells; j++ ){
539     for( k=0; k < n_cells; k++ ){
540    
541     makeElement( i * cellx,
542     j * celly,
543     k * cellz );
544    
545     makeElement( i * cellx + 0.5 * cellx,
546     j * celly + 0.5 * celly,
547     k * cellz );
548    
549     makeElement( i * cellx,
550     j * celly + 0.5 * celly,
551     k * cellz + 0.5 * cellz );
552    
553     makeElement( i * cellx + 0.5 * cellx,
554     j * celly,
555     k * cellz + 0.5 * cellz );
556     }
557     }
558     }
559    
560     if( have_extra ){
561     done = 0;
562    
563     int start_ndx;
564     for( i=0; i < (n_cells+1) && !done; i++ ){
565     for( j=0; j < (n_cells+1) && !done; j++ ){
566    
567     if( i < n_cells ){
568    
569     if( j < n_cells ){
570     start_ndx = n_cells;
571     }
572     else start_ndx = 0;
573     }
574     else start_ndx = 0;
575    
576     for( k=start_ndx; k < (n_cells+1) && !done; k++ ){
577    
578     makeElement( i * cellx,
579     j * celly,
580     k * cellz );
581     done = ( current_mol >= tot_nmol );
582    
583     if( !done && n_per_extra > 1 ){
584     makeElement( i * cellx + 0.5 * cellx,
585     j * celly + 0.5 * celly,
586     k * cellz );
587     done = ( current_mol >= tot_nmol );
588     }
589    
590     if( !done && n_per_extra > 2){
591     makeElement( i * cellx,
592     j * celly + 0.5 * celly,
593     k * cellz + 0.5 * cellz );
594     done = ( current_mol >= tot_nmol );
595     }
596    
597     if( !done && n_per_extra > 3){
598     makeElement( i * cellx + 0.5 * cellx,
599     j * celly,
600     k * cellz + 0.5 * cellz );
601     done = ( current_mol >= tot_nmol );
602     }
603     }
604     }
605     }
606     }
607    
608    
609     for( i=0; i<simnfo->n_atoms; i++ ){
610     simnfo->atoms[i]->set_vx( 0.0 );
611     simnfo->atoms[i]->set_vy( 0.0 );
612     simnfo->atoms[i]->set_vz( 0.0 );
613     }
614     }
615    
616     void SimSetup::makeElement( double x, double y, double z ){
617    
618     int k;
619     AtomStamp* current_atom;
620     DirectionalAtom* dAtom;
621     double rotMat[3][3];
622    
623     for( k=0; k<comp_stamps[current_comp]->getNAtoms(); k++ ){
624    
625     current_atom = comp_stamps[current_comp]->getAtom( k );
626     if( !current_atom->havePosition() ){
627     std::cerr << "Component " << comp_stamps[current_comp]->getID()
628     << ", atom " << current_atom->getType()
629     << " does not have a position specified.\n"
630     << "The initialization routine is unable to give a start"
631     << " position.\n";
632     exit(8);
633     }
634    
635     the_atoms[current_atom_ndx]->setX( x + current_atom->getPosX() );
636     the_atoms[current_atom_ndx]->setY( y + current_atom->getPosY() );
637     the_atoms[current_atom_ndx]->setZ( z + current_atom->getPosZ() );
638    
639     if( the_atoms[current_atom_ndx]->isDirectional() ){
640    
641     dAtom = (DirectionalAtom *)the_atoms[current_atom_ndx];
642    
643     rotMat[0][0] = 1.0;
644     rotMat[0][1] = 0.0;
645     rotMat[0][2] = 0.0;
646    
647     rotMat[1][0] = 0.0;
648     rotMat[1][1] = 1.0;
649     rotMat[1][2] = 0.0;
650    
651     rotMat[2][0] = 0.0;
652     rotMat[2][1] = 0.0;
653     rotMat[2][2] = 1.0;
654    
655     dAtom->setA( rotMat );
656     }
657    
658     current_atom_ndx++;
659     }
660    
661     current_mol++;
662     current_comp_mol++;
663    
664     if( current_comp_mol >= components_nmol[current_comp] ){
665    
666     current_comp_mol = 0;
667     current_comp++;
668     }
669     }