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