ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/interface_implementation/SimSetup.cpp
Revision: 254
Committed: Thu Jan 30 20:03:37 2003 UTC (21 years, 5 months ago) by chuckv
File size: 23069 byte(s)
Log Message:
Bug fixes for mpi version of code

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 mmeineke 176 #include "simError.h"
10 mmeineke 144
11 chuckv 131 #ifdef IS_MPI
12 chuckv 139 #include "mpiBASS.h"
13 chuckv 215 #include "mpiSimulation.hpp"
14 chuckv 131 #include "bassDiag.hpp"
15     #endif
16 mmeineke 10
17     SimSetup::SimSetup(){
18     stamps = new MakeStamps();
19     globals = new Globals();
20 mmeineke 164
21     #ifdef IS_MPI
22     strcpy( checkPointMsg, "SimSetup creation successful" );
23     MPIcheckPoint();
24 mmeineke 176 #endif // IS_MPI
25 mmeineke 10 }
26    
27     SimSetup::~SimSetup(){
28     delete stamps;
29     delete globals;
30     }
31    
32     void SimSetup::parseFile( char* fileName ){
33    
34 chuckv 131 #ifdef IS_MPI
35 mmeineke 164 if( worldRank == 0 ){
36     #endif // is_mpi
37    
38     inFileName = fileName;
39     set_interface_stamps( stamps, globals );
40    
41     #ifdef IS_MPI
42     mpiEventInit();
43 chuckv 124 #endif
44 mmeineke 164
45     yacc_BASS( fileName );
46    
47 chuckv 131 #ifdef IS_MPI
48 mmeineke 164 throwMPIEvent(NULL);
49     }
50 mmeineke 176 else receiveParse();
51 chuckv 124 #endif
52    
53 mmeineke 10 }
54    
55 chuckv 131 #ifdef IS_MPI
56 chuckv 124 void SimSetup::receiveParse(void){
57    
58     set_interface_stamps( stamps, globals );
59     mpiEventInit();
60 mmeineke 164 MPIcheckPoint();
61 chuckv 124 mpiEventLoop();
62    
63     }
64    
65 chuckv 131
66 chuckv 128 void SimSetup::testMe(void){
67     bassDiag* dumpMe = new bassDiag(globals,stamps);
68     dumpMe->dumpStamps();
69     delete dumpMe;
70     }
71 chuckv 131 #endif
72 mmeineke 176
73 mmeineke 10 void SimSetup::createSim( void ){
74    
75     MakeStamps *the_stamps;
76     Globals* the_globals;
77 mmeineke 202 int i, j;
78 mmeineke 10
79     // get the stamps and globals;
80     the_stamps = stamps;
81     the_globals = globals;
82    
83     // set the easy ones first
84     simnfo->target_temp = the_globals->getTargetTemp();
85     simnfo->dt = the_globals->getDt();
86     simnfo->run_time = the_globals->getRunTime();
87    
88     // get the ones we know are there, yet still may need some work.
89     n_components = the_globals->getNComponents();
90     strcpy( force_field, the_globals->getForceField() );
91     strcpy( ensemble, the_globals->getEnsemble() );
92 chuckv 124
93 mmeineke 10 if( !strcmp( force_field, "TraPPE" ) ) the_ff = new TraPPEFF();
94     else if( !strcmp( force_field, "DipoleTest" ) ) the_ff = new DipoleTestFF();
95     else if( !strcmp( force_field, "TraPPE_Ex" ) ) the_ff = new TraPPE_ExFF();
96 chuckv 215 else if( !strcmp( force_field, "LJ" ) ) the_ff = new LJ_FF();
97 mmeineke 10 else{
98 mmeineke 176 sprintf( painCave.errMsg,
99     "SimSetup Error. Unrecognized force field -> %s\n",
100     force_field );
101     painCave.isFatal = 1;
102     simError();
103 mmeineke 10 }
104    
105 mmeineke 176 #ifdef IS_MPI
106     strcpy( checkPointMsg, "ForceField creation successful" );
107     MPIcheckPoint();
108     #endif // is_mpi
109    
110 mmeineke 10 // get the components and calculate the tot_nMol and indvidual n_mol
111     the_components = the_globals->getComponents();
112     components_nmol = new int[n_components];
113     comp_stamps = new MoleculeStamp*[n_components];
114 chuckv 124
115 mmeineke 10 if( !the_globals->haveNMol() ){
116 chuckv 124 // we don't have the total number of molecules, so we assume it is
117 mmeineke 10 // given in each component
118    
119     tot_nmol = 0;
120     for( i=0; i<n_components; i++ ){
121 chuckv 124
122 mmeineke 10 if( !the_components[i]->haveNMol() ){
123     // we have a problem
124 mmeineke 189 sprintf( painCave.errMsg,
125     "SimSetup Error. No global NMol or component NMol"
126     " given. Cannot calculate the number of atoms.\n" );
127     painCave.isFatal = 1;
128     simError();
129 mmeineke 10 }
130    
131     tot_nmol += the_components[i]->getNMol();
132     components_nmol[i] = the_components[i]->getNMol();
133     }
134     }
135     else{
136 mmeineke 189 sprintf( painCave.errMsg,
137     "SimSetup error.\n"
138     "\tSorry, the ability to specify total"
139     " nMols and then give molfractions in the components\n"
140     "\tis not currently supported."
141     " Please give nMol in the components.\n" );
142     painCave.isFatal = 1;
143     simError();
144    
145    
146     // tot_nmol = the_globals->getNMol();
147    
148     // //we have the total number of molecules, now we check for molfractions
149     // for( i=0; i<n_components; i++ ){
150    
151     // if( !the_components[i]->haveMolFraction() ){
152    
153     // if( !the_components[i]->haveNMol() ){
154     // //we have a problem
155     // std::cerr << "SimSetup error. Neither molFraction nor "
156     // << " nMol was given in component
157    
158     }
159 chuckv 124
160 mmeineke 189 #ifdef IS_MPI
161     strcpy( checkPointMsg, "Have the number of components" );
162     MPIcheckPoint();
163     #endif // is_mpi
164 chuckv 124
165 mmeineke 10 // make an array of molecule stamps that match the components used.
166 mmeineke 198 // also extract the used stamps out into a separate linked list
167 mmeineke 10
168 mmeineke 198 simnfo->nComponents = n_components;
169     simnfo->componentsNmol = components_nmol;
170     simnfo->compStamps = comp_stamps;
171     simnfo->headStamp = new LinkedMolStamp();
172    
173     char* id;
174     LinkedMolStamp* headStamp = simnfo->headStamp;
175     LinkedMolStamp* currentStamp = NULL;
176 mmeineke 10 for( i=0; i<n_components; i++ ){
177    
178 mmeineke 198 id = the_components[i]->getType();
179     comp_stamps[i] = NULL;
180    
181     // check to make sure the component isn't already in the list
182    
183     comp_stamps[i] = headStamp->match( id );
184     if( comp_stamps[i] == NULL ){
185    
186     // extract the component from the list;
187    
188     currentStamp = the_stamps->extractMolStamp( id );
189     if( currentStamp == NULL ){
190     sprintf( painCave.errMsg,
191     "SimSetup error: Component \"%s\" was not found in the "
192 chuckv 249 "list of declared molecules\n",
193 mmeineke 198 id );
194     painCave.isFatal = 1;
195     simError();
196     }
197    
198     headStamp->add( currentStamp );
199     comp_stamps[i] = headStamp->match( id );
200     }
201 mmeineke 10 }
202    
203 mmeineke 198 #ifdef IS_MPI
204     strcpy( checkPointMsg, "Component stamps successfully extracted\n" );
205     MPIcheckPoint();
206     #endif // is_mpi
207    
208 mmeineke 10
209 chuckv 124
210 mmeineke 198
211 mmeineke 10 // caclulate the number of atoms, bonds, bends and torsions
212    
213     tot_atoms = 0;
214     tot_bonds = 0;
215     tot_bends = 0;
216     tot_torsions = 0;
217     for( i=0; i<n_components; i++ ){
218 mmeineke 202
219     tot_atoms += components_nmol[i] * comp_stamps[i]->getNAtoms();
220     tot_bonds += components_nmol[i] * comp_stamps[i]->getNBonds();
221     tot_bends += components_nmol[i] * comp_stamps[i]->getNBends();
222 mmeineke 10 tot_torsions += components_nmol[i] * comp_stamps[i]->getNTorsions();
223     }
224 chuckv 124
225 mmeineke 10 tot_SRI = tot_bonds + tot_bends + tot_torsions;
226 chuckv 124
227 mmeineke 10 simnfo->n_atoms = tot_atoms;
228     simnfo->n_bonds = tot_bonds;
229     simnfo->n_bends = tot_bends;
230     simnfo->n_torsions = tot_torsions;
231     simnfo->n_SRI = tot_SRI;
232 chuckv 195 simnfo->n_mol = tot_nmol;
233 mmeineke 10
234 chuckv 195
235     #ifdef IS_MPI
236    
237 chuckv 194 // divide the molecules among processors here.
238 chuckv 195
239 chuckv 215 mpiSim = new mpiSimulation( simnfo );
240 chuckv 195
241 chuckv 254 fprintf( stderr, "about to call divideLabour.\n" );
242    
243 chuckv 253 globalIndex = mpiSim->divideLabor();
244 chuckv 194
245 chuckv 254 fprintf(stderr, "we're back from divideLabour\n" );
246    
247 mmeineke 202 // set up the local variables
248    
249 mmeineke 205 int localMol, allMol;
250 mmeineke 202 int local_atoms, local_bonds, local_bends, local_torsions, local_SRI;
251    
252 mmeineke 205 allMol = 0;
253 mmeineke 202 localMol = 0;
254     local_atoms = 0;
255     local_bonds = 0;
256     local_bends = 0;
257     local_torsions = 0;
258     for( i=0; i<n_components; i++ ){
259    
260     for( j=0; j<components_nmol[i]; j++ ){
261    
262 mmeineke 205 if( mpiSim->getMyMolStart() <= allMol &&
263     allMol <= mpiSim->getMyMolEnd() ){
264 mmeineke 202
265     local_atoms += comp_stamps[i]->getNAtoms();
266     local_bonds += comp_stamps[i]->getNBonds();
267     local_bends += comp_stamps[i]->getNBends();
268     local_torsions += comp_stamps[i]->getNTorsions();
269     localMol++;
270     }
271 mmeineke 205 allMol++;
272 mmeineke 202 }
273     }
274 mmeineke 205 local_SRI = local_bonds + local_bends + local_torsions;
275 mmeineke 202
276    
277     simnfo->n_atoms = mpiSim->getMyNlocal();
278    
279 mmeineke 205 if( local_atoms != simnfo->n_atoms ){
280     sprintf( painCave.errMsg,
281     "SimSetup error: mpiSim's localAtom (%d) and SimSetup's"
282     " localAtom (%d) are note equal.\n",
283     simnfo->n_atoms,
284     local_atoms );
285     painCave.isFatal = 1;
286     simError();
287     }
288 mmeineke 202
289 mmeineke 205 simnfo->n_bonds = local_bonds;
290     simnfo->n_bends = local_bends;
291     simnfo->n_torsions = local_torsions;
292     simnfo->n_SRI = local_SRI;
293     simnfo->n_mol = localMol;
294    
295     strcpy( checkPointMsg, "Passed nlocal consistency check." );
296     MPIcheckPoint();
297    
298    
299 chuckv 195 #endif // is_mpi
300    
301 chuckv 194
302 mmeineke 10 // create the atom and short range interaction arrays
303 chuckv 124
304 chuckv 195 Atom::createArrays(simnfo->n_atoms);
305     the_atoms = new Atom*[simnfo->n_atoms];
306     the_molecules = new Molecule[simnfo->n_mol];
307 chuckv 124
308    
309 chuckv 195 if( simnfo->n_SRI ){
310     the_sris = new SRI*[simnfo->n_SRI];
311     the_excludes = new ex_pair[simnfo->n_SRI];
312 mmeineke 10 }
313    
314     // set the arrays into the SimInfo object
315    
316     simnfo->atoms = the_atoms;
317     simnfo->sr_interactions = the_sris;
318     simnfo->n_exclude = tot_SRI;
319     simnfo->excludes = the_excludes;
320    
321 chuckv 124
322 mmeineke 10 // get some of the tricky things that may still be in the globals
323    
324     if( simnfo->n_dipoles ){
325    
326     if( !the_globals->haveRRF() ){
327 mmeineke 189 sprintf( painCave.errMsg,
328     "SimSetup Error, system has dipoles, but no rRF was set.\n");
329     painCave.isFatal = 1;
330     simError();
331 mmeineke 10 }
332     if( !the_globals->haveDielectric() ){
333 mmeineke 189 sprintf( painCave.errMsg,
334     "SimSetup Error, system has dipoles, but no"
335     " dielectric was set.\n" );
336     painCave.isFatal = 1;
337     simError();
338 mmeineke 10 }
339    
340     simnfo->rRF = the_globals->getRRF();
341     simnfo->dielectric = the_globals->getDielectric();
342     }
343    
344 mmeineke 189 #ifdef IS_MPI
345     strcpy( checkPointMsg, "rRf and dielectric check out" );
346     MPIcheckPoint();
347     #endif // is_mpi
348    
349 mmeineke 10 if( the_globals->haveBox() ){
350     simnfo->box_x = the_globals->getBox();
351     simnfo->box_y = the_globals->getBox();
352     simnfo->box_z = the_globals->getBox();
353     }
354     else if( the_globals->haveDensity() ){
355 chuckv 124
356 mmeineke 10 double vol;
357     vol = (double)tot_nmol / the_globals->getDensity();
358     simnfo->box_x = pow( vol, ( 1.0 / 3.0 ) );
359     simnfo->box_y = simnfo->box_x;
360     simnfo->box_z = simnfo->box_x;
361     }
362     else{
363     if( !the_globals->haveBoxX() ){
364 mmeineke 189 sprintf( painCave.errMsg,
365     "SimSetup error, no periodic BoxX size given.\n" );
366     painCave.isFatal = 1;
367     simError();
368 mmeineke 10 }
369     simnfo->box_x = the_globals->getBoxX();
370    
371     if( !the_globals->haveBoxY() ){
372 mmeineke 189 sprintf( painCave.errMsg,
373     "SimSetup error, no periodic BoxY size given.\n" );
374     painCave.isFatal = 1;
375     simError();
376 mmeineke 10 }
377     simnfo->box_y = the_globals->getBoxY();
378    
379     if( !the_globals->haveBoxZ() ){
380 mmeineke 189 sprintf( painCave.errMsg,
381     "SimSetup error, no periodic BoxZ size given.\n" );
382     painCave.isFatal = 1;
383     simError();
384 mmeineke 10 }
385     simnfo->box_z = the_globals->getBoxZ();
386     }
387    
388 mmeineke 189 #ifdef IS_MPI
389     strcpy( checkPointMsg, "Box size set up" );
390     MPIcheckPoint();
391     #endif // is_mpi
392 chuckv 124
393 mmeineke 189
394 chuckv 248 // initialize the arrays
395 mmeineke 189
396 chuckv 248 the_ff->setSimInfo( simnfo );
397    
398     makeAtoms();
399    
400     if( tot_bonds ){
401     makeBonds();
402     }
403    
404     if( tot_bends ){
405     makeBends();
406     }
407    
408     if( tot_torsions ){
409     makeTorsions();
410     }
411    
412    
413    
414    
415    
416    
417 chuckv 206 if( the_globals->haveInitialConfig() ){
418    
419     InitializeFromFile* fileInit;
420     #ifdef IS_MPI // is_mpi
421     if( worldRank == 0 ){
422     #endif //is_mpi
423     fileInit = new InitializeFromFile( the_globals->getInitialConfig() );
424     #ifdef IS_MPI
425     }else fileInit = new InitializeFromFile( NULL );
426     #endif
427     fileInit->read_xyz( simnfo ); // default velocities on
428 chuckv 124
429 chuckv 206 delete fileInit;
430     }
431     else{
432 chuckv 124
433 chuckv 195 #ifdef IS_MPI
434    
435     // no init from bass
436    
437     sprintf( painCave.errMsg,
438     "Cannot intialize a parallel simulation without an initial configuration file.\n" );
439     painCave.isFatal;
440     simError();
441    
442     #else
443    
444 mmeineke 189 initFromBass();
445 chuckv 195
446 chuckv 206
447     #endif
448     }
449    
450 mmeineke 189 #ifdef IS_MPI
451 chuckv 195 strcpy( checkPointMsg, "Successfully read in the initial configuration" );
452 mmeineke 189 MPIcheckPoint();
453     #endif // is_mpi
454 mmeineke 10
455    
456 mmeineke 189
457    
458    
459 chuckv 206
460 mmeineke 189
461 mmeineke 184 #ifdef IS_MPI
462 mmeineke 189 if( worldRank == 0 ){
463 mmeineke 184 #endif // is_mpi
464 mmeineke 189
465     if( the_globals->haveFinalConfig() ){
466     strcpy( simnfo->finalName, the_globals->getFinalConfig() );
467     }
468     else{
469     strcpy( simnfo->finalName, inFileName );
470     char* endTest;
471     int nameLength = strlen( simnfo->finalName );
472     endTest = &(simnfo->finalName[nameLength - 5]);
473     if( !strcmp( endTest, ".bass" ) ){
474     strcpy( endTest, ".eor" );
475 mmeineke 184 }
476 mmeineke 189 else if( !strcmp( endTest, ".BASS" ) ){
477     strcpy( endTest, ".eor" );
478     }
479 mmeineke 184 else{
480 mmeineke 189 endTest = &(simnfo->finalName[nameLength - 4]);
481     if( !strcmp( endTest, ".bss" ) ){
482 mmeineke 184 strcpy( endTest, ".eor" );
483     }
484 mmeineke 189 else if( !strcmp( endTest, ".mdl" ) ){
485 mmeineke 184 strcpy( endTest, ".eor" );
486     }
487     else{
488 mmeineke 189 strcat( simnfo->finalName, ".eor" );
489 mmeineke 184 }
490     }
491 mmeineke 189 }
492    
493     // make the sample and status out names
494    
495     strcpy( simnfo->sampleName, inFileName );
496     char* endTest;
497     int nameLength = strlen( simnfo->sampleName );
498     endTest = &(simnfo->sampleName[nameLength - 5]);
499     if( !strcmp( endTest, ".bass" ) ){
500     strcpy( endTest, ".dump" );
501     }
502     else if( !strcmp( endTest, ".BASS" ) ){
503     strcpy( endTest, ".dump" );
504     }
505     else{
506     endTest = &(simnfo->sampleName[nameLength - 4]);
507     if( !strcmp( endTest, ".bss" ) ){
508 mmeineke 184 strcpy( endTest, ".dump" );
509     }
510 mmeineke 189 else if( !strcmp( endTest, ".mdl" ) ){
511 mmeineke 184 strcpy( endTest, ".dump" );
512     }
513     else{
514 mmeineke 189 strcat( simnfo->sampleName, ".dump" );
515 mmeineke 184 }
516 mmeineke 189 }
517    
518     strcpy( simnfo->statusName, inFileName );
519     nameLength = strlen( simnfo->statusName );
520     endTest = &(simnfo->statusName[nameLength - 5]);
521     if( !strcmp( endTest, ".bass" ) ){
522     strcpy( endTest, ".stat" );
523     }
524     else if( !strcmp( endTest, ".BASS" ) ){
525     strcpy( endTest, ".stat" );
526     }
527     else{
528     endTest = &(simnfo->statusName[nameLength - 4]);
529     if( !strcmp( endTest, ".bss" ) ){
530 mmeineke 184 strcpy( endTest, ".stat" );
531     }
532 mmeineke 189 else if( !strcmp( endTest, ".mdl" ) ){
533 mmeineke 184 strcpy( endTest, ".stat" );
534     }
535     else{
536 mmeineke 189 strcat( simnfo->statusName, ".stat" );
537 mmeineke 184 }
538 mmeineke 189 }
539    
540 mmeineke 184 #ifdef IS_MPI
541 mmeineke 189 }
542 mmeineke 184 #endif // is_mpi
543 mmeineke 189
544 mmeineke 10 // set the status, sample, and themal kick times
545 mmeineke 189
546 mmeineke 10 if( the_globals->haveSampleTime() ){
547 chuckv 124 simnfo->sampleTime = the_globals->getSampleTime();
548 mmeineke 10 simnfo->statusTime = simnfo->sampleTime;
549     simnfo->thermalTime = simnfo->sampleTime;
550     }
551     else{
552 chuckv 124 simnfo->sampleTime = the_globals->getRunTime();
553 mmeineke 10 simnfo->statusTime = simnfo->sampleTime;
554     simnfo->thermalTime = simnfo->sampleTime;
555     }
556    
557     if( the_globals->haveStatusTime() ){
558     simnfo->statusTime = the_globals->getStatusTime();
559     }
560    
561     if( the_globals->haveThermalTime() ){
562     simnfo->thermalTime = the_globals->getThermalTime();
563     }
564    
565     // check for the temperature set flag
566    
567     if( the_globals->haveTempSet() ) simnfo->setTemp = the_globals->getTempSet();
568 chuckv 124
569    
570 chuckv 248 // // make the longe range forces and the integrator
571 chuckv 124
572 chuckv 248 // new AllLong( simnfo );
573 chuckv 124
574 chuckv 249 if( !strcmp( force_field, "TraPPE" ) ) new Verlet( *simnfo, the_ff );
575 mmeineke 10 if( !strcmp( force_field, "DipoleTest" ) ) new Symplectic( simnfo );
576     if( !strcmp( force_field, "TraPPE_Ex" ) ) new Symplectic( simnfo );
577 chuckv 248 if( !strcmp( force_field, "LJ" ) ) new Verlet( *simnfo, the_ff );
578    
579 mmeineke 10 }
580    
581     void SimSetup::makeAtoms( void ){
582 chuckv 124
583 mmeineke 10 int i, j, k, index;
584     double ux, uy, uz, uSqr, u;
585     AtomStamp* current_atom;
586     DirectionalAtom* dAtom;
587 mmeineke 205 int molIndex, molStart, molEnd, nMemb, lMolIndex;
588 mmeineke 10
589 mmeineke 205 lMolIndex = 0;
590 mmeineke 117 molIndex = 0;
591 mmeineke 10 index = 0;
592     for( i=0; i<n_components; i++ ){
593 chuckv 124
594 mmeineke 10 for( j=0; j<components_nmol[i]; j++ ){
595 chuckv 124
596 mmeineke 205 #ifdef IS_MPI
597 chuckv 215 if( mpiSim->getMyMolStart() <= molIndex &&
598     molIndex <= mpiSim->getMyMolEnd() ){
599 mmeineke 205 #endif // is_mpi
600 chuckv 124
601 mmeineke 205 molStart = index;
602     nMemb = comp_stamps[i]->getNAtoms();
603     for( k=0; k<comp_stamps[i]->getNAtoms(); k++ ){
604    
605     current_atom = comp_stamps[i]->getAtom( k );
606     if( current_atom->haveOrientation() ){
607    
608     dAtom = new DirectionalAtom(index);
609     simnfo->n_oriented++;
610     the_atoms[index] = dAtom;
611    
612     ux = current_atom->getOrntX();
613     uy = current_atom->getOrntY();
614     uz = current_atom->getOrntZ();
615    
616     uSqr = (ux * ux) + (uy * uy) + (uz * uz);
617    
618     u = sqrt( uSqr );
619     ux = ux / u;
620     uy = uy / u;
621     uz = uz / u;
622    
623     dAtom->setSUx( ux );
624     dAtom->setSUy( uy );
625     dAtom->setSUz( uz );
626     }
627     else{
628     the_atoms[index] = new GeneralAtom(index);
629     }
630     the_atoms[index]->setType( current_atom->getType() );
631     the_atoms[index]->setIndex( index );
632    
633     // increment the index and repeat;
634     index++;
635 mmeineke 10 }
636 mmeineke 205
637     molEnd = index -1;
638     the_molecules[lMolIndex].setNMembers( nMemb );
639     the_molecules[lMolIndex].setStartAtom( molStart );
640     the_molecules[lMolIndex].setEndAtom( molEnd );
641     the_molecules[lMolIndex].setStampID( i );
642     lMolIndex++;
643 chuckv 124
644 mmeineke 205 #ifdef IS_MPI
645 mmeineke 10 }
646 mmeineke 205 #endif //is_mpi
647    
648 mmeineke 117 molIndex++;
649 mmeineke 10 }
650     }
651 chuckv 124
652 chuckv 253 #ifdef IS_MPI
653     for( i=0; i<mpiSim->getMyNlocal(); i++ ) the_atoms[i]->setGlobalIndex( globalIndex[i] );
654    
655     delete[] globalIndex;
656 chuckv 254
657     mpiSim->mpiRefresh();
658     #endif //IS_MPI
659 chuckv 253
660 mmeineke 10 the_ff->initializeAtoms();
661     }
662    
663     void SimSetup::makeBonds( void ){
664    
665 mmeineke 205 int i, j, k, index, offset, molIndex;
666 mmeineke 10 bond_pair* the_bonds;
667     BondStamp* current_bond;
668    
669     the_bonds = new bond_pair[tot_bonds];
670     index = 0;
671     offset = 0;
672 chuckv 249 molIndex = 0;
673 chuckv 215
674 mmeineke 10 for( i=0; i<n_components; i++ ){
675 chuckv 124
676 mmeineke 10 for( j=0; j<components_nmol[i]; j++ ){
677 chuckv 124
678 mmeineke 205 #ifdef IS_MPI
679 chuckv 215 if( mpiSim->getMyMolStart() <= molIndex &&
680     molIndex <= mpiSim->getMyMolEnd() ){
681 mmeineke 205 #endif // is_mpi
682    
683     for( k=0; k<comp_stamps[i]->getNBonds(); k++ ){
684    
685     current_bond = comp_stamps[i]->getBond( k );
686     the_bonds[index].a = current_bond->getA() + offset;
687     the_bonds[index].b = current_bond->getB() + offset;
688    
689     the_excludes[index].i = the_bonds[index].a;
690     the_excludes[index].j = the_bonds[index].b;
691    
692     // increment the index and repeat;
693     index++;
694     }
695     offset += comp_stamps[i]->getNAtoms();
696    
697     #ifdef IS_MPI
698 mmeineke 10 }
699 chuckv 254 #endif //is_mpi
700 mmeineke 205
701     molIndex++;
702     }
703 mmeineke 10 }
704 chuckv 124
705 mmeineke 10 the_ff->initializeBonds( the_bonds );
706     }
707    
708     void SimSetup::makeBends( void ){
709    
710 mmeineke 205 int i, j, k, index, offset, molIndex;
711 mmeineke 10 bend_set* the_bends;
712     BendStamp* current_bend;
713    
714     the_bends = new bend_set[tot_bends];
715     index = 0;
716     offset = 0;
717 mmeineke 205 molIndex = 0;
718 mmeineke 10 for( i=0; i<n_components; i++ ){
719 chuckv 124
720 mmeineke 10 for( j=0; j<components_nmol[i]; j++ ){
721 chuckv 124
722 mmeineke 205 #ifdef IS_MPI
723 chuckv 215 if( mpiSim->getMyMolStart() <= molIndex &&
724     molIndex <= mpiSim->getMyMolEnd() ){
725 mmeineke 205 #endif // is_mpi
726 chuckv 124
727 mmeineke 205 for( k=0; k<comp_stamps[i]->getNBends(); k++ ){
728    
729     current_bend = comp_stamps[i]->getBend( k );
730     the_bends[index].a = current_bend->getA() + offset;
731     the_bends[index].b = current_bend->getB() + offset;
732     the_bends[index].c = current_bend->getC() + offset;
733    
734     the_excludes[index + tot_bonds].i = the_bends[index].a;
735     the_excludes[index + tot_bonds].j = the_bends[index].c;
736    
737     // increment the index and repeat;
738     index++;
739     }
740     offset += comp_stamps[i]->getNAtoms();
741    
742     #ifdef IS_MPI
743     }
744     #endif //is_mpi
745 mmeineke 10
746 mmeineke 205 molIndex++;
747 mmeineke 10 }
748     }
749 chuckv 124
750 mmeineke 10 the_ff->initializeBends( the_bends );
751     }
752    
753     void SimSetup::makeTorsions( void ){
754    
755 mmeineke 205 int i, j, k, index, offset, molIndex;
756 mmeineke 10 torsion_set* the_torsions;
757     TorsionStamp* current_torsion;
758    
759     the_torsions = new torsion_set[tot_torsions];
760     index = 0;
761     offset = 0;
762 mmeineke 205 molIndex = 0;
763 mmeineke 10 for( i=0; i<n_components; i++ ){
764 chuckv 124
765 mmeineke 10 for( j=0; j<components_nmol[i]; j++ ){
766 chuckv 124
767 mmeineke 205 #ifdef IS_MPI
768 chuckv 215 if( mpiSim->getMyMolStart() <= molIndex &&
769     molIndex <= mpiSim->getMyMolEnd() ){
770 mmeineke 205 #endif // is_mpi
771    
772 mmeineke 10 for( k=0; k<comp_stamps[i]->getNTorsions(); k++ ){
773 chuckv 124
774 mmeineke 10 current_torsion = comp_stamps[i]->getTorsion( k );
775     the_torsions[index].a = current_torsion->getA() + offset;
776     the_torsions[index].b = current_torsion->getB() + offset;
777     the_torsions[index].c = current_torsion->getC() + offset;
778     the_torsions[index].d = current_torsion->getD() + offset;
779    
780     the_excludes[index + tot_bonds + tot_bends].i = the_torsions[index].a;
781     the_excludes[index + tot_bonds + tot_bends].j = the_torsions[index].d;
782    
783     // increment the index and repeat;
784     index++;
785     }
786     offset += comp_stamps[i]->getNAtoms();
787 mmeineke 205
788     #ifdef IS_MPI
789     }
790     #endif //is_mpi
791    
792     molIndex++;
793 mmeineke 10 }
794     }
795 chuckv 124
796 mmeineke 10 the_ff->initializeTorsions( the_torsions );
797     }
798    
799     void SimSetup::initFromBass( void ){
800    
801     int i, j, k;
802     int n_cells;
803     double cellx, celly, cellz;
804     double temp1, temp2, temp3;
805     int n_per_extra;
806     int n_extra;
807     int have_extra, done;
808    
809     temp1 = (double)tot_nmol / 4.0;
810     temp2 = pow( temp1, ( 1.0 / 3.0 ) );
811     temp3 = ceil( temp2 );
812    
813     have_extra =0;
814     if( temp2 < temp3 ){ // we have a non-complete lattice
815     have_extra =1;
816    
817     n_cells = (int)temp3 - 1;
818     cellx = simnfo->box_x / temp3;
819     celly = simnfo->box_y / temp3;
820     cellz = simnfo->box_z / temp3;
821     n_extra = tot_nmol - ( 4 * n_cells * n_cells * n_cells );
822     temp1 = ((double)n_extra) / ( pow( temp3, 3.0 ) - pow( n_cells, 3.0 ) );
823     n_per_extra = (int)ceil( temp1 );
824    
825     if( n_per_extra > 4){
826 mmeineke 189 sprintf( painCave.errMsg,
827     "SimSetup error. There has been an error in constructing"
828     " the non-complete lattice.\n" );
829     painCave.isFatal = 1;
830     simError();
831 mmeineke 10 }
832     }
833     else{
834     n_cells = (int)temp3;
835     cellx = simnfo->box_x / temp3;
836     celly = simnfo->box_y / temp3;
837     cellz = simnfo->box_z / temp3;
838     }
839 chuckv 124
840 mmeineke 10 current_mol = 0;
841     current_comp_mol = 0;
842     current_comp = 0;
843     current_atom_ndx = 0;
844 chuckv 124
845 mmeineke 10 for( i=0; i < n_cells ; i++ ){
846     for( j=0; j < n_cells; j++ ){
847     for( k=0; k < n_cells; k++ ){
848 chuckv 124
849 mmeineke 10 makeElement( i * cellx,
850     j * celly,
851     k * cellz );
852 chuckv 124
853 mmeineke 10 makeElement( i * cellx + 0.5 * cellx,
854     j * celly + 0.5 * celly,
855     k * cellz );
856 chuckv 124
857 mmeineke 10 makeElement( i * cellx,
858     j * celly + 0.5 * celly,
859     k * cellz + 0.5 * cellz );
860 chuckv 124
861 mmeineke 10 makeElement( i * cellx + 0.5 * cellx,
862     j * celly,
863     k * cellz + 0.5 * cellz );
864     }
865     }
866     }
867    
868     if( have_extra ){
869     done = 0;
870 chuckv 124
871 mmeineke 10 int start_ndx;
872     for( i=0; i < (n_cells+1) && !done; i++ ){
873     for( j=0; j < (n_cells+1) && !done; j++ ){
874 chuckv 124
875 mmeineke 10 if( i < n_cells ){
876 chuckv 124
877 mmeineke 10 if( j < n_cells ){
878     start_ndx = n_cells;
879     }
880     else start_ndx = 0;
881     }
882     else start_ndx = 0;
883 chuckv 124
884 mmeineke 10 for( k=start_ndx; k < (n_cells+1) && !done; k++ ){
885 chuckv 124
886 mmeineke 10 makeElement( i * cellx,
887     j * celly,
888     k * cellz );
889     done = ( current_mol >= tot_nmol );
890 chuckv 124
891 mmeineke 10 if( !done && n_per_extra > 1 ){
892     makeElement( i * cellx + 0.5 * cellx,
893     j * celly + 0.5 * celly,
894     k * cellz );
895     done = ( current_mol >= tot_nmol );
896     }
897 chuckv 124
898 mmeineke 10 if( !done && n_per_extra > 2){
899     makeElement( i * cellx,
900     j * celly + 0.5 * celly,
901     k * cellz + 0.5 * cellz );
902     done = ( current_mol >= tot_nmol );
903     }
904 chuckv 124
905 mmeineke 10 if( !done && n_per_extra > 3){
906     makeElement( i * cellx + 0.5 * cellx,
907     j * celly,
908     k * cellz + 0.5 * cellz );
909     done = ( current_mol >= tot_nmol );
910     }
911     }
912     }
913     }
914     }
915 chuckv 124
916    
917 mmeineke 10 for( i=0; i<simnfo->n_atoms; i++ ){
918     simnfo->atoms[i]->set_vx( 0.0 );
919     simnfo->atoms[i]->set_vy( 0.0 );
920     simnfo->atoms[i]->set_vz( 0.0 );
921     }
922     }
923    
924     void SimSetup::makeElement( double x, double y, double z ){
925    
926     int k;
927     AtomStamp* current_atom;
928     DirectionalAtom* dAtom;
929     double rotMat[3][3];
930    
931     for( k=0; k<comp_stamps[current_comp]->getNAtoms(); k++ ){
932 chuckv 124
933 mmeineke 10 current_atom = comp_stamps[current_comp]->getAtom( k );
934     if( !current_atom->havePosition() ){
935 mmeineke 189 sprintf( painCave.errMsg,
936     "SimSetup:initFromBass error.\n"
937     "\tComponent %s, atom %s does not have a position specified.\n"
938     "\tThe initialization routine is unable to give a start"
939     " position.\n",
940     comp_stamps[current_comp]->getID(),
941     current_atom->getType() );
942     painCave.isFatal = 1;
943     simError();
944 mmeineke 10 }
945 chuckv 124
946 mmeineke 10 the_atoms[current_atom_ndx]->setX( x + current_atom->getPosX() );
947     the_atoms[current_atom_ndx]->setY( y + current_atom->getPosY() );
948     the_atoms[current_atom_ndx]->setZ( z + current_atom->getPosZ() );
949 chuckv 124
950 mmeineke 10 if( the_atoms[current_atom_ndx]->isDirectional() ){
951 chuckv 124
952 mmeineke 10 dAtom = (DirectionalAtom *)the_atoms[current_atom_ndx];
953 chuckv 124
954 mmeineke 10 rotMat[0][0] = 1.0;
955     rotMat[0][1] = 0.0;
956     rotMat[0][2] = 0.0;
957    
958     rotMat[1][0] = 0.0;
959     rotMat[1][1] = 1.0;
960     rotMat[1][2] = 0.0;
961    
962     rotMat[2][0] = 0.0;
963     rotMat[2][1] = 0.0;
964     rotMat[2][2] = 1.0;
965    
966     dAtom->setA( rotMat );
967     }
968    
969     current_atom_ndx++;
970     }
971 chuckv 124
972 mmeineke 10 current_mol++;
973     current_comp_mol++;
974    
975     if( current_comp_mol >= components_nmol[current_comp] ){
976 chuckv 124
977 mmeineke 10 current_comp_mol = 0;
978     current_comp++;
979     }
980     }