1 |
#include <cstdlib> |
2 |
#include <cstdio> |
3 |
#include <cstring> |
4 |
|
5 |
#include <iostream> |
6 |
using namespace std; |
7 |
|
8 |
#ifdef IS_MPI |
9 |
#include <mpi.h> |
10 |
#include <mpi++.h> |
11 |
#endif //is_mpi |
12 |
|
13 |
#include "ForceFields.hpp" |
14 |
#include "SRI.hpp" |
15 |
#include "simError.h" |
16 |
|
17 |
#include <fortranWrappers.hpp> |
18 |
|
19 |
#ifdef IS_MPI |
20 |
#include "mpiForceField.h" |
21 |
#endif // is_mpi |
22 |
|
23 |
|
24 |
|
25 |
namespace { |
26 |
|
27 |
// Declare the structures that will be passed by the parser and MPI |
28 |
|
29 |
typedef struct{ |
30 |
char name[15]; |
31 |
double mass; |
32 |
double epslon; |
33 |
double sigma; |
34 |
int ident; |
35 |
int last; // 0 -> default |
36 |
// 1 -> in MPI: tells nodes to stop listening |
37 |
} atomStruct; |
38 |
|
39 |
int parseAtom( char *lineBuffer, int lineNum, atomStruct &info ); |
40 |
|
41 |
#ifdef IS_MPI |
42 |
|
43 |
MPI_Datatype mpiAtomStructType; |
44 |
|
45 |
#endif |
46 |
} |
47 |
|
48 |
|
49 |
//**************************************************************** |
50 |
// begins the actual forcefield stuff. |
51 |
//**************************************************************** |
52 |
|
53 |
|
54 |
LJ_FF::LJ_FF(){ |
55 |
|
56 |
char fileName[200]; |
57 |
char* ffPath_env = "FORCE_PARAM_PATH"; |
58 |
char* ffPath; |
59 |
char temp[200]; |
60 |
char errMsg[1000]; |
61 |
|
62 |
// do the funtion wrapping |
63 |
wrapMeFF( this ); |
64 |
|
65 |
#ifdef IS_MPI |
66 |
int i; |
67 |
|
68 |
// ********************************************************************** |
69 |
// Init the atomStruct mpi type |
70 |
|
71 |
atomStruct atomProto; // mpiPrototype |
72 |
int atomBC[3] = {15,3,2}; // block counts |
73 |
MPI_Aint atomDspls[3]; // displacements |
74 |
MPI_Datatype atomMbrTypes[3]; // member mpi types |
75 |
|
76 |
MPI_Address(&atomProto.name, &atomDspls[0]); |
77 |
MPI_Address(&atomProto.mass, &atomDspls[1]); |
78 |
MPI_Address(&atomProto.ident, &atomDspls[2]); |
79 |
|
80 |
atomMbrTypes[0] = MPI_CHAR; |
81 |
atomMbrTypes[1] = MPI_DOUBLE; |
82 |
atomMbrTypes[2] = MPI_INT; |
83 |
|
84 |
for (i=2; i >= 0; i--) atomDspls[i] -= atomDspls[0]; |
85 |
|
86 |
MPI_Type_struct(3, atomBC, atomDspls, atomMbrTypes, &mpiAtomStructType); |
87 |
MPI_Type_commit(&mpiAtomStructType); |
88 |
|
89 |
// *********************************************************************** |
90 |
|
91 |
if( worldRank == 0 ){ |
92 |
#endif |
93 |
|
94 |
// generate the force file name |
95 |
|
96 |
strcpy( fileName, "LJ_FF.frc" ); |
97 |
// fprintf( stderr,"Trying to open %s\n", fileName ); |
98 |
|
99 |
// attempt to open the file in the current directory first. |
100 |
|
101 |
frcFile = fopen( fileName, "r" ); |
102 |
|
103 |
if( frcFile == NULL ){ |
104 |
|
105 |
// next see if the force path enviorment variable is set |
106 |
|
107 |
ffPath = getenv( ffPath_env ); |
108 |
if( ffPath == NULL ) { |
109 |
sprintf( painCave.errMsg, |
110 |
"Error opening the force field parameter file: %s\n" |
111 |
"Have you tried setting the FORCE_PARAM_PATH environment " |
112 |
"vairable?\n", |
113 |
fileName ); |
114 |
painCave.isFatal = 1; |
115 |
simError(); |
116 |
} |
117 |
|
118 |
|
119 |
strcpy( temp, ffPath ); |
120 |
strcat( temp, "/" ); |
121 |
strcat( temp, fileName ); |
122 |
strcpy( fileName, temp ); |
123 |
|
124 |
frcFile = fopen( fileName, "r" ); |
125 |
|
126 |
if( frcFile == NULL ){ |
127 |
|
128 |
sprintf( painCave.errMsg, |
129 |
"Error opening the force field parameter file: %s\n" |
130 |
"Have you tried setting the FORCE_PARAM_PATH environment " |
131 |
"vairable?\n", |
132 |
fileName ); |
133 |
painCave.isFatal = 1; |
134 |
simError(); |
135 |
} |
136 |
} |
137 |
|
138 |
#ifdef IS_MPI |
139 |
} |
140 |
|
141 |
sprintf( checkPointMsg, "LJ_FF file opened sucessfully." ); |
142 |
MPIcheckPoint(); |
143 |
|
144 |
#endif // is_mpi |
145 |
} |
146 |
|
147 |
|
148 |
LJ_FF::~LJ_FF(){ |
149 |
|
150 |
#ifdef IS_MPI |
151 |
if( worldRank == 0 ){ |
152 |
#endif // is_mpi |
153 |
|
154 |
fclose( frcFile ); |
155 |
|
156 |
#ifdef IS_MPI |
157 |
} |
158 |
#endif // is_mpi |
159 |
} |
160 |
|
161 |
|
162 |
|
163 |
void LJ_FF::initializeAtoms( void ){ |
164 |
|
165 |
class LinkedType { |
166 |
public: |
167 |
LinkedType(){ |
168 |
next = NULL; |
169 |
name[0] = '\0'; |
170 |
} |
171 |
~LinkedType(){ if( next != NULL ) delete next; } |
172 |
|
173 |
LinkedType* find(char* key){ |
174 |
if( !strcmp(name, key) ) return this; |
175 |
if( next != NULL ) return next->find(key); |
176 |
return NULL; |
177 |
} |
178 |
|
179 |
|
180 |
void add( atomStruct &info ){ |
181 |
|
182 |
// check for duplicates |
183 |
|
184 |
if( !strcmp( info.name, name ) ){ |
185 |
sprintf( painCave.errMsg, |
186 |
"Duplicate LJ atom type \"%s\" found in " |
187 |
"the LJ_FF param file./n", |
188 |
name ); |
189 |
painCave.isFatal = 1; |
190 |
simError(); |
191 |
} |
192 |
|
193 |
if( next != NULL ) next->add(info); |
194 |
else{ |
195 |
next = new LinkedType(); |
196 |
strcpy(next->name, info.name); |
197 |
next->mass = info.mass; |
198 |
next->epslon = info.epslon; |
199 |
next->sigma = info.sigma; |
200 |
next->ident = info.ident; |
201 |
} |
202 |
} |
203 |
|
204 |
|
205 |
#ifdef IS_MPI |
206 |
|
207 |
void duplicate( atomStruct &info ){ |
208 |
strcpy(info.name, name); |
209 |
info.mass = mass; |
210 |
info.epslon = epslon; |
211 |
info.sigma = sigma; |
212 |
info.ident = ident; |
213 |
info.last = 0; |
214 |
} |
215 |
|
216 |
|
217 |
#endif |
218 |
|
219 |
char name[15]; |
220 |
double mass; |
221 |
double epslon; |
222 |
double sigma; |
223 |
int ident; |
224 |
LinkedType* next; |
225 |
}; |
226 |
|
227 |
LinkedType* headAtomType; |
228 |
LinkedType* currentAtomType; |
229 |
atomStruct info; |
230 |
info.last = 1; // initialize last to have the last set. |
231 |
// if things go well, last will be set to 0 |
232 |
|
233 |
int i; |
234 |
int identNum; |
235 |
|
236 |
Atom** the_atoms; |
237 |
int nAtoms; |
238 |
the_atoms = entry_plug->atoms; |
239 |
nAtoms = entry_plug->n_atoms; |
240 |
|
241 |
|
242 |
#ifdef IS_MPI |
243 |
if( worldRank == 0 ){ |
244 |
#endif |
245 |
|
246 |
// read in the atom types. |
247 |
|
248 |
headAtomType = new LinkedType; |
249 |
|
250 |
fastForward( "AtomTypes", "initializeAtoms" ); |
251 |
|
252 |
// we are now at the AtomTypes section. |
253 |
|
254 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
255 |
lineNum++; |
256 |
|
257 |
|
258 |
// read a line, and start parseing out the atom types |
259 |
|
260 |
if( eof_test == NULL ){ |
261 |
sprintf( painCave.errMsg, |
262 |
"Error in reading Atoms from force file at line %d.\n", |
263 |
lineNum ); |
264 |
painCave.isFatal = 1; |
265 |
simError(); |
266 |
} |
267 |
|
268 |
identNum = 1; |
269 |
// stop reading at end of file, or at next section |
270 |
while( readLine[0] != '#' && eof_test != NULL ){ |
271 |
|
272 |
// toss comment lines |
273 |
if( readLine[0] != '!' ){ |
274 |
|
275 |
// the parser returns 0 if the line was blank |
276 |
if( parseAtomLJ( readLine, lineNum, info ) ){ |
277 |
info.ident = identNum; |
278 |
headAtomType->add( info );; |
279 |
identNum++; |
280 |
} |
281 |
} |
282 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
283 |
lineNum++; |
284 |
} |
285 |
|
286 |
#ifdef IS_MPI |
287 |
|
288 |
// send out the linked list to all the other processes |
289 |
|
290 |
sprintf( checkPointMsg, |
291 |
"LJ_FF atom structures read successfully." ); |
292 |
MPIcheckPoint(); |
293 |
|
294 |
currentAtomType = headAtomType->next; //skip the first element who is a place holder. |
295 |
while( currentAtomType != NULL ){ |
296 |
currentAtomType->duplicate( info ); |
297 |
|
298 |
|
299 |
|
300 |
sendFrcStruct( &info, mpiAtomStructType ); |
301 |
|
302 |
sprintf( checkPointMsg, |
303 |
"successfully sent lJ force type: \"%s\"\n", |
304 |
info.name ); |
305 |
MPIcheckPoint(); |
306 |
|
307 |
currentAtomType = currentAtomType->next; |
308 |
} |
309 |
info.last = 1; |
310 |
sendFrcStruct( &info, mpiAtomStructType ); |
311 |
|
312 |
} |
313 |
|
314 |
else{ |
315 |
|
316 |
// listen for node 0 to send out the force params |
317 |
|
318 |
MPIcheckPoint(); |
319 |
|
320 |
headAtomType = new LinkedType; |
321 |
recieveFrcStruct( &info, mpiAtomStructType ); |
322 |
|
323 |
while( !info.last ){ |
324 |
|
325 |
|
326 |
|
327 |
headAtomType->add( info ); |
328 |
|
329 |
MPIcheckPoint(); |
330 |
|
331 |
recieveFrcStruct( &info, mpiAtomStructType ); |
332 |
} |
333 |
} |
334 |
#endif // is_mpi |
335 |
|
336 |
// call new A_types in fortran |
337 |
|
338 |
int isError; |
339 |
|
340 |
// dummy variables |
341 |
int isLJ = 1; |
342 |
int isDipole = 0; |
343 |
int isSSD = 0; |
344 |
int isGB = 0; |
345 |
double w0 = 0.0; |
346 |
double v0 = 0.0; |
347 |
double dipole = 0.0; |
348 |
|
349 |
|
350 |
currentAtomType = headAtomType; |
351 |
while( currentAtomType != NULL ){ |
352 |
|
353 |
if( currentAtomType->name[0] != '\0' ){ |
354 |
isError = 0; |
355 |
newAtype( &(currentAtomType->ident), |
356 |
&(currentAtomType->mass), |
357 |
&(currentAtomType->epslon), |
358 |
&(currentAtomType->sigma), |
359 |
&isLJ, &isSSD, &isDipole, &isGB, &w0, &v0, &dipole, |
360 |
&isError ); |
361 |
if( isError ){ |
362 |
sprintf( painCave.errMsg, |
363 |
"Error initializing the \"%s\" atom type in fortran\n", |
364 |
currentAtomType->name ); |
365 |
painCave.isFatal = 1; |
366 |
simError(); |
367 |
} |
368 |
} |
369 |
currentAtomType = currentAtomType->next; |
370 |
} |
371 |
|
372 |
#ifdef IS_MPI |
373 |
sprintf( checkPointMsg, |
374 |
"LJ_FF atom structures successfully sent to fortran\n" ); |
375 |
MPIcheckPoint(); |
376 |
#endif // is_mpi |
377 |
|
378 |
|
379 |
|
380 |
// initialize the atoms |
381 |
|
382 |
double bigSigma = 0.0; |
383 |
Atom* thisAtom; |
384 |
|
385 |
for( i=0; i<nAtoms; i++ ){ |
386 |
|
387 |
currentAtomType = headAtomType->find( the_atoms[i]->getType() ); |
388 |
if( currentAtomType == NULL ){ |
389 |
sprintf( painCave.errMsg, |
390 |
"AtomType error, %s not found in force file.\n", |
391 |
the_atoms[i]->getType() ); |
392 |
painCave.isFatal = 1; |
393 |
simError(); |
394 |
} |
395 |
|
396 |
the_atoms[i]->setMass( currentAtomType->mass ); |
397 |
the_atoms[i]->setEpslon( currentAtomType->epslon ); |
398 |
the_atoms[i]->setSigma( currentAtomType->sigma ); |
399 |
the_atoms[i]->setIdent( currentAtomType->ident ); |
400 |
the_atoms[i]->setLJ(); |
401 |
|
402 |
if( bigSigma < currentAtomType->sigma ) bigSigma = currentAtomType->sigma; |
403 |
} |
404 |
|
405 |
|
406 |
#ifdef IS_MPI |
407 |
double tempBig = bigSigma; |
408 |
MPI::COMM_WORLD.Allreduce( &tempBig, &bigSigma, 1, MPI_DOUBLE, MPI_MAX ); |
409 |
#endif //is_mpi |
410 |
|
411 |
//calc rCut and rList |
412 |
|
413 |
entry_plug->rCut = 2.5 * bigSigma; |
414 |
if(entry_plug->rCut > (entry_plug->box_x / 2.0)) entry_plug->rCut = entry_plug->box_x / 2.0; |
415 |
if(entry_plug->rCut > (entry_plug->box_y / 2.0)) entry_plug->rCut = entry_plug->box_y / 2.0; |
416 |
if(entry_plug->rCut > (entry_plug->box_z / 2.0)) entry_plug->rCut = entry_plug->box_z / 2.0; |
417 |
|
418 |
entry_plug->rList = entry_plug->rCut + 1.0; |
419 |
|
420 |
// clean up the memory |
421 |
|
422 |
delete headAtomType; |
423 |
|
424 |
#ifdef IS_MPI |
425 |
sprintf( checkPointMsg, "LJ_FF atoms initialized succesfully" ); |
426 |
MPIcheckPoint(); |
427 |
#endif // is_mpi |
428 |
|
429 |
initFortran(); |
430 |
entry_plug->refreshSim(); |
431 |
|
432 |
} |
433 |
|
434 |
void LJ_FF::initializeBonds( bond_pair* the_bonds ){ |
435 |
|
436 |
if( entry_plug->n_bonds ){ |
437 |
sprintf( painCave.errMsg, |
438 |
"LJ_FF does not support bonds.\n" ); |
439 |
painCave.isFatal = 1; |
440 |
simError(); |
441 |
} |
442 |
#ifdef IS_MPI |
443 |
MPIcheckPoint(); |
444 |
#endif // is_mpi |
445 |
|
446 |
} |
447 |
|
448 |
void LJ_FF::initializeBends( bend_set* the_bends ){ |
449 |
|
450 |
if( entry_plug->n_bends ){ |
451 |
sprintf( painCave.errMsg, |
452 |
"LJ_FF does not support bends.\n" ); |
453 |
painCave.isFatal = 1; |
454 |
simError(); |
455 |
} |
456 |
#ifdef IS_MPI |
457 |
MPIcheckPoint(); |
458 |
#endif // is_mpi |
459 |
|
460 |
} |
461 |
|
462 |
void LJ_FF::initializeTorsions( torsion_set* the_torsions ){ |
463 |
|
464 |
if( entry_plug->n_torsions ){ |
465 |
sprintf( painCave.errMsg, |
466 |
"LJ_FF does not support torsions.\n" ); |
467 |
painCave.isFatal = 1; |
468 |
simError(); |
469 |
} |
470 |
#ifdef IS_MPI |
471 |
MPIcheckPoint(); |
472 |
#endif // is_mpi |
473 |
|
474 |
} |
475 |
|
476 |
void LJ_FF::doForces( int calcPot ){ |
477 |
|
478 |
int i, isError; |
479 |
double* frc; |
480 |
double* pos; |
481 |
double* trq; |
482 |
double* tau; |
483 |
short int passedCalcPot = (short int)calcPot; |
484 |
|
485 |
// forces are zeroed here, before any are acumulated. |
486 |
// NOTE: do not rezero the forces in Fortran. |
487 |
|
488 |
for(i=0; i<entry_plug->n_atoms; i++){ |
489 |
entry_plug->atoms[i]->zeroForces(); |
490 |
} |
491 |
|
492 |
frc = Atom::getFrcArray(); |
493 |
pos = Atom::getPosArray(); |
494 |
trq = Atom::getTrqArray(); |
495 |
tau = entry_plug->tau; |
496 |
|
497 |
// dummy variables |
498 |
// NOTE: THIS IS A MAJOR POTENTIAL BUG!!!!!! be careful. |
499 |
// that said, here we go anyway. |
500 |
|
501 |
double* A = NULL; |
502 |
double* mu = NULL; |
503 |
double* u_l = NULL; |
504 |
|
505 |
isError = 0; |
506 |
fortranForceLoop( pos, A, mu, u_l, frc, trq, tau, &(entry_plug->lrPot), |
507 |
&passedCalcPot, &isError ); |
508 |
|
509 |
|
510 |
if( isError ){ |
511 |
sprintf( painCave.errMsg, |
512 |
"Error returned from the fortran force calculation.\n" ); |
513 |
painCave.isFatal = 1; |
514 |
simError(); |
515 |
} |
516 |
|
517 |
#ifdef IS_MPI |
518 |
sprintf( checkPointMsg, |
519 |
"returned from the force calculation.\n" ); |
520 |
MPIcheckPoint(); |
521 |
#endif // is_mpi |
522 |
|
523 |
} |
524 |
|
525 |
void LJ_FF::initFortran( void ){ |
526 |
|
527 |
int nLocal = entry_plug->n_atoms; |
528 |
int *ident; |
529 |
int isError; |
530 |
int i; |
531 |
|
532 |
ident = new int[nLocal]; |
533 |
|
534 |
for(i=0; i<nLocal; i++){ |
535 |
ident[i] = entry_plug->atoms[i]->getIdent(); |
536 |
} |
537 |
|
538 |
isError = 0; |
539 |
initfortran( &nLocal, ident, &isError ); |
540 |
|
541 |
if(isError){ |
542 |
sprintf( painCave.errMsg, |
543 |
"LJ_FF error: There was an error initializing the component list in fortran.\n" ); |
544 |
painCave.isFatal = 1; |
545 |
simError(); |
546 |
} |
547 |
|
548 |
|
549 |
#ifdef IS_MPI |
550 |
sprintf( checkPointMsg, "LJ_FF successfully initialized the fortran component list.\n" ); |
551 |
MPIcheckPoint(); |
552 |
#endif // is_mpi |
553 |
|
554 |
delete[] ident; |
555 |
|
556 |
} |
557 |
|
558 |
|
559 |
void LJ_FF::fastForward( char* stopText, char* searchOwner ){ |
560 |
|
561 |
int foundText = 0; |
562 |
char* the_token; |
563 |
|
564 |
rewind( frcFile ); |
565 |
lineNum = 0; |
566 |
|
567 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
568 |
lineNum++; |
569 |
if( eof_test == NULL ){ |
570 |
sprintf( painCave.errMsg, "Error fast forwarding force file for %s: " |
571 |
" file is empty.\n", |
572 |
searchOwner ); |
573 |
painCave.isFatal = 1; |
574 |
simError(); |
575 |
} |
576 |
|
577 |
|
578 |
while( !foundText ){ |
579 |
while( eof_test != NULL && readLine[0] != '#' ){ |
580 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
581 |
lineNum++; |
582 |
} |
583 |
if( eof_test == NULL ){ |
584 |
sprintf( painCave.errMsg, |
585 |
"Error fast forwarding force file for %s at " |
586 |
"line %d: file ended unexpectedly.\n", |
587 |
searchOwner, |
588 |
lineNum ); |
589 |
painCave.isFatal = 1; |
590 |
simError(); |
591 |
} |
592 |
|
593 |
the_token = strtok( readLine, " ,;\t#\n" ); |
594 |
foundText = !strcmp( stopText, the_token ); |
595 |
|
596 |
if( !foundText ){ |
597 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
598 |
lineNum++; |
599 |
|
600 |
if( eof_test == NULL ){ |
601 |
sprintf( painCave.errMsg, |
602 |
"Error fast forwarding force file for %s at " |
603 |
"line %d: file ended unexpectedly.\n", |
604 |
searchOwner, |
605 |
lineNum ); |
606 |
painCave.isFatal = 1; |
607 |
simError(); |
608 |
} |
609 |
} |
610 |
} |
611 |
} |
612 |
|
613 |
|
614 |
|
615 |
int parseAtom( char *lineBuffer, int lineNum, atomStruct &info ){ |
616 |
|
617 |
char* the_token; |
618 |
|
619 |
the_token = strtok( lineBuffer, " \n\t,;" ); |
620 |
if( the_token != NULL ){ |
621 |
|
622 |
strcpy( info.name, the_token ); |
623 |
|
624 |
if( ( the_token = strtok( NULL, " \n\t,;" ) ) == NULL ){ |
625 |
sprintf( painCave.errMsg, |
626 |
"Error parseing AtomTypes: line %d\n", lineNum ); |
627 |
painCave.isFatal = 1; |
628 |
simError(); |
629 |
} |
630 |
|
631 |
info.mass = atof( the_token ); |
632 |
|
633 |
if( ( the_token = strtok( NULL, " \n\t,;" ) ) == NULL ){ |
634 |
sprintf( painCave.errMsg, |
635 |
"Error parseing AtomTypes: line %d\n", lineNum ); |
636 |
painCave.isFatal = 1; |
637 |
simError(); |
638 |
} |
639 |
|
640 |
info.epslon = atof( the_token ); |
641 |
|
642 |
if( ( the_token = strtok( NULL, " \n\t,;" ) ) == NULL ){ |
643 |
sprintf( painCave.errMsg, |
644 |
"Error parseing AtomTypes: line %d\n", lineNum ); |
645 |
painCave.isFatal = 1; |
646 |
simError(); |
647 |
} |
648 |
|
649 |
info.sigma = atof( the_token ); |
650 |
|
651 |
return 1; |
652 |
} |
653 |
else return 0; |
654 |
} |