File: AdunDynamics.m

package info (click to toggle)
adun.app 0.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 6,824 kB
  • ctags: 713
  • sloc: objc: 49,683; ansic: 4,680; sh: 523; python: 79; makefile: 67; cpp: 33
file content (516 lines) | stat: -rwxr-xr-x 11,895 bytes parent folder | download
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
/*
   Project: Adun

   Copyright (C) 2005 Michael Johnston & Jordi Villa-Freixa

   Author: 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/AdunDynamics.h"
#include "AdunKernel/AdDataSources.h"

@implementation AdDynamics

/*
 * Accessing Data Source & Matrix Creation
 */

- (void) _initialiseVelocities
{
	int i, j;
	double var, hold;
	
	NSDebugLLog(@"AdDynamics", @"Randomly initialising velocities.");

	/*
   	 * Draw velocities from a Maxwell Distribution at the given temperature
	 * Maxwell Distribution is a gaussian with variance = sqrt(KB*T/mass)
	 * Catch when the temperature is set to zero
  	 * Not sure what the temperature limit should be..
	 * FIXME: We're assuming units here by using KB.
	 */

	if(targetTemperature > 0.001)
	{
		var = targetTemperature*KB;
		for(i=0; i<numberOfElements; i++)
		{
			hold = sqrt(var/masses[i]);		
			for(j=0; j<3; j++)
				velocities->matrix[i][j] = gsl_ran_gaussian(twister, hold);
		}
	}
	else
		for(i=0; i<numberOfElements; i++)
			for(j=0; j<3; j++)
				velocities->matrix[i][j] = 0;
}

- (void) _retrieveMasses
{
	int i;

	elementMasses = [[[dataSource elementProperties] 
				columnWithHeader: @"Mass"] copy];
	masses = [memoryManager allocateArrayOfSize: 	
			[elementMasses count]*sizeof(double)];

	for(i=0; i<(int)[elementMasses count]; i++)  	
		 masses[i] = [[elementMasses objectAtIndex: i] 	 
					 doubleValue]; 	 
}

- (void) _retrieveCoordinates
{
	NSDebugLLog(@"AdDynamics", @"Accesing data source for coordinates");
	coordinates = [[dataSource elementConfiguration]
			cRepresentation];
	numberOfElements = coordinates->no_rows;
	NSDebugLLog(@"AdDynamics", @"Number of coordinates = %d\n", numberOfElements);
	coordinatesLock = [NSLock new];
}

- (void) _retrieveAtomTypes
{
	NSDebugLLog(@"AdDynamics", @"Accesing data source for elementTypes");
	elementTypes = [[[dataSource elementProperties] 
			columnWithHeader: @"ForceFieldName"] copy];
}

/*
 * Initialisation
 */

- (id) initWithDataSource: (id) anObject
	targetTemperature: (double) initialTemperature
	seed: (int) rngSeed
	removeTranslationalDOF: (BOOL) value
{
	if((self = [super init]))
	{
		if(anObject ==  nil)
			[NSException raise: NSInvalidArgumentException
				format: @"Data source cannot be nil"];

		dataSource = anObject;
		if(initialTemperature < 0.0)
		{
			NSWarnLog(@"Target temperature cannot be negative.");
			NSWarnLog(@"Defaulting to 300");
			targetTemperature = 300;
		}
		else
			targetTemperature = initialTemperature;

		seed = rngSeed;
		velocitiesLock = coordinatesLock = nil;
	
		twister = gsl_rng_alloc(gsl_rng_mt19937);
		gsl_rng_set(twister, seed);

		memoryManager = [AdMemoryManager appMemoryManager];
		masses = NULL;

		[self _retrieveCoordinates];
		[self _retrieveAtomTypes];
		[self _retrieveMasses];

		velocities = [memoryManager allocateMatrixWithRows: numberOfElements 
				withColumns: 3];	
		velocitiesLock = [NSLock new];
		[self _initialiseVelocities];

		[self calculateCentreOfMass];

		degreesOfFreedom = 3*numberOfElements;
		if(value)
			[self removeTranslationalDOF];

		[self calculateCentreOfMass];
		kineticEnergyToTemperature =  (2*KB_1)/(degreesOfFreedom);
		kineticEnergy = AdCalculateKineticEnergy(velocities, masses);
		temperature = kineticEnergyToTemperature*kineticEnergy;
	}

	return self;
}

- (id) initWithDataSource: (id) anObject
{
	return [self initWithDataSource: anObject
		targetTemperature: 300.0
		seed: 500
		removeTranslationalDOF: YES];
}

- (id) init
{
	return [self initWithDataSource: nil];
}

- (void) dealloc
{
	gsl_rng_free(twister);
	[elementTypes release];
	[elementMasses release];
	[coordinatesLock release];
	[velocitiesLock release];
	[memoryManager freeMatrix: coordinates];
	[memoryManager freeMatrix: velocities];
	[memoryManager freeArray: masses];
	[super dealloc];
}

/*
 * Public Methods
 */

- (void) calculateCentreOfMass
{
	int i, j;
	
	for(i=0; i<3;i++)
		centreOfMass.vector[i] = 0;
	
	for(totalMass = 0, i=0; i<numberOfElements; i++)
	{
		totalMass += masses[i];
		for(j=0; j<3;j++)
			centreOfMass.vector[j] += coordinates->matrix[i][j]*masses[i];
	}

	for(i=0; i<3; i++)
		centreOfMass.vector[i] = centreOfMass.vector[i]/totalMass;

	Ad3DVectorLength(&centreOfMass);

	NSDebugLLog(@"AdDynamics", @"Mass %lf. Centre of Mass is: %-6.2lf%-6.2lf%-6.2lf", 
			totalMass, centreOfMass.vector[0], centreOfMass.vector[1], centreOfMass.vector[2]);
}

- (void) removeTranslationalDOF
{
	AdRemoveTranslationalDOF(velocities, masses);
	degreesOfFreedom = 3*numberOfElements - 3;;
	kineticEnergyToTemperature =  (2*KB_1)/(degreesOfFreedom);
}

- (void) moveCentreOfMassToOrigin
{	
	int i,j;

	NSDebugLLog(@"AdDynamics", @"Moving centreOfMass of mass to (0,0,0)");
	[self calculateCentreOfMass];

	for(i=0; i<numberOfElements; i++)
		for(j=0; j<3; j++)
			coordinates->matrix[i][j] -= centreOfMass.vector[j];

	[self calculateCentreOfMass];
}

- (void) centreOnPoint: (double*) point
{
	int i,j;

	NSDebugLLog(@"AdDynamics", @"Moving center of mass to point %-6.2lf%-6.2lf%-6.2lf", 
		point[0], 
		point[1], 
		point[2]);

	[self moveCentreOfMassToOrigin];

	for(i=0; i<numberOfElements; i++)
		for(j=0; j<3; j++)
			coordinates->matrix[i][j] += point[j];
	[self calculateCentreOfMass];
}

- (void) centreOnElement: (int) elementIndex
{
	int i,j;
	double position[3];

	NSDebugLLog(@"AdDynamics", @"Moving element %d (%-6.2lf%-6.2lf%-6.2lf) to origin", elementIndex,
		coordinates->matrix[elementIndex][0],
		coordinates->matrix[elementIndex][1], 
		coordinates->matrix[elementIndex][2]);
	
	position[0] = coordinates->matrix[elementIndex][0];
	position[1] = coordinates->matrix[elementIndex][1];
	position[2] = coordinates->matrix[elementIndex][2];

	for(i=0; i<numberOfElements; i++)
		for(j=0; j<3; j++)
			coordinates->matrix[i][j] -= position[j];
			
	[self calculateCentreOfMass];
}

- (void) reinitialiseVelocities
{
	//Aquire a lock before writing to matrix
	NSDebugLLog(@"AdDynamics", @"Reinitialising velocities");
	[velocitiesLock lock];
	[self _initialiseVelocities];
	kineticEnergy = AdCalculateKineticEnergy(velocities, masses);
	temperature = kineticEnergyToTemperature*kineticEnergy;
	NSDebugLLog(@"AdDynamics", @"New kinetic energy %lf", kineticEnergy);
	NSDebugLLog(@"AdDynamics", @"New temperature %lf", temperature);
	[velocitiesLock unlock];
}

/*
 * Accessors 
 */

- (unsigned int) numberOfElements
{
	return numberOfElements;
}

- (unsigned int) degreesOfFreedom
{
	return degreesOfFreedom;
}

- (NSArray*) elementTypes
{
	return [[elementTypes retain]
			autorelease];
}

- (NSArray*) elementMasses
{
	return [[elementMasses retain]
			autorelease];
}

- (double) kineticEnergy
{
	return kineticEnergy;
}

- (double) temperature
{
	return temperature;
}

- (AdMatrix*) coordinates
{
	return coordinates;
}

- (AdMatrix*) velocities
{
	return velocities;
}

- (void) setCoordinates: (AdMatrix*) matrix
{
	if(coordinates == NULL)
		return;

	[coordinatesLock lock];
	AdCopyAdMatrixToAdMatrix(matrix, coordinates);
	[coordinatesLock unlock];
}

- (void) setVelocities: (AdMatrix*) matrix
{
	if(velocities == NULL)
		return;

	[velocitiesLock lock];
	AdCopyAdMatrixToAdMatrix(matrix, velocities);
	kineticEnergy = AdCalculateKineticEnergy(velocities, masses);
	temperature = kineticEnergyToTemperature*kineticEnergy;
	[velocitiesLock unlock];
}

- (double) totalMass
{
	return totalMass;
}

- (Vector3D) centreOfMass
{
	//Centre may have moved
	[self calculateCentreOfMass];
	return centreOfMass;
}

- (double) targetTemperature
{
	return targetTemperature;
}

- (int) seed
{
	return seed;
}

- (void) setSeed: (int) aNumber
{
	seed = aNumber;
	gsl_rng_set(twister, seed);
}

- (void) setTargetTemperature: (double) aNumber
{
	if(targetTemperature < 0.0)
		[NSException raise: NSInvalidArgumentException
			format: @"Temperature cannot be negative"];
		
	targetTemperature = aNumber;
}

- (id) dataSource
{
	return dataSource;
}

//AdMatrixModification

- (BOOL) allowsDirectModificationOfMatrix: (AdMatrix*) matrix;
{
	if(matrix == coordinates || matrix == velocities)
		return YES;
	else
		return NO;
}

/**
We try the lock for the matrix. If we dont
acquire it we return NO. If we do aquire we
immediatly unlock it and return YES.
*/
- (BOOL) matrixIsAvailableForModification: (AdMatrix*) matrix;
{
	NSLock* lock;

	if(matrix == coordinates)
		lock = coordinatesLock;
	else if(matrix == velocities)
		lock = velocitiesLock;
	else
		return NO;
	
	if([lock tryLock])
	{
		[lock unlock];
		return YES;
	}
	else
		return NO;

}

- (void) object: (id) object willBeginWritingToMatrix: (AdMatrix*) matrix;
{
	if(matrix == coordinates)
	{
		[coordinatesLock lock];
	}
	else if(matrix == velocities)
	{
		[velocitiesLock lock];
	}

	return;
}

- (void) object: (id) object didFinishWritingToMatrix: (AdMatrix*) matrix;
{
	if(matrix == coordinates)
	{
		[coordinatesLock unlock];
	}
	else if(matrix == velocities)
	{
		//Update kinetic energy and temperature after the write to velocities
		kineticEnergy = AdCalculateKineticEnergy(velocities, masses);
		temperature = kineticEnergyToTemperature*kineticEnergy;
		[velocitiesLock unlock];
	}
}

//NSCoding Methods

- (id) initWithCoder: (NSCoder*) decoder
{	
	AdDataMatrix* matrix;

	if([decoder allowsKeyedCoding])
	{
		memoryManager = [AdMemoryManager appMemoryManager];
		seed = [decoder decodeIntForKey: @"Seed"];
		targetTemperature = [decoder decodeDoubleForKey: @"TargetTemperature"];
		degreesOfFreedom = [decoder decodeIntForKey: @"DegreesOfFreedom"];
		dataSource = [decoder decodeObjectForKey: @"DataSource"];
		kineticEnergyToTemperature =  (2*KB_1)/(degreesOfFreedom);

		matrix = [decoder decodeObjectForKey: @"Coordinates"];
		coordinates = [matrix cRepresentation];
		matrix = [decoder decodeObjectForKey: @"Velocities"];
		velocities = [matrix cRepresentation];

		numberOfElements = coordinates->no_rows;
		coordinatesLock = [NSLock new];
		velocitiesLock = [NSLock new];

		[self _retrieveAtomTypes];
		[self _retrieveMasses];
		[self calculateCentreOfMass];

		kineticEnergy = AdCalculateKineticEnergy(velocities, masses);
		temperature = kineticEnergyToTemperature*kineticEnergy;

		twister = gsl_rng_alloc(gsl_rng_mt19937);
		gsl_rng_set(twister, seed);
}
	else
		[NSException raise: NSInvalidArgumentException 
			format: @"%@ does not support non keyed coding", [self classDescription]];

	return self;
}

- (void) encodeWithCoder: (NSCoder*) encoder
{
	AdDataMatrix* matrix;

	if([encoder allowsKeyedCoding])
	{
		NSDebugLLog(@"Encode", @"Encoding %@", [self description]);
		[encoder encodeInt: seed forKey: @"Seed"];
		[encoder encodeDouble: targetTemperature forKey: @"TargetTemperature"];
		[encoder encodeInt: degreesOfFreedom forKey: @"DegreesOfFreedom"];
		[encoder encodeConditionalObject: dataSource forKey: @"DataSource"];

		matrix = [AdDataMatrix matrixFromADMatrix: coordinates];
		[encoder encodeObject: matrix forKey: @"Coordinates"];	
		matrix = [AdDataMatrix matrixFromADMatrix: velocities];
		[encoder encodeObject: matrix forKey: @"Velocities"];	
		NSDebugLLog(@"Encode", @"Complete %@", [self description]);
	}
	else
		[NSException raise: NSInvalidArgumentException
			format: @"%@ class does not support non keyed coding", [self class]];
}

@end