ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/mdtools/interface_implementation/SimSetup.cpp
Revision: 184
Committed: Thu Nov 21 20:33:06 2002 UTC (21 years, 7 months ago) by mmeineke
File size: 17610 byte(s)
Log Message:
*** empty log message ***

File Contents

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