1 |
#include <stdlib.h> |
2 |
#include <stdio.h> |
3 |
#include <string.h> |
4 |
|
5 |
#include <iostream> |
6 |
using namespace std; |
7 |
|
8 |
#ifdef IS_MPI |
9 |
#include <mpi.h> |
10 |
#endif //is_mpi |
11 |
|
12 |
#include "UseTheForce/ForceFields.hpp" |
13 |
#include "primitives/SRI.hpp" |
14 |
#include "utils/simError.h" |
15 |
#include "UseTheForce/DarkSide/atype_interface.h" |
16 |
|
17 |
//#include "UseTheForce/fortranWrappers.hpp" |
18 |
|
19 |
#ifdef IS_MPI |
20 |
#include "UseTheForce/mpiForceField.h" |
21 |
#endif // is_mpi |
22 |
|
23 |
|
24 |
|
25 |
namespace LJ_NS{ |
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 |
class LinkedAtomType { |
48 |
public: |
49 |
LinkedAtomType(){ |
50 |
next = NULL; |
51 |
name[0] = '\0'; |
52 |
} |
53 |
~LinkedAtomType(){ if( next != NULL ) delete next; } |
54 |
|
55 |
LinkedAtomType* find(char* key){ |
56 |
if( !strcmp(name, key) ) return this; |
57 |
if( next != NULL ) return next->find(key); |
58 |
return NULL; |
59 |
} |
60 |
|
61 |
|
62 |
void add( atomStruct &info ){ |
63 |
|
64 |
// check for duplicates |
65 |
|
66 |
if( !strcmp( info.name, name ) ){ |
67 |
sprintf( painCave.errMsg, |
68 |
"Duplicate LJ atom type \"%s\" found in " |
69 |
"the LJFF param file./n", |
70 |
name ); |
71 |
painCave.isFatal = 1; |
72 |
simError(); |
73 |
} |
74 |
|
75 |
if( next != NULL ) next->add(info); |
76 |
else{ |
77 |
next = new LinkedAtomType(); |
78 |
strcpy(next->name, info.name); |
79 |
next->mass = info.mass; |
80 |
next->epslon = info.epslon; |
81 |
next->sigma = info.sigma; |
82 |
next->ident = info.ident; |
83 |
} |
84 |
} |
85 |
|
86 |
|
87 |
#ifdef IS_MPI |
88 |
|
89 |
void duplicate( atomStruct &info ){ |
90 |
strcpy(info.name, name); |
91 |
info.mass = mass; |
92 |
info.epslon = epslon; |
93 |
info.sigma = sigma; |
94 |
info.ident = ident; |
95 |
info.last = 0; |
96 |
} |
97 |
|
98 |
|
99 |
#endif |
100 |
|
101 |
char name[15]; |
102 |
double mass; |
103 |
double epslon; |
104 |
double sigma; |
105 |
int ident; |
106 |
LinkedAtomType* next; |
107 |
}; |
108 |
|
109 |
LinkedAtomType* headAtomType; |
110 |
LinkedAtomType* currentAtomType; |
111 |
|
112 |
} |
113 |
|
114 |
using namespace LJ_NS; |
115 |
|
116 |
//**************************************************************** |
117 |
// begins the actual forcefield stuff. |
118 |
//**************************************************************** |
119 |
|
120 |
|
121 |
LJFF::LJFF(){ |
122 |
|
123 |
char fileName[200]; |
124 |
char* ffPath_env = "FORCE_PARAM_PATH"; |
125 |
char* ffPath; |
126 |
char temp[200]; |
127 |
|
128 |
headAtomType = NULL; |
129 |
currentAtomType = NULL; |
130 |
|
131 |
// do the funtion wrapping |
132 |
// wrapMeFF( this ); |
133 |
|
134 |
#ifdef IS_MPI |
135 |
int i; |
136 |
|
137 |
// ********************************************************************** |
138 |
// Init the atomStruct mpi type |
139 |
|
140 |
atomStruct atomProto; // mpiPrototype |
141 |
int atomBC[3] = {15,3,2}; // block counts |
142 |
MPI_Aint atomDspls[3]; // displacements |
143 |
MPI_Datatype atomMbrTypes[3]; // member mpi types |
144 |
|
145 |
MPI_Address(&atomProto.name, &atomDspls[0]); |
146 |
MPI_Address(&atomProto.mass, &atomDspls[1]); |
147 |
MPI_Address(&atomProto.ident, &atomDspls[2]); |
148 |
|
149 |
atomMbrTypes[0] = MPI_CHAR; |
150 |
atomMbrTypes[1] = MPI_DOUBLE; |
151 |
atomMbrTypes[2] = MPI_INT; |
152 |
|
153 |
for (i=2; i >= 0; i--) atomDspls[i] -= atomDspls[0]; |
154 |
|
155 |
MPI_Type_struct(3, atomBC, atomDspls, atomMbrTypes, &mpiAtomStructType); |
156 |
MPI_Type_commit(&mpiAtomStructType); |
157 |
|
158 |
// *********************************************************************** |
159 |
|
160 |
if( worldRank == 0 ){ |
161 |
#endif |
162 |
|
163 |
// generate the force file name |
164 |
|
165 |
strcpy( fileName, "LJFF.frc" ); |
166 |
// fprintf( stderr,"Trying to open %s\n", fileName ); |
167 |
|
168 |
// attempt to open the file in the current directory first. |
169 |
|
170 |
frcFile = fopen( fileName, "r" ); |
171 |
|
172 |
if( frcFile == NULL ){ |
173 |
|
174 |
// next see if the force path enviorment variable is set |
175 |
|
176 |
ffPath = getenv( ffPath_env ); |
177 |
if( ffPath == NULL ) { |
178 |
STR_DEFINE(ffPath, FRC_PATH ); |
179 |
} |
180 |
|
181 |
|
182 |
strcpy( temp, ffPath ); |
183 |
strcat( temp, "/" ); |
184 |
strcat( temp, fileName ); |
185 |
strcpy( fileName, temp ); |
186 |
|
187 |
frcFile = fopen( fileName, "r" ); |
188 |
|
189 |
if( frcFile == NULL ){ |
190 |
|
191 |
sprintf( painCave.errMsg, |
192 |
"Error opening the force field parameter file:\n" |
193 |
"\t%s\n" |
194 |
"\tHave you tried setting the FORCE_PARAM_PATH environment " |
195 |
"variable?\n", |
196 |
fileName ); |
197 |
painCave.severity = OOPSE_ERROR; |
198 |
painCave.isFatal = 1; |
199 |
simError(); |
200 |
} |
201 |
} |
202 |
|
203 |
#ifdef IS_MPI |
204 |
} |
205 |
|
206 |
sprintf( checkPointMsg, "LJFF file opened sucessfully." ); |
207 |
MPIcheckPoint(); |
208 |
|
209 |
#endif // is_mpi |
210 |
} |
211 |
|
212 |
|
213 |
LJFF::~LJFF(){ |
214 |
|
215 |
if( headAtomType != NULL ) delete headAtomType; |
216 |
|
217 |
#ifdef IS_MPI |
218 |
if( worldRank == 0 ){ |
219 |
#endif // is_mpi |
220 |
|
221 |
fclose( frcFile ); |
222 |
|
223 |
#ifdef IS_MPI |
224 |
} |
225 |
#endif // is_mpi |
226 |
} |
227 |
|
228 |
void LJFF::initForceField(){ |
229 |
|
230 |
initFortran(0); |
231 |
} |
232 |
|
233 |
void LJFF::cleanMe( void ){ |
234 |
|
235 |
#ifdef IS_MPI |
236 |
|
237 |
// keep the linked list in the mpi version |
238 |
|
239 |
#else // is_mpi |
240 |
|
241 |
// delete the linked list in the single processor version |
242 |
|
243 |
if( headAtomType != NULL ) delete headAtomType; |
244 |
|
245 |
#endif // is_mpi |
246 |
} |
247 |
|
248 |
void LJFF::readParams( void ){ |
249 |
|
250 |
atomStruct info; |
251 |
info.last = 1; // initialize last to have the last set. |
252 |
// if things go well, last will be set to 0 |
253 |
|
254 |
int identNum; |
255 |
|
256 |
|
257 |
bigSigma = 0.0; |
258 |
#ifdef IS_MPI |
259 |
if( worldRank == 0 ){ |
260 |
#endif |
261 |
|
262 |
// read in the atom types. |
263 |
|
264 |
headAtomType = new LinkedAtomType; |
265 |
|
266 |
fastForward( "AtomTypes", "initializeAtoms" ); |
267 |
|
268 |
// we are now at the AtomTypes section. |
269 |
|
270 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
271 |
lineNum++; |
272 |
|
273 |
|
274 |
// read a line, and start parseing out the atom types |
275 |
|
276 |
if( eof_test == NULL ){ |
277 |
sprintf( painCave.errMsg, |
278 |
"Error in reading Atoms from force file at line %d.\n", |
279 |
lineNum ); |
280 |
painCave.isFatal = 1; |
281 |
simError(); |
282 |
} |
283 |
|
284 |
identNum = 1; |
285 |
// stop reading at end of file, or at next section |
286 |
while( readLine[0] != '#' && eof_test != NULL ){ |
287 |
|
288 |
// toss comment lines |
289 |
if( readLine[0] != '!' ){ |
290 |
|
291 |
// the parser returns 0 if the line was blank |
292 |
if( parseAtom( readLine, lineNum, info ) ){ |
293 |
info.ident = identNum; |
294 |
headAtomType->add( info );; |
295 |
identNum++; |
296 |
} |
297 |
} |
298 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
299 |
lineNum++; |
300 |
} |
301 |
|
302 |
#ifdef IS_MPI |
303 |
|
304 |
// send out the linked list to all the other processes |
305 |
|
306 |
sprintf( checkPointMsg, |
307 |
"LJFF atom structures read successfully." ); |
308 |
MPIcheckPoint(); |
309 |
|
310 |
currentAtomType = headAtomType->next; //skip the first element who is a place holder. |
311 |
while( currentAtomType != NULL ){ |
312 |
currentAtomType->duplicate( info ); |
313 |
|
314 |
|
315 |
|
316 |
sendFrcStruct( &info, mpiAtomStructType ); |
317 |
|
318 |
sprintf( checkPointMsg, |
319 |
"successfully sent lJ force type: \"%s\"\n", |
320 |
info.name ); |
321 |
MPIcheckPoint(); |
322 |
|
323 |
currentAtomType = currentAtomType->next; |
324 |
} |
325 |
info.last = 1; |
326 |
sendFrcStruct( &info, mpiAtomStructType ); |
327 |
|
328 |
} |
329 |
|
330 |
else{ |
331 |
|
332 |
// listen for node 0 to send out the force params |
333 |
|
334 |
MPIcheckPoint(); |
335 |
|
336 |
headAtomType = new LinkedAtomType; |
337 |
receiveFrcStruct( &info, mpiAtomStructType ); |
338 |
|
339 |
while( !info.last ){ |
340 |
|
341 |
|
342 |
|
343 |
headAtomType->add( info ); |
344 |
|
345 |
MPIcheckPoint(); |
346 |
|
347 |
receiveFrcStruct( &info, mpiAtomStructType ); |
348 |
} |
349 |
} |
350 |
#endif // is_mpi |
351 |
|
352 |
// call new A_types in fortran |
353 |
|
354 |
int isError; |
355 |
|
356 |
// dummy variables |
357 |
int isLJ = 1; |
358 |
int isDipole = 0; |
359 |
int isSSD = 0; |
360 |
int isGB = 0; |
361 |
int isEAM = 0; |
362 |
int isCharge = 0; |
363 |
double charge = 0.0; |
364 |
double dipole = 0.0; |
365 |
|
366 |
currentAtomType = headAtomType; |
367 |
while( currentAtomType != NULL ){ |
368 |
|
369 |
if( currentAtomType->name[0] != '\0' ){ |
370 |
isError = 0; |
371 |
makeAtype( &(currentAtomType->ident), |
372 |
&isLJ, |
373 |
&isSSD, |
374 |
&isDipole, |
375 |
&isGB, |
376 |
&isEAM, |
377 |
&isCharge, |
378 |
&(currentAtomType->epslon), |
379 |
&(currentAtomType->sigma), |
380 |
&charge, |
381 |
&dipole, |
382 |
&isError ); |
383 |
if( isError ){ |
384 |
sprintf( painCave.errMsg, |
385 |
"Error initializing the \"%s\" atom type in fortran\n", |
386 |
currentAtomType->name ); |
387 |
painCave.isFatal = 1; |
388 |
simError(); |
389 |
} |
390 |
} |
391 |
currentAtomType = currentAtomType->next; |
392 |
} |
393 |
|
394 |
entry_plug->useLJ = 1; |
395 |
|
396 |
#ifdef IS_MPI |
397 |
sprintf( checkPointMsg, |
398 |
"LJFF atom structures successfully sent to fortran\n" ); |
399 |
MPIcheckPoint(); |
400 |
#endif // is_mpi |
401 |
|
402 |
} |
403 |
|
404 |
double LJFF::getAtomTypeMass (char* atomType) { |
405 |
|
406 |
currentAtomType = headAtomType->find( atomType ); |
407 |
if( currentAtomType == NULL ){ |
408 |
sprintf( painCave.errMsg, |
409 |
"AtomType error, %s not found in force file.\n", |
410 |
atomType ); |
411 |
painCave.isFatal = 1; |
412 |
simError(); |
413 |
} |
414 |
|
415 |
return currentAtomType->mass; |
416 |
} |
417 |
|
418 |
void LJFF::initializeAtoms( int nAtoms, Atom** the_atoms ){ |
419 |
|
420 |
int i; |
421 |
|
422 |
// initialize the atoms |
423 |
|
424 |
|
425 |
for( i=0; i<nAtoms; i++ ){ |
426 |
|
427 |
currentAtomType = headAtomType->find( the_atoms[i]->getType() ); |
428 |
if( currentAtomType == NULL ){ |
429 |
sprintf( painCave.errMsg, |
430 |
"AtomType error, %s not found in force file.\n", |
431 |
the_atoms[i]->getType() ); |
432 |
painCave.isFatal = 1; |
433 |
simError(); |
434 |
} |
435 |
|
436 |
the_atoms[i]->setMass( currentAtomType->mass ); |
437 |
the_atoms[i]->setIdent( currentAtomType->ident ); |
438 |
|
439 |
if( bigSigma < currentAtomType->sigma ) bigSigma = currentAtomType->sigma; |
440 |
} |
441 |
} |
442 |
|
443 |
void LJFF::initializeBonds( int nBonds, Bond** BondArray, |
444 |
bond_pair* the_bonds ){ |
445 |
|
446 |
if( nBonds ){ |
447 |
sprintf( painCave.errMsg, |
448 |
"LJFF does not support bonds.\n" ); |
449 |
painCave.isFatal = 1; |
450 |
simError(); |
451 |
} |
452 |
} |
453 |
|
454 |
void LJFF::initializeBends( int nBends, Bend** bendArray, |
455 |
bend_set* the_bends ){ |
456 |
|
457 |
if( nBends ){ |
458 |
sprintf( painCave.errMsg, |
459 |
"LJFF does not support bends.\n" ); |
460 |
painCave.isFatal = 1; |
461 |
simError(); |
462 |
} |
463 |
} |
464 |
|
465 |
void LJFF::initializeTorsions( int nTorsions, Torsion** torsionArray, |
466 |
torsion_set* the_torsions ){ |
467 |
|
468 |
if( nTorsions ){ |
469 |
sprintf( painCave.errMsg, |
470 |
"LJFF does not support torsions.\n" ); |
471 |
painCave.isFatal = 1; |
472 |
simError(); |
473 |
} |
474 |
} |
475 |
|
476 |
void LJFF::fastForward( char* stopText, char* searchOwner ){ |
477 |
|
478 |
int foundText = 0; |
479 |
char* the_token; |
480 |
|
481 |
rewind( frcFile ); |
482 |
lineNum = 0; |
483 |
|
484 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
485 |
lineNum++; |
486 |
if( eof_test == NULL ){ |
487 |
sprintf( painCave.errMsg, "Error fast forwarding force file for %s: " |
488 |
" file is empty.\n", |
489 |
searchOwner ); |
490 |
painCave.isFatal = 1; |
491 |
simError(); |
492 |
} |
493 |
|
494 |
|
495 |
while( !foundText ){ |
496 |
while( eof_test != NULL && readLine[0] != '#' ){ |
497 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
498 |
lineNum++; |
499 |
} |
500 |
if( eof_test == NULL ){ |
501 |
sprintf( painCave.errMsg, |
502 |
"Error fast forwarding force file for %s at " |
503 |
"line %d: file ended unexpectedly.\n", |
504 |
searchOwner, |
505 |
lineNum ); |
506 |
painCave.isFatal = 1; |
507 |
simError(); |
508 |
} |
509 |
|
510 |
the_token = strtok( readLine, " ,;\t#\n" ); |
511 |
foundText = !strcmp( stopText, the_token ); |
512 |
|
513 |
if( !foundText ){ |
514 |
eof_test = fgets( readLine, sizeof(readLine), frcFile ); |
515 |
lineNum++; |
516 |
|
517 |
if( eof_test == NULL ){ |
518 |
sprintf( painCave.errMsg, |
519 |
"Error fast forwarding force file for %s at " |
520 |
"line %d: file ended unexpectedly.\n", |
521 |
searchOwner, |
522 |
lineNum ); |
523 |
painCave.isFatal = 1; |
524 |
simError(); |
525 |
} |
526 |
} |
527 |
} |
528 |
} |
529 |
|
530 |
|
531 |
|
532 |
int LJ_NS::parseAtom( char *lineBuffer, int lineNum, atomStruct &info ){ |
533 |
|
534 |
char* the_token; |
535 |
|
536 |
the_token = strtok( lineBuffer, " \n\t,;" ); |
537 |
if( the_token != NULL ){ |
538 |
|
539 |
strcpy( info.name, the_token ); |
540 |
|
541 |
if( ( the_token = strtok( NULL, " \n\t,;" ) ) == NULL ){ |
542 |
sprintf( painCave.errMsg, |
543 |
"Error parseing AtomTypes: line %d\n", lineNum ); |
544 |
painCave.isFatal = 1; |
545 |
simError(); |
546 |
} |
547 |
|
548 |
info.mass = atof( the_token ); |
549 |
|
550 |
if( ( the_token = strtok( NULL, " \n\t,;" ) ) == NULL ){ |
551 |
sprintf( painCave.errMsg, |
552 |
"Error parseing AtomTypes: line %d\n", lineNum ); |
553 |
painCave.isFatal = 1; |
554 |
simError(); |
555 |
} |
556 |
|
557 |
info.epslon = atof( the_token ); |
558 |
|
559 |
if( ( the_token = strtok( NULL, " \n\t,;" ) ) == NULL ){ |
560 |
sprintf( painCave.errMsg, |
561 |
"Error parseing AtomTypes: line %d\n", lineNum ); |
562 |
painCave.isFatal = 1; |
563 |
simError(); |
564 |
} |
565 |
|
566 |
info.sigma = atof( the_token ); |
567 |
|
568 |
return 1; |
569 |
} |
570 |
else return 0; |
571 |
} |
572 |
|
573 |
|