ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE/libmdtools/SimSetup.cpp
Revision: 378
Committed: Fri Mar 21 17:42:12 2003 UTC (21 years, 3 months ago) by mmeineke
File size: 26871 byte(s)
Log Message:
This commit was generated by cvs2svn to compensate for changes in r377,
which included commits to RCS files with non-trunk default branches.

File Contents

# User Rev Content
1 mmeineke 377 #include <cstdlib>
2     #include <iostream>
3     #include <cmath>
4    
5     #include "SimSetup.hpp"
6     #include "parse_me.h"
7     #include "Integrator.hpp"
8     #include "simError.h"
9    
10     #ifdef IS_MPI
11     #include "mpiBASS.h"
12     #include "mpiSimulation.hpp"
13     #endif
14    
15     SimSetup::SimSetup(){
16     stamps = new MakeStamps();
17     globals = new Globals();
18    
19     #ifdef IS_MPI
20     strcpy( checkPointMsg, "SimSetup creation successful" );
21     MPIcheckPoint();
22     #endif // IS_MPI
23     }
24    
25     SimSetup::~SimSetup(){
26     delete stamps;
27     delete globals;
28     }
29    
30     void SimSetup::parseFile( char* fileName ){
31    
32     #ifdef IS_MPI
33     if( worldRank == 0 ){
34     #endif // is_mpi
35    
36     inFileName = fileName;
37     set_interface_stamps( stamps, globals );
38    
39     #ifdef IS_MPI
40     mpiEventInit();
41     #endif
42    
43     yacc_BASS( fileName );
44    
45     #ifdef IS_MPI
46     throwMPIEvent(NULL);
47     }
48     else receiveParse();
49     #endif
50    
51     }
52    
53     #ifdef IS_MPI
54     void SimSetup::receiveParse(void){
55    
56     set_interface_stamps( stamps, globals );
57     mpiEventInit();
58     MPIcheckPoint();
59     mpiEventLoop();
60    
61     }
62    
63     #endif // is_mpi
64    
65     void SimSetup::createSim( void ){
66    
67     MakeStamps *the_stamps;
68     Globals* the_globals;
69     int i, j;
70    
71     // get the stamps and globals;
72     the_stamps = stamps;
73     the_globals = globals;
74    
75     // set the easy ones first
76     simnfo->target_temp = the_globals->getTargetTemp();
77     simnfo->dt = the_globals->getDt();
78     simnfo->run_time = the_globals->getRunTime();
79    
80     // get the ones we know are there, yet still may need some work.
81     n_components = the_globals->getNComponents();
82     strcpy( force_field, the_globals->getForceField() );
83     strcpy( ensemble, the_globals->getEnsemble() );
84     strcpy( simnfo->ensemble, ensemble );
85    
86     strcpy( simnfo->mixingRule, the_globals->getMixingRule() );
87     simnfo->usePBC = the_globals->getPBC();
88    
89    
90    
91     if( !strcmp( force_field, "TraPPE" ) ) the_ff = new TraPPEFF();
92     else if( !strcmp( force_field, "DipoleTest" ) ) the_ff = new DipoleTestFF();
93     else if( !strcmp( force_field, "TraPPE_Ex" ) ) the_ff = new TraPPE_ExFF();
94     else if( !strcmp( force_field, "LJ" ) ) the_ff = new LJ_FF();
95     else{
96     sprintf( painCave.errMsg,
97     "SimSetup Error. Unrecognized force field -> %s\n",
98     force_field );
99     painCave.isFatal = 1;
100     simError();
101     }
102    
103     #ifdef IS_MPI
104     strcpy( checkPointMsg, "ForceField creation successful" );
105     MPIcheckPoint();
106     #endif // is_mpi
107    
108    
109    
110     // 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    
115     if( !the_globals->haveNMol() ){
116     // we don't have the total number of molecules, so we assume it is
117     // given in each component
118    
119     tot_nmol = 0;
120     for( i=0; i<n_components; i++ ){
121    
122     if( !the_components[i]->haveNMol() ){
123     // we have a problem
124     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     }
130    
131     tot_nmol += the_components[i]->getNMol();
132     components_nmol[i] = the_components[i]->getNMol();
133     }
134     }
135     else{
136     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    
160     #ifdef IS_MPI
161     strcpy( checkPointMsg, "Have the number of components" );
162     MPIcheckPoint();
163     #endif // is_mpi
164    
165     // make an array of molecule stamps that match the components used.
166     // also extract the used stamps out into a separate linked list
167    
168     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     for( i=0; i<n_components; i++ ){
177    
178     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     "list of declared molecules\n",
193     id );
194     painCave.isFatal = 1;
195     simError();
196     }
197    
198     headStamp->add( currentStamp );
199     comp_stamps[i] = headStamp->match( id );
200     }
201     }
202    
203     #ifdef IS_MPI
204     strcpy( checkPointMsg, "Component stamps successfully extracted\n" );
205     MPIcheckPoint();
206     #endif // is_mpi
207    
208    
209    
210    
211     // 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    
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     tot_torsions += components_nmol[i] * comp_stamps[i]->getNTorsions();
223     }
224    
225     tot_SRI = tot_bonds + tot_bends + tot_torsions;
226    
227     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     simnfo->n_mol = tot_nmol;
233    
234    
235     #ifdef IS_MPI
236    
237     // divide the molecules among processors here.
238    
239     mpiSim = new mpiSimulation( simnfo );
240    
241    
242    
243     globalIndex = mpiSim->divideLabor();
244    
245    
246    
247     // set up the local variables
248    
249     int localMol, allMol;
250     int local_atoms, local_bonds, local_bends, local_torsions, local_SRI;
251    
252     allMol = 0;
253     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     if( mpiSim->getMyMolStart() <= allMol &&
263     allMol <= mpiSim->getMyMolEnd() ){
264    
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     allMol++;
272     }
273     }
274     local_SRI = local_bonds + local_bends + local_torsions;
275    
276    
277     simnfo->n_atoms = mpiSim->getMyNlocal();
278    
279     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    
289     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     #endif // is_mpi
300    
301    
302     // create the atom and short range interaction arrays
303    
304     Atom::createArrays(simnfo->n_atoms);
305     the_atoms = new Atom*[simnfo->n_atoms];
306     the_molecules = new Molecule[simnfo->n_mol];
307    
308    
309     if( simnfo->n_SRI ){
310     the_sris = new SRI*[simnfo->n_SRI];
311     the_excludes = new int[2 * simnfo->n_SRI];
312     simnfo->globalExcludes = new int;
313     simnfo->n_exclude = tot_SRI;
314     }
315     else{
316    
317     the_excludes = new int[2];
318     the_excludes[0] = 0;
319     the_excludes[1] = 0;
320     simnfo->globalExcludes = new int;
321     simnfo->globalExcludes[0] = 0;
322    
323     simnfo->n_exclude = 1;
324     }
325    
326     // set the arrays into the SimInfo object
327    
328     simnfo->atoms = the_atoms;
329     simnfo->sr_interactions = the_sris;
330     simnfo->nGlobalExcludes = 0;
331     simnfo->excludes = the_excludes;
332    
333    
334     // get some of the tricky things that may still be in the globals
335    
336     if( simnfo->n_dipoles ){
337    
338     if( !the_globals->haveRRF() ){
339     sprintf( painCave.errMsg,
340     "SimSetup Error, system has dipoles, but no rRF was set.\n");
341     painCave.isFatal = 1;
342     simError();
343     }
344     if( !the_globals->haveDielectric() ){
345     sprintf( painCave.errMsg,
346     "SimSetup Error, system has dipoles, but no"
347     " dielectric was set.\n" );
348     painCave.isFatal = 1;
349     simError();
350     }
351    
352     simnfo->rRF = the_globals->getRRF();
353     simnfo->dielectric = the_globals->getDielectric();
354     }
355    
356     #ifdef IS_MPI
357     strcpy( checkPointMsg, "rRf and dielectric check out" );
358     MPIcheckPoint();
359     #endif // is_mpi
360    
361     if( the_globals->haveBox() ){
362     simnfo->box_x = the_globals->getBox();
363     simnfo->box_y = the_globals->getBox();
364     simnfo->box_z = the_globals->getBox();
365     }
366     else if( the_globals->haveDensity() ){
367    
368     double vol;
369     vol = (double)tot_nmol / the_globals->getDensity();
370     simnfo->box_x = pow( vol, ( 1.0 / 3.0 ) );
371     simnfo->box_y = simnfo->box_x;
372     simnfo->box_z = simnfo->box_x;
373     }
374     else{
375     if( !the_globals->haveBoxX() ){
376     sprintf( painCave.errMsg,
377     "SimSetup error, no periodic BoxX size given.\n" );
378     painCave.isFatal = 1;
379     simError();
380     }
381     simnfo->box_x = the_globals->getBoxX();
382    
383     if( !the_globals->haveBoxY() ){
384     sprintf( painCave.errMsg,
385     "SimSetup error, no periodic BoxY size given.\n" );
386     painCave.isFatal = 1;
387     simError();
388     }
389     simnfo->box_y = the_globals->getBoxY();
390    
391     if( !the_globals->haveBoxZ() ){
392     sprintf( painCave.errMsg,
393     "SimSetup error, no periodic BoxZ size given.\n" );
394     painCave.isFatal = 1;
395     simError();
396     }
397     simnfo->box_z = the_globals->getBoxZ();
398     }
399    
400     #ifdef IS_MPI
401     strcpy( checkPointMsg, "Box size set up" );
402     MPIcheckPoint();
403     #endif // is_mpi
404    
405    
406     // initialize the arrays
407    
408     the_ff->setSimInfo( simnfo );
409    
410     makeAtoms();
411     simnfo->identArray = new int[simnfo->n_atoms];
412     for(i=0; i<simnfo->n_atoms; i++){
413     simnfo->identArray[i] = the_atoms[i]->getIdent();
414     }
415    
416     if( tot_bonds ){
417     makeBonds();
418     }
419    
420     if( tot_bends ){
421     makeBends();
422     }
423    
424     if( tot_torsions ){
425     makeTorsions();
426     }
427    
428    
429    
430    
431    
432    
433     if( the_globals->haveInitialConfig() ){
434    
435     InitializeFromFile* fileInit;
436     #ifdef IS_MPI // is_mpi
437     if( worldRank == 0 ){
438     #endif //is_mpi
439     fileInit = new InitializeFromFile( the_globals->getInitialConfig() );
440     #ifdef IS_MPI
441     }else fileInit = new InitializeFromFile( NULL );
442     #endif
443     fileInit->read_xyz( simnfo ); // default velocities on
444    
445     delete fileInit;
446     }
447     else{
448    
449     #ifdef IS_MPI
450    
451     // no init from bass
452    
453     sprintf( painCave.errMsg,
454     "Cannot intialize a parallel simulation without an initial configuration file.\n" );
455     painCave.isFatal;
456     simError();
457    
458     #else
459    
460     initFromBass();
461    
462    
463     #endif
464     }
465    
466     #ifdef IS_MPI
467     strcpy( checkPointMsg, "Successfully read in the initial configuration" );
468     MPIcheckPoint();
469     #endif // is_mpi
470    
471    
472    
473    
474    
475    
476    
477     #ifdef IS_MPI
478     if( worldRank == 0 ){
479     #endif // is_mpi
480    
481     if( the_globals->haveFinalConfig() ){
482     strcpy( simnfo->finalName, the_globals->getFinalConfig() );
483     }
484     else{
485     strcpy( simnfo->finalName, inFileName );
486     char* endTest;
487     int nameLength = strlen( simnfo->finalName );
488     endTest = &(simnfo->finalName[nameLength - 5]);
489     if( !strcmp( endTest, ".bass" ) ){
490     strcpy( endTest, ".eor" );
491     }
492     else if( !strcmp( endTest, ".BASS" ) ){
493     strcpy( endTest, ".eor" );
494     }
495     else{
496     endTest = &(simnfo->finalName[nameLength - 4]);
497     if( !strcmp( endTest, ".bss" ) ){
498     strcpy( endTest, ".eor" );
499     }
500     else if( !strcmp( endTest, ".mdl" ) ){
501     strcpy( endTest, ".eor" );
502     }
503     else{
504     strcat( simnfo->finalName, ".eor" );
505     }
506     }
507     }
508    
509     // make the sample and status out names
510    
511     strcpy( simnfo->sampleName, inFileName );
512     char* endTest;
513     int nameLength = strlen( simnfo->sampleName );
514     endTest = &(simnfo->sampleName[nameLength - 5]);
515     if( !strcmp( endTest, ".bass" ) ){
516     strcpy( endTest, ".dump" );
517     }
518     else if( !strcmp( endTest, ".BASS" ) ){
519     strcpy( endTest, ".dump" );
520     }
521     else{
522     endTest = &(simnfo->sampleName[nameLength - 4]);
523     if( !strcmp( endTest, ".bss" ) ){
524     strcpy( endTest, ".dump" );
525     }
526     else if( !strcmp( endTest, ".mdl" ) ){
527     strcpy( endTest, ".dump" );
528     }
529     else{
530     strcat( simnfo->sampleName, ".dump" );
531     }
532     }
533    
534     strcpy( simnfo->statusName, inFileName );
535     nameLength = strlen( simnfo->statusName );
536     endTest = &(simnfo->statusName[nameLength - 5]);
537     if( !strcmp( endTest, ".bass" ) ){
538     strcpy( endTest, ".stat" );
539     }
540     else if( !strcmp( endTest, ".BASS" ) ){
541     strcpy( endTest, ".stat" );
542     }
543     else{
544     endTest = &(simnfo->statusName[nameLength - 4]);
545     if( !strcmp( endTest, ".bss" ) ){
546     strcpy( endTest, ".stat" );
547     }
548     else if( !strcmp( endTest, ".mdl" ) ){
549     strcpy( endTest, ".stat" );
550     }
551     else{
552     strcat( simnfo->statusName, ".stat" );
553     }
554     }
555    
556     #ifdef IS_MPI
557     }
558     #endif // is_mpi
559    
560     // set the status, sample, and themal kick times
561    
562     if( the_globals->haveSampleTime() ){
563     simnfo->sampleTime = the_globals->getSampleTime();
564     simnfo->statusTime = simnfo->sampleTime;
565     simnfo->thermalTime = simnfo->sampleTime;
566     }
567     else{
568     simnfo->sampleTime = the_globals->getRunTime();
569     simnfo->statusTime = simnfo->sampleTime;
570     simnfo->thermalTime = simnfo->sampleTime;
571     }
572    
573     if( the_globals->haveStatusTime() ){
574     simnfo->statusTime = the_globals->getStatusTime();
575     }
576    
577     if( the_globals->haveThermalTime() ){
578     simnfo->thermalTime = the_globals->getThermalTime();
579     }
580    
581     // check for the temperature set flag
582    
583     if( the_globals->haveTempSet() ) simnfo->setTemp = the_globals->getTempSet();
584    
585    
586     // // make the longe range forces and the integrator
587    
588     // new AllLong( simnfo );
589    
590     if( !strcmp( force_field, "TraPPE" ) ) new Verlet( *simnfo, the_ff );
591     if( !strcmp( force_field, "DipoleTest" ) ) new Symplectic( simnfo, the_ff );
592     if( !strcmp( force_field, "TraPPE_Ex" ) ) new Symplectic( simnfo, the_ff );
593     if( !strcmp( force_field, "LJ" ) ) new Verlet( *simnfo, the_ff );
594    
595    
596    
597     // initialize the Fortran
598    
599     simnfo->refreshSim();
600    
601     if( !strcmp( simnfo->mixingRule, "standard") ){
602     the_ff->initForceField( LB_MIXING_RULE );
603     }
604     else if( !strcmp( simnfo->mixingRule, "explicit") ){
605     the_ff->initForceField( EXPLICIT_MIXING_RULE );
606     }
607     else{
608     sprintf( painCave.errMsg,
609     "SimSetup Error: unknown mixing rule -> \"%s\"\n",
610     simnfo->mixingRule );
611     painCave.isFatal = 1;
612     simError();
613     }
614    
615    
616     #ifdef IS_MPI
617     strcpy( checkPointMsg,
618     "Successfully intialized the mixingRule for Fortran." );
619     MPIcheckPoint();
620     #endif // is_mpi
621     }
622    
623     void SimSetup::makeAtoms( void ){
624    
625     int i, j, k, index;
626     double ux, uy, uz, uSqr, u;
627     AtomStamp* current_atom;
628    
629     DirectionalAtom* dAtom;
630     int molIndex, molStart, molEnd, nMemb, lMolIndex;
631    
632     lMolIndex = 0;
633     molIndex = 0;
634     index = 0;
635     for( i=0; i<n_components; i++ ){
636    
637     for( j=0; j<components_nmol[i]; j++ ){
638    
639     #ifdef IS_MPI
640     if( mpiSim->getMyMolStart() <= molIndex &&
641     molIndex <= mpiSim->getMyMolEnd() ){
642     #endif // is_mpi
643    
644     molStart = index;
645     nMemb = comp_stamps[i]->getNAtoms();
646     for( k=0; k<comp_stamps[i]->getNAtoms(); k++ ){
647    
648     current_atom = comp_stamps[i]->getAtom( k );
649     if( current_atom->haveOrientation() ){
650    
651     dAtom = new DirectionalAtom(index);
652     simnfo->n_oriented++;
653     the_atoms[index] = dAtom;
654    
655     ux = current_atom->getOrntX();
656     uy = current_atom->getOrntY();
657     uz = current_atom->getOrntZ();
658    
659     uSqr = (ux * ux) + (uy * uy) + (uz * uz);
660    
661     u = sqrt( uSqr );
662     ux = ux / u;
663     uy = uy / u;
664     uz = uz / u;
665    
666     dAtom->setSUx( ux );
667     dAtom->setSUy( uy );
668     dAtom->setSUz( uz );
669     }
670     else{
671     the_atoms[index] = new GeneralAtom(index);
672     }
673     the_atoms[index]->setType( current_atom->getType() );
674     the_atoms[index]->setIndex( index );
675    
676     // increment the index and repeat;
677     index++;
678     }
679    
680     molEnd = index -1;
681     the_molecules[lMolIndex].setNMembers( nMemb );
682     the_molecules[lMolIndex].setStartAtom( molStart );
683     the_molecules[lMolIndex].setEndAtom( molEnd );
684     the_molecules[lMolIndex].setStampID( i );
685     lMolIndex++;
686    
687     #ifdef IS_MPI
688     }
689     #endif //is_mpi
690    
691     molIndex++;
692     }
693     }
694    
695     #ifdef IS_MPI
696     for( i=0; i<mpiSim->getMyNlocal(); i++ ) the_atoms[i]->setGlobalIndex( globalIndex[i] );
697    
698     delete[] globalIndex;
699    
700     mpiSim->mpiRefresh();
701     #endif //IS_MPI
702    
703     the_ff->initializeAtoms();
704     }
705    
706     void SimSetup::makeBonds( void ){
707    
708     int i, j, k, index, offset, molIndex, exI, exJ, tempEx;
709     bond_pair* the_bonds;
710     BondStamp* current_bond;
711    
712     the_bonds = new bond_pair[tot_bonds];
713     index = 0;
714     offset = 0;
715     molIndex = 0;
716    
717     for( i=0; i<n_components; i++ ){
718    
719     for( j=0; j<components_nmol[i]; j++ ){
720    
721     #ifdef IS_MPI
722     if( mpiSim->getMyMolStart() <= molIndex &&
723     molIndex <= mpiSim->getMyMolEnd() ){
724     #endif // is_mpi
725    
726     for( k=0; k<comp_stamps[i]->getNBonds(); k++ ){
727    
728     current_bond = comp_stamps[i]->getBond( k );
729     the_bonds[index].a = current_bond->getA() + offset;
730     the_bonds[index].b = current_bond->getB() + offset;
731    
732     exI = the_bonds[index].a;
733     exJ = the_bonds[index].b;
734    
735     // exclude_I must always be the smaller of the pair
736     if( exI > exJ ){
737     tempEx = exI;
738     exI = exJ;
739     exJ = tempEx;
740     }
741    
742    
743     #ifdef IS_MPI
744    
745     the_excludes[index*2] =
746     the_atoms[exI]->getGlobalIndex() + 1;
747     the_excludes[index*2 + 1] =
748     the_atoms[exJ]->getGlobalIndex() + 1;
749    
750     #else // isn't MPI
751    
752     the_excludes[index*2] = exI + 1;
753     the_excludes[index*2 + 1] = exJ + 1;
754     // fortran index from 1 (hence the +1 in the indexing)
755     #endif //is_mpi
756    
757     // increment the index and repeat;
758     index++;
759     }
760     offset += comp_stamps[i]->getNAtoms();
761    
762     #ifdef IS_MPI
763     }
764     #endif //is_mpi
765    
766     molIndex++;
767     }
768     }
769    
770     the_ff->initializeBonds( the_bonds );
771     }
772    
773     void SimSetup::makeBends( void ){
774    
775     int i, j, k, index, offset, molIndex, exI, exJ, tempEx;
776     bend_set* the_bends;
777     BendStamp* current_bend;
778     LinkedAssign* extras;
779     LinkedAssign* current_extra;
780    
781    
782     the_bends = new bend_set[tot_bends];
783     index = 0;
784     offset = 0;
785     molIndex = 0;
786     for( i=0; i<n_components; i++ ){
787    
788     for( j=0; j<components_nmol[i]; j++ ){
789    
790     #ifdef IS_MPI
791     if( mpiSim->getMyMolStart() <= molIndex &&
792     molIndex <= mpiSim->getMyMolEnd() ){
793     #endif // is_mpi
794    
795     for( k=0; k<comp_stamps[i]->getNBends(); k++ ){
796    
797     current_bend = comp_stamps[i]->getBend( k );
798     the_bends[index].a = current_bend->getA() + offset;
799     the_bends[index].b = current_bend->getB() + offset;
800     the_bends[index].c = current_bend->getC() + offset;
801    
802     if( current_bend->haveExtras() ){
803    
804     extras = current_bend->getExtras();
805     current_extra = extras;
806    
807     while( current_extra != NULL ){
808     if( !strcmp( current_extra->getlhs(), "ghostVectorSource" )){
809    
810     switch( current_extra->getType() ){
811    
812     case 0:
813     the_bends[index].ghost =
814     current_extra->getInt() + offset;
815     the_bends[index].isGhost = 1;
816     break;
817    
818     case 1:
819     the_bends[index].ghost =
820     (int)current_extra->getDouble() + offset;
821     the_bends[index].isGhost = 1;
822     break;
823    
824     default:
825     sprintf( painCave.errMsg,
826     "SimSetup Error: ghostVectorSource was neiter a "
827     "double nor an int.\n"
828     "-->Bend[%d] in %s\n",
829     k, comp_stamps[i]->getID() );
830     painCave.isFatal = 1;
831     simError();
832     }
833     }
834    
835     else{
836    
837     sprintf( painCave.errMsg,
838     "SimSetup Error: unhandled bend assignment:\n"
839     " -->%s in Bend[%d] in %s\n",
840     current_extra->getlhs(),
841     k, comp_stamps[i]->getID() );
842     painCave.isFatal = 1;
843     simError();
844     }
845    
846     current_extra = current_extra->getNext();
847     }
848     }
849    
850     if( !the_bends[index].isGhost ){
851    
852     exI = the_bends[index].a;
853     exJ = the_bends[index].c;
854     }
855     else{
856    
857     exI = the_bends[index].a;
858     exJ = the_bends[index].b;
859     }
860    
861     // exclude_I must always be the smaller of the pair
862     if( exI > exJ ){
863     tempEx = exI;
864     exI = exJ;
865     exJ = tempEx;
866     }
867    
868    
869     #ifdef IS_MPI
870    
871     the_excludes[(index + tot_bonds)*2] =
872     the_atoms[exI]->getGlobalIndex() + 1;
873     the_excludes[(index + tot_bonds)*2 + 1] =
874     the_atoms[exJ]->getGlobalIndex() + 1;
875    
876     #else // isn't MPI
877    
878     the_excludes[(index + tot_bonds)*2] = exI + 1;
879     the_excludes[(index + tot_bonds)*2 + 1] = exJ + 1;
880     // fortran index from 1 (hence the +1 in the indexing)
881     #endif //is_mpi
882    
883    
884     // increment the index and repeat;
885     index++;
886     }
887     offset += comp_stamps[i]->getNAtoms();
888    
889     #ifdef IS_MPI
890     }
891     #endif //is_mpi
892    
893     molIndex++;
894     }
895     }
896    
897     #ifdef IS_MPI
898     sprintf( checkPointMsg,
899     "Successfully created the bends list.\n" );
900     MPIcheckPoint();
901     #endif // is_mpi
902    
903    
904     the_ff->initializeBends( the_bends );
905     }
906    
907     void SimSetup::makeTorsions( void ){
908    
909     int i, j, k, index, offset, molIndex, exI, exJ, tempEx;
910     torsion_set* the_torsions;
911     TorsionStamp* current_torsion;
912    
913     the_torsions = new torsion_set[tot_torsions];
914     index = 0;
915     offset = 0;
916     molIndex = 0;
917     for( i=0; i<n_components; i++ ){
918    
919     for( j=0; j<components_nmol[i]; j++ ){
920    
921     #ifdef IS_MPI
922     if( mpiSim->getMyMolStart() <= molIndex &&
923     molIndex <= mpiSim->getMyMolEnd() ){
924     #endif // is_mpi
925    
926     for( k=0; k<comp_stamps[i]->getNTorsions(); k++ ){
927    
928     current_torsion = comp_stamps[i]->getTorsion( k );
929     the_torsions[index].a = current_torsion->getA() + offset;
930     the_torsions[index].b = current_torsion->getB() + offset;
931     the_torsions[index].c = current_torsion->getC() + offset;
932     the_torsions[index].d = current_torsion->getD() + offset;
933    
934     exI = the_torsions[index].a;
935     exJ = the_torsions[index].d;
936    
937    
938     // exclude_I must always be the smaller of the pair
939     if( exI > exJ ){
940     tempEx = exI;
941     exI = exJ;
942     exJ = tempEx;
943     }
944    
945    
946     #ifdef IS_MPI
947    
948     the_excludes[(index + tot_bonds + tot_bends)*2] =
949     the_atoms[exI]->getGlobalIndex() + 1;
950     the_excludes[(index + tot_bonds + tot_bends)*2 + 1] =
951     the_atoms[exJ]->getGlobalIndex() + 1;
952    
953     #else // isn't MPI
954    
955     the_excludes[(index + tot_bonds + tot_bends)*2] = exI + 1;
956     the_excludes[(index + tot_bonds + tot_bends)*2 + 1] = exJ + 1;
957     // fortran indexes from 1 (hence the +1 in the indexing)
958     #endif //is_mpi
959    
960    
961     // increment the index and repeat;
962     index++;
963     }
964     offset += comp_stamps[i]->getNAtoms();
965    
966     #ifdef IS_MPI
967     }
968     #endif //is_mpi
969    
970     molIndex++;
971     }
972     }
973    
974     the_ff->initializeTorsions( the_torsions );
975     }
976    
977     void SimSetup::initFromBass( void ){
978    
979     int i, j, k;
980     int n_cells;
981     double cellx, celly, cellz;
982     double temp1, temp2, temp3;
983     int n_per_extra;
984     int n_extra;
985     int have_extra, done;
986    
987     temp1 = (double)tot_nmol / 4.0;
988     temp2 = pow( temp1, ( 1.0 / 3.0 ) );
989     temp3 = ceil( temp2 );
990    
991     have_extra =0;
992     if( temp2 < temp3 ){ // we have a non-complete lattice
993     have_extra =1;
994    
995     n_cells = (int)temp3 - 1;
996     cellx = simnfo->box_x / temp3;
997     celly = simnfo->box_y / temp3;
998     cellz = simnfo->box_z / temp3;
999     n_extra = tot_nmol - ( 4 * n_cells * n_cells * n_cells );
1000     temp1 = ((double)n_extra) / ( pow( temp3, 3.0 ) - pow( n_cells, 3.0 ) );
1001     n_per_extra = (int)ceil( temp1 );
1002    
1003     if( n_per_extra > 4){
1004     sprintf( painCave.errMsg,
1005     "SimSetup error. There has been an error in constructing"
1006     " the non-complete lattice.\n" );
1007     painCave.isFatal = 1;
1008     simError();
1009     }
1010     }
1011     else{
1012     n_cells = (int)temp3;
1013     cellx = simnfo->box_x / temp3;
1014     celly = simnfo->box_y / temp3;
1015     cellz = simnfo->box_z / temp3;
1016     }
1017    
1018     current_mol = 0;
1019     current_comp_mol = 0;
1020     current_comp = 0;
1021     current_atom_ndx = 0;
1022    
1023     for( i=0; i < n_cells ; i++ ){
1024     for( j=0; j < n_cells; j++ ){
1025     for( k=0; k < n_cells; k++ ){
1026    
1027     makeElement( i * cellx,
1028     j * celly,
1029     k * cellz );
1030    
1031     makeElement( i * cellx + 0.5 * cellx,
1032     j * celly + 0.5 * celly,
1033     k * cellz );
1034    
1035     makeElement( i * cellx,
1036     j * celly + 0.5 * celly,
1037     k * cellz + 0.5 * cellz );
1038    
1039     makeElement( i * cellx + 0.5 * cellx,
1040     j * celly,
1041     k * cellz + 0.5 * cellz );
1042     }
1043     }
1044     }
1045    
1046     if( have_extra ){
1047     done = 0;
1048    
1049     int start_ndx;
1050     for( i=0; i < (n_cells+1) && !done; i++ ){
1051     for( j=0; j < (n_cells+1) && !done; j++ ){
1052    
1053     if( i < n_cells ){
1054    
1055     if( j < n_cells ){
1056     start_ndx = n_cells;
1057     }
1058     else start_ndx = 0;
1059     }
1060     else start_ndx = 0;
1061    
1062     for( k=start_ndx; k < (n_cells+1) && !done; k++ ){
1063    
1064     makeElement( i * cellx,
1065     j * celly,
1066     k * cellz );
1067     done = ( current_mol >= tot_nmol );
1068    
1069     if( !done && n_per_extra > 1 ){
1070     makeElement( i * cellx + 0.5 * cellx,
1071     j * celly + 0.5 * celly,
1072     k * cellz );
1073     done = ( current_mol >= tot_nmol );
1074     }
1075    
1076     if( !done && n_per_extra > 2){
1077     makeElement( i * cellx,
1078     j * celly + 0.5 * celly,
1079     k * cellz + 0.5 * cellz );
1080     done = ( current_mol >= tot_nmol );
1081     }
1082    
1083     if( !done && n_per_extra > 3){
1084     makeElement( i * cellx + 0.5 * cellx,
1085     j * celly,
1086     k * cellz + 0.5 * cellz );
1087     done = ( current_mol >= tot_nmol );
1088     }
1089     }
1090     }
1091     }
1092     }
1093    
1094    
1095     for( i=0; i<simnfo->n_atoms; i++ ){
1096     simnfo->atoms[i]->set_vx( 0.0 );
1097     simnfo->atoms[i]->set_vy( 0.0 );
1098     simnfo->atoms[i]->set_vz( 0.0 );
1099     }
1100     }
1101    
1102     void SimSetup::makeElement( double x, double y, double z ){
1103    
1104     int k;
1105     AtomStamp* current_atom;
1106     DirectionalAtom* dAtom;
1107     double rotMat[3][3];
1108    
1109     for( k=0; k<comp_stamps[current_comp]->getNAtoms(); k++ ){
1110    
1111     current_atom = comp_stamps[current_comp]->getAtom( k );
1112     if( !current_atom->havePosition() ){
1113     sprintf( painCave.errMsg,
1114     "SimSetup:initFromBass error.\n"
1115     "\tComponent %s, atom %s does not have a position specified.\n"
1116     "\tThe initialization routine is unable to give a start"
1117     " position.\n",
1118     comp_stamps[current_comp]->getID(),
1119     current_atom->getType() );
1120     painCave.isFatal = 1;
1121     simError();
1122     }
1123    
1124     the_atoms[current_atom_ndx]->setX( x + current_atom->getPosX() );
1125     the_atoms[current_atom_ndx]->setY( y + current_atom->getPosY() );
1126     the_atoms[current_atom_ndx]->setZ( z + current_atom->getPosZ() );
1127    
1128     if( the_atoms[current_atom_ndx]->isDirectional() ){
1129    
1130     dAtom = (DirectionalAtom *)the_atoms[current_atom_ndx];
1131    
1132     rotMat[0][0] = 1.0;
1133     rotMat[0][1] = 0.0;
1134     rotMat[0][2] = 0.0;
1135    
1136     rotMat[1][0] = 0.0;
1137     rotMat[1][1] = 1.0;
1138     rotMat[1][2] = 0.0;
1139    
1140     rotMat[2][0] = 0.0;
1141     rotMat[2][1] = 0.0;
1142     rotMat[2][2] = 1.0;
1143    
1144     dAtom->setA( rotMat );
1145     }
1146    
1147     current_atom_ndx++;
1148     }
1149    
1150     current_mol++;
1151     current_comp_mol++;
1152    
1153     if( current_comp_mol >= components_nmol[current_comp] ){
1154    
1155     current_comp_mol = 0;
1156     current_comp++;
1157     }
1158     }