1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
|
/*
Project: Adun
Copyright (C) 2005 Michael Johnston & Jordi Villa-Freixa
Author: Michael Johnston
Created: 2005-06-23 11:06:55 +0200 by michael johnston
This application is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
*/
#include "AdunKernel/AdunGRFNonbondedTerm.h"
static NSArray* knownTypes;
@implementation AdGRFNonbondedTerm
- (BOOL) _checkMatrix: (AdDataMatrix*) matrix containsParametersForType: (NSString*) type
{
NSArray* headers;
headers = [matrix columnHeaders];
if([type isEqual: @"A"])
{
if(![headers containsObject: @"VDW A"])
return NO;
else if(![headers containsObject: @"VDW B"])
return NO;
}
else if([type isEqual: @"B"])
{
if(![headers containsObject: @"VDW WellDepth"])
return NO;
else if(![headers containsObject: @"VDW Separation"])
return NO;
}
if(![headers containsObject: @"PartialCharge"])
return NO;
return YES;
}
/*
* To speed up the calculation in the type A case we
* can precompute the product A*A and B*B for the LJ interactions
*/
- (void) _precomputeParameters
{
int atomOne, atomTwo;
ListElement* list_p;
list_p = interactionList->next;
if([lennardJonesType isEqual: @"A"])
{
while(list_p->next != NULL)
{
atomOne = list_p->bond[0];
atomTwo = list_p->bond[1];
list_p->params[0] = parameters->matrix[atomOne][0]*parameters->matrix[atomTwo][0];
list_p->params[1] = parameters->matrix[atomOne][1]*parameters->matrix[atomTwo][1];
list_p->params[2] = partialCharges[atomOne]*partialCharges[atomTwo];
list_p = list_p->next;
}
}
else
{
while(list_p->next != NULL)
{
atomOne = list_p->bond[0];
atomTwo = list_p->bond[1];
list_p->params[0] = sqrt(parameters->matrix[atomOne][0]*parameters->matrix[atomTwo][0]);
list_p->params[1] = (parameters->matrix[atomOne][1] + parameters->matrix[atomTwo][1]);
list_p->params[2] = partialCharges[atomOne]*partialCharges[atomTwo];
list_p = list_p->next;
}
}
}
/*
* Retrieve the necessary parameters from the element properties.
*/
- (void) _initialiseParameters
{
int numberOfElements, i;
NSArray* parametersOne, *parametersTwo;
numberOfElements = [system numberOfElements];
parameters = [memoryManager
allocateMatrixWithRows: numberOfElements
withColumns: 2];
if([lennardJonesType isEqual: @"A"])
{
//We have LJ parameters A and B
parametersOne = [elementProperties columnWithHeader: @"VDW A"];
parametersTwo = [elementProperties columnWithHeader: @"VDW B"];
for(i=0; i<numberOfElements; i++)
{
parameters->matrix[i][0] = [[parametersOne objectAtIndex: i]
doubleValue];
parameters->matrix[i][1] = [[parametersTwo objectAtIndex: i]
doubleValue];
}
}
else
{
//We have LJ parameters WellDepth and Separation
parametersOne = [elementProperties columnWithHeader: @"VDW WellDepth"];
parametersTwo = [elementProperties columnWithHeader: @"VDW Separation"];
for(i=0; i<numberOfElements; i++)
{
parameters->matrix[i][0] = [[parametersOne objectAtIndex: i]
doubleValue];
parameters->matrix[i][1] = [[parametersTwo objectAtIndex: i]
doubleValue];
}
}
parametersOne = [elementProperties columnWithHeader: @"PartialCharge"];
partialCharges = [memoryManager
allocateArrayOfSize: numberOfElements*sizeof(double)];
for(i=0; i<numberOfElements; i++)
partialCharges[i] = [[parametersOne objectAtIndex: i] doubleValue];
}
- (void) _calculateGRFParameters
{
b0 = (epsilon1 - 2*epsilon2*(1 + kappa*cutoff))/epsilon2*(1 + kappa*cutoff);
b1 = (epsilon1 - 4*epsilon2)*(1+kappa*cutoff) - 2*epsilon2*(kappa*cutoff)*(kappa*cutoff);
b1 /= (epsilon1 - 2*epsilon2)*(1+kappa*cutoff) + epsilon2*(kappa*cutoff)*(kappa*cutoff);
b0 += 1;
b0 /= cutoff;
b1 += 1;
b1 /= pow(cutoff,3);
NSDebugLLog(@"GRFNonbondedCalculator", @"B0 initial value %lf. B1 inital value %lf", b0, b1);
}
- (void) _determineLJType
{
NSArray* availableInteractions;
availableInteractions = [system availableInteractions];
if([availableInteractions containsObject: @"TypeOneVDWInteraction"])
lennardJonesType = [@"A" retain];
else if([availableInteractions containsObject: @"TypeTwoVDWInteraction"])
lennardJonesType = [@"B" retain];
else
{
NSWarnLog(@"Unable to determine Lennard Jones type");
NSWarnLog(@"Interactions %@", availableInteractions);
lennardJonesType = [@"A" retain];
}
}
/*
* Initialisation
*/
+ (void) initialize
{
knownTypes = [NSArray arrayWithObjects:
@"A", @"B", nil];
[knownTypes retain];
}
- (id) init
{
return [self initWithSystem: nil];
}
- (id) initWithSystem: (id) system
{
return [self initWithSystem: nil
cutoff: 12.0
updateInterval: 20
epsilonOne: 1.0
epsilonTwo: 78.0
kappa: 0.0
nonbondedPairs: nil
externalForceMatrix: NULL];
}
- (id) initWithSystem: (id) aSystem
cutoff: (double) aDouble
updateInterval: (unsigned int) anInt
epsilonOne: (double) e1
epsilonTwo: (double) e2
kappa: (double) k
nonbondedPairs: (NSArray*) nonbondedPairs
externalForceMatrix: (AdMatrix*) matrix
{
return [self initWithSystem: aSystem
cutoff: aDouble
updateInterval: anInt
epsilonOne: e1
epsilonTwo: e2
kappa: k
nonbondedPairs: nonbondedPairs
externalForceMatrix: matrix
listHandlerClass: [AdCellListHandler class]];
}
- (id) initWithSystem: (id) aSystem
cutoff: (double) aDouble
updateInterval: (unsigned int) anInt
epsilonOne: (double) e1
epsilonTwo: (double) e2
kappa: (double) k
nonbondedPairs: (NSArray*) nonbondedPairs
externalForceMatrix: (AdMatrix*) matrix
listHandlerClass: (Class) aClass
{
AdMatrix* coordinates;
if((self = [super init]))
{
elementProperties = nil;
pairs = nil;
lennardJonesType = nil;
system = nil;
interactionList = NULL;
partialCharges = NULL;
forces = parameters = NULL;
usingExternalForceMatrix = NO;
memoryManager = [AdMemoryManager appMemoryManager];
cutoff = aDouble;
buffer = 1.0;
updateInterval = anInt;
epsilon1 = e1;
epsilon2 = e2;
kappa = k;
electrostaticConstant = PI4EP_R/epsilon1;
[self _calculateGRFParameters];
if(aClass == nil)
aClass = [AdCellListHandler class];
if(![aClass isSubclassOfClass: [AdListHandler class]])
[NSException raise: NSInvalidArgumentException
format: @"Supplied list handler class, %@, is not a subclass of AdListHandler",
NSStringFromClass(aClass)];
listHandlerClass = aClass;
if(aSystem != nil)
{
system = [aSystem retain];
[self _determineLJType];
//Retrieve element coordinates
coordinates = [system coordinates];
if(coordinates == NULL)
{
[self release];
[NSException raise: NSInvalidArgumentException
format: @"Coordinates cannot be NULL"];
}
//Check the element properties contain the required parameters
elementProperties = [system elementProperties];
[elementProperties retain];
if(![self _checkMatrix: elementProperties containsParametersForType: lennardJonesType])
{
[self release];
NSWarnLog(@"Requried properties not present in - %@", [elementProperties columnHeaders]);
[NSException raise: NSInvalidArgumentException
format: @"Properites matrix does not contain correct parameters for LJ type %@"
,lennardJonesType];
}
[self _initialiseParameters];
//Create handler
listHandler = [[aClass alloc]
initWithSystem: system
allowedPairs: nil
cutoff: cutoff + buffer];
[listHandler setDelegate: self];
messageId = [[NSProcessInfo processInfo] globallyUniqueString];
[messageId retain];
[[AdMainLoopTimer mainLoopTimer]
sendMessage: @selector(update)
toObject: listHandler
interval: updateInterval
name: messageId];
if(nonbondedPairs == nil)
nonbondedPairs = [system indexSetArrayForCategory:@"Nonbonded"];
[self setNonbondedPairs: nonbondedPairs];
if(matrix == NULL)
{
usingExternalForceMatrix = NO;
forces = [memoryManager allocateMatrixWithRows: coordinates->no_rows
withColumns: 3];
}
else
{
if(matrix->no_rows != coordinates->no_rows)
{
[self release];
[NSException raise: NSInvalidArgumentException
format: @"Force matrix has incorrect number of rows"];
}
if(matrix->no_columns != 3)
{
[self release];
[NSException raise: NSInvalidArgumentException
format: @"Force matrix has incorrect number of columns"];
}
forces = matrix;
usingExternalForceMatrix = YES;
}
[self _precomputeParameters];
}
}
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter]
removeObserver: self];
[pairs release];
[listHandler release];
[elementProperties release];
[lennardJonesType release];
[memoryManager freeArray: partialCharges];
[memoryManager freeMatrix: parameters];
if(!usingExternalForceMatrix)
[memoryManager freeMatrix: forces];
[system release];
if(messageId != nil)
{
[[AdMainLoopTimer mainLoopTimer]
removeMessageWithName: messageId];
[messageId release];
}
[super dealloc];
}
- (NSString*) description
{
NSMutableString* description = [NSMutableString string];
[description appendFormat:
@"%@. System: %@\n\tCutoff: %5.2lf. Epsilon 1: %5.2lf. Epsilon 2: %5.2lf. Kappa: %5.2lf. Update interval: %d\n",
NSStringFromClass([self class]), [system systemName], cutoff,
epsilon1, epsilon2, kappa, updateInterval];
[description appendFormat: @"\t%@", [listHandler description]];
return description;
}
/*
* Force & Potential Calculation
*/
- (void) evaluateForces;
{
ListElement* list_p;
AdMatrix* coordinates;
coordinates = [system coordinates];
if(interactionList == NULL)
{
if(system != nil && pairs != nil)
{
[self setNonbondedPairs:
[system indexSetArrayForCategory:@"Nonbonded"]];
}
else
return;
}
//May be quicker to get the number of nonbonded interactions and then use a for loop here
vdwPotential = 0;
estPotential = 0;
list_p = interactionList->next;
if([lennardJonesType isEqual: @"A"])
{
while(list_p->next != NULL)
{
AdGRFCoulombAndLennardJonesAForce(list_p,
coordinates->matrix,
forces->matrix,
electrostaticConstant,
cutoff,
b0,
b1,
&vdwPotential,
&estPotential);
list_p = list_p->next;
}
}
else
{
while(list_p->next != NULL)
{
AdGRFCoulombAndLennardJonesBForce(list_p,
coordinates->matrix,
forces->matrix,
electrostaticConstant,
cutoff,
b0,
b1,
&vdwPotential,
&estPotential);
list_p = list_p->next;
}
}
}
- (void) evaluateLennardJonesForces
{
NSWarnLog(@"Method (%@) not implemented", NSStringFromSelector(_cmd));
}
- (void) evaluateElectrostaticForces
{
NSWarnLog(@"Method (%@) not implemented", NSStringFromSelector(_cmd));
}
- (void) evaluateEnergy;
{
ListElement* list_p;
AdMatrix* coordinates;
coordinates = [system coordinates];
if(interactionList == NULL)
{
/*
* If system and pairs are not nil the list was invalidated by receipt of
* an AdSystemContentsDidChangeNotification. In this case we rebuild it
* using a newly acquired pair array
*/
if(system != nil && pairs != nil)
{
[self setNonbondedPairs:
[system indexSetArrayForCategory:@"Nonbonded"]];
}
else
return;
}
vdwPotential = 0;
estPotential = 0;
if([lennardJonesType isEqual: @"A"])
{
list_p = interactionList->next;
while(list_p->next != NULL)
{
AdGRFCoulombAndLennardJonesAEnergy(list_p,
coordinates->matrix,
electrostaticConstant,
cutoff,
b0,
b1,
&vdwPotential,
&estPotential);
list_p = list_p->next;
}
}
else
{
list_p = interactionList->next;
while(list_p->next != NULL)
{
AdGRFCoulombAndLennardJonesBEnergy(list_p,
coordinates->matrix,
electrostaticConstant,
cutoff,
b0,
b1,
&vdwPotential,
&estPotential);
list_p = list_p->next;
}
}
}
/*
* List Handler Delegate Methods
*/
- (void) handlerDidUpdateList: (AdListHandler*) handler
{
[self _precomputeParameters];
}
- (void) handlerDidInvalidateList: (AdListHandler*) handler
{
interactionList = NULL;
}
- (void) handlerDidHandleContentChange: (AdListHandler*) handler
{
int numberOfElements;
NSDebugLLog(@"AdGRFNonbondedTerm",
@"Received handlerDidHandleContentChange message");
NSDebugLLog(@"AdGRFNonbondedTerm",
@"Updating affected variables");
//Free affected instance variables
[elementProperties release];
[memoryManager freeArray: partialCharges];
[memoryManager freeMatrix: parameters];
//Reaquire necessary information
NSDebugLLog(@"AdGRFNonbondedTerm",
@"Reinitialising parameters");
numberOfElements = [system numberOfElements];
elementProperties = [[system elementProperties] retain];
[self _initialiseParameters];
if(!usingExternalForceMatrix)
{
NSDebugLLog(@"AdGRFNonbondedTerm",
@"Recreating force matrix");
[memoryManager freeMatrix: forces];
forces = [memoryManager allocateMatrixWithRows: numberOfElements
withColumns: 3];
}
/*
* Reset allowed pairs
* We have to use the allowed pairs supplied by
* indexSetArrayForCategory since we dont know
* if any user supplied pair list is still valid.
*/
NSDebugLLog(@"AdGRFNonbondedTerm",
@"Aqurining nonbonded pairs from system");
[pairs release];
pairs = [system indexSetArrayForCategory: @"Nonbonded"];
[pairs retain];
NSDebugLLog(@"AdGRFNonbondedTerm",
@"Updating list handler with new pairs");
[listHandler setAllowedPairs: pairs];
//Recreate list
NSDebugLLog(@"AdGRFNonbondedTerm", @"Recreating list");
[listHandler createList];
interactionList = [[listHandler pairList] pointerValue];
NSDebugLLog(@"AdGRFNonbondedTerm", @"Precomputing parameters");
[self _precomputeParameters];
NSDebugLLog(@"AdGRFNonbondedTerm", @"Update complete");
}
/*
* Accessors
*/
- (double) electrostaticEnergy
{
return estPotential;
}
- (double) lennardJonesEnergy
{
return vdwPotential;
}
- (double) energy
{
return estPotential + vdwPotential;
}
- (NSString*) lennardJonesType
{
return [[lennardJonesType retain]
autorelease];
}
- (double) cutoff
{
return cutoff;
}
- (void) setCutoff: (double) aDouble
{
cutoff = aDouble;
[self _calculateGRFParameters];
if(listHandler != nil)
[listHandler setCutoff: cutoff + buffer];
}
- (unsigned int) updateInterval
{
return updateInterval;
}
- (void) setUpdateInterval: (unsigned int) anInt
{
updateInterval = anInt;
if(listHandler != nil)
[[AdMainLoopTimer mainLoopTimer]
resetIntervalForMessageWithName: messageId
to: anInt];
}
- (void) updateList: (BOOL) reset
{
[listHandler update];
if(reset)
[[AdMainLoopTimer mainLoopTimer]
resetCounterForMessageWithName: messageId];
}
- (void) setExternalForceMatrix: (AdMatrix*) matrix
{
int numberOfElements;
numberOfElements = [system numberOfElements];
//Check matrix has correct dimensions
if(matrix == NULL)
[NSException raise: NSInvalidArgumentException
format: @"Matrix cannot be NULL"];
else if(matrix->no_rows != numberOfElements)
[NSException raise: NSInvalidArgumentException
format: @"Matrix has incorrect number of rows (%d - required %d)",
matrix->no_rows, numberOfElements];
else if(matrix->no_columns != 3)
[NSException raise: NSInvalidArgumentException
format: @"Matrix has incorrect number of columns"];
if(!usingExternalForceMatrix)
{
[memoryManager freeMatrix: forces];
usingExternalForceMatrix = YES;
}
forces = matrix;
}
- (AdMatrix*) forces
{
return forces;
}
- (void) clearForces
{
int i,j;
for(i=0; i<forces->no_rows; i++)
for(j=0; j<3; j++)
forces->matrix[i][j] = 0;
}
/**
Returns YES if the object writes its forces to an external
matrix. NO otherwise.
*/
- (BOOL) usesExternalForceMatrix
{
return usingExternalForceMatrix;
}
/**
Sets the system the term should be calculated on.
*/
- (void) setSystem: (id) anObject
{
int numberOfElements;
//Clear all system related variables
if(system != nil)
{
[elementProperties release];
[memoryManager freeArray: partialCharges];
[memoryManager freeMatrix: parameters];
if(!usingExternalForceMatrix)
[memoryManager freeMatrix: forces];
[system release];
}
system = [anObject retain];
if(system != nil)
{
[self _determineLJType];
numberOfElements = [system numberOfElements];
elementProperties = [[system elementProperties] retain];
usingExternalForceMatrix = NO;
forces = [memoryManager allocateMatrixWithRows: numberOfElements
withColumns: 3];
//Require parameters and partial charges
[self _initialiseParameters];
//Update handler
if(listHandler == nil)
{
listHandler = [[listHandlerClass alloc]
initWithSystem: system
allowedPairs: nil
cutoff: cutoff + buffer];
[listHandler setDelegate: self];
messageId = [[NSProcessInfo processInfo]
globallyUniqueString];
[messageId retain];
[[AdMainLoopTimer mainLoopTimer]
sendMessage: @selector(update)
toObject: listHandler
interval: updateInterval
name: messageId];
}
[listHandler setSystem: system];
[self setNonbondedPairs:
[system indexSetArrayForCategory: @"Nonbonded"]];
}
}
/**
Returns the system the object operates on.
*/
- (id) system
{
return [[system retain] autorelease];
}
/**
Returns YES if the term can calculate its energy.
*/
- (BOOL) canEvaluateEnergy
{
return YES;
}
/**
Return YES if the term can calculate forces.
*/
- (BOOL) canEvaluateForces
{
return YES;
}
- (void) setNonbondedPairs: (NSArray*) nonbondedPairs
{
//Cant specify pairs if there is no system
if(system == nil)
return;
if(pairs != nil)
[pairs release];
pairs = [nonbondedPairs retain];
[listHandler setAllowedPairs: pairs];
[listHandler createList];
interactionList = [[listHandler pairList] pointerValue];
[self _precomputeParameters];
}
- (NSArray*) nonbondedPairs
{
return [[pairs retain] autorelease];
}
- (double) espilonOne
{
return epsilon1;
}
- (void) setEspilonOne: (double) value
{
epsilon1 = value;
[self _calculateGRFParameters];
}
- (double) espilonTwo
{
return epsilon2;
}
- (void) setEspilonTwo: (double) value
{
epsilon2 = value;
[self _calculateGRFParameters];
}
- (double) kappa
{
return kappa;
}
- (void) setKappa: (double) value
{
kappa = value;
[self _calculateGRFParameters];
}
@end
|