File: AdFrameworkFunctions.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 (316 lines) | stat: -rw-r--r-- 7,780 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
#include "AdunKernel/AdFrameworkFunctions.h"

NSError* AdCreateError(NSString* domain, int code, NSString* localizedDescription,
	NSString* detailedDescription,
	NSString* recoverySuggestion)
{
	NSMutableDictionary* errorInfo;
	NSError* error;

	errorInfo = [NSMutableDictionary dictionary];

	if(localizedDescription != nil)
		[errorInfo setObject: localizedDescription 
			forKey: NSLocalizedDescriptionKey];

	if(detailedDescription != nil)
		[errorInfo setObject: detailedDescription 
			forKey: @"AdDetailedDescriptionKey"];

	if(recoverySuggestion != nil)		
		[errorInfo setObject: recoverySuggestion
			forKey: @"NSRecoverySuggestionKey"];

	if([errorInfo count] == 0)
		errorInfo = nil;

	error = [NSError errorWithDomain: domain
			code: code
			userInfo: errorInfo];

	return error;		
}

NSError* AdKnownExceptionError(int code, NSString* localizedDescription,
	NSString* detailedDescription,
	NSString* recoverySuggestion)
{
	return AdCreateError(AdunKernelErrorDomain,
		code,
		localizedDescription,
		detailedDescription,
		recoverySuggestion);
}

inline void AdRemoveTranslationalDOF(AdMatrix* velocities, double* masses)
{
	int i, j;
	double total_mass;
	double centre[3];
	
	/*
	 * Remove the translational degrees of freedom
	 * by subtracting the centre of mass velocity.
	 * This sets the inital momentum to zero.
	 */

	for(i=0; i<3; i++)
		centre[i] = 0;

	for(total_mass = 0, i=0; i<velocities->no_rows; i++)
	{
		total_mass += masses[i];		

		for(j=0; j<3; j++)
			centre[j] += velocities->matrix[i][j]*masses[i];
	}

	for(i=0; i<3; i++)
		centre[i] = centre[i]/total_mass;	

	for(i=0; i<velocities->no_rows; i++)
		for(j=0; j<3; j++)
			velocities->matrix[i][j] -= centre[j];
}

Vector3D Ad3DVectorFromNSArray(NSArray* array)
{
	int i;
	Vector3D vector;

	if([array count] != 3)
	{
		NSWarnLog(@"Cannot convert to Vector3D - Incorrect number of elements");
		array = [NSArray arrayWithObjects: 
				[NSNumber numberWithDouble: 0.0],
				[NSNumber numberWithDouble: 0.0],
				[NSNumber numberWithDouble: 0.0],
				nil];
	}	

	for(i=0; i<3; i++)
		vector.vector[i] = [[array objectAtIndex: i] doubleValue];

	return vector;	
}

void AdWriteMatrixToFile(AdMatrix* matrix, NSString* fileName, NSString* fileFlag)
{
	int i, j;
	FILE* stream;

	if(matrix == NULL)
		[NSException raise: NSInvalidArgumentException
			format: @"Matrix cannot be NULL"];

	if(fileName == nil)
		fileName = @"matrix.out";

	if(fileFlag == nil)
		fileFlag = @"w";

	//open the file
	stream = fopen([fileName cString], [fileFlag cString]);
	for(i=0; i<matrix->no_rows; i++)
	{
		for(j=0; j<matrix->no_columns; j++)
			fprintf(stream, "%-12E ", matrix->matrix[i][j]);
		fprintf(stream, "\n");
	}

	fclose(stream);
}

void AdLogTimingInformation(struct tms *start, struct tms *end, int steps)
{
	int clockTicks;

	clockTicks = sysconf(_SC_CLK_TCK);

	GSPrintf(stdout, @"Timing information\n");	
	GSPrintf(stdout, @"Clock granularity %lf. Steps %d\n\n", 1.0/clockTicks, steps);
	GSPrintf(stdout, @"%-12@\t%-12@\t%-12@\t%-12@\n",
		@"User", @"System", @"Total", @"Time Per Step");
	end->tms_utime -= start->tms_utime;
	end->tms_stime -= start->tms_stime;
	GSPrintf(stdout, @"%-12.3lf\t%-12.3lf\t%-12.3lf\t%-12.6lf\n",
		((double)end->tms_utime)/clockTicks, 
		((double)end->tms_stime)/clockTicks, 
		((double)(end->tms_utime + end->tms_stime))/clockTicks,
		((double)(end->tms_utime + end->tms_stime))/(clockTicks*steps));
}

void AdLogMemoryUsage(void)
{
#ifndef __FREEBSD__
	struct mallinfo mem_struct;
	float factor = 1048576.0;

	mem_struct = mallinfo();
	NSLog(@"Arena : %lf MB. Hblks : %lf MB. Uordblocks %lf MB. Fordblocks %lf MB", 
		(float)mem_struct.arena/factor,
		(float)mem_struct.hblkhd/factor, 
		(float)mem_struct.uordblks/factor, 
		(float)mem_struct.fordblks/factor);
#else
	NSLog(@"Memory logging not available under FreeBSD");
#endif		
}


void AdLogError(NSError* error)
{
	NSString* string;
	NSError* underlyingError;

	if(error == nil)
	{
		NSWarnLog(@"Supplied nil error");
		return;
	}	

	NSWarnLog(@"Detected error from domain %@", [error domain]);
	NSWarnLog(@"Error code %d", [error code]);
	if((string = [[error userInfo] objectForKey: NSLocalizedDescriptionKey]) !=  nil)
		NSWarnLog(@"Description - %@", string);

	if((string = [[error userInfo] objectForKey: @"AdDetailedDescriptionKey"]) !=  nil)
		NSWarnLog(@"Detail - %@", string );
	
	if((string = [[error userInfo] objectForKey: @"NSRecoverySuggestionKey"]) !=  nil)
		NSWarnLog(@"Recovery suggestion - %@", string);

	if((underlyingError = [[error userInfo] objectForKey: @"NSUnderlyingErrorKey"]) != nil)
	{
		NSWarnLog(@"Underlying error present - Details follow:\n");
		AdLogError(underlyingError);
	}	
}

BOOL AdCheckMatrixDimensions(AdDataMatrix* matrixOne, AdDataMatrix* matrixTwo)
{
	BOOL rows, columns;

	rows = ([matrixOne numberOfRows] == [matrixTwo numberOfRows]) ? YES : NO;
	columns = ([matrixOne numberOfColumns] == [matrixTwo numberOfColumns]) ? YES : NO;
	return rows && columns;
}

gsl_matrix* AdGSLMatrixFromAdMatrix(AdMatrix* matrix)
{
	int i, j;
	gsl_matrix* newMatrix;
	
	newMatrix = gsl_matrix_alloc(matrix->no_rows, matrix->no_columns);

	for(i=0; i<matrix->no_rows; i++)
		for(j=0; j<matrix->no_columns; j++)
		{
			gsl_matrix_set(newMatrix, 
				i, j, 
				matrix->matrix[i][j]);
		}			
	
	return newMatrix;
}

gsl_matrix* AdGSLMatrixFromAdDataMatrix(AdDataMatrix* matrix)
{
	int i, j;
	gsl_matrix* newMatrix;
	AdMatrix* temp;
	
	newMatrix = gsl_matrix_alloc([matrix numberOfRows], [matrix numberOfColumns]);
	temp = [matrix cRepresentation];

	for(i=0; i<(int)[matrix numberOfRows]; i++)
		for(j=0; j<(int)[matrix numberOfColumns]; j++)
		{
			gsl_matrix_set(newMatrix, 
				i, j, 
				temp->matrix[i][j]);
		}			
	
	[[AdMemoryManager appMemoryManager] freeMatrix: temp];
	return newMatrix;
}

void AdLogMatrixRow(int rowNumber, AdMatrix* matrix)
{
	int i;
	NSMutableString* aString = [NSMutableString new];

	[aString appendFormat: @"%d: ", rowNumber];
	for(i=0; i< matrix->no_columns; i++)
		[aString appendFormat: @"%-12lf ", matrix->matrix[rowNumber][i]];
	
	NSLog(@"%@", aString); 
	[aString release];
}

void AdLogMatrixRows(NSIndexSet* indexSet, AdMatrix* matrix)
{
	int index;
	
	index = [indexSet indexGreaterThanIndex: matrix->no_rows - 1];
	if(index != NSNotFound)
		[NSException raise: NSRangeException 
			format: @"Index %d is out of range (%d, %d)", 
				index, 0, matrix->no_rows];

	index = [indexSet firstIndex];
	if(index != NSNotFound)
	{
		do
		{
			AdLogMatrixRow(index, matrix);
		}
		while((index = [indexSet indexGreaterThanIndex: index]) != NSNotFound);
	}
}

NSString* AdTimeStamp(void)
{
	NSDate *date = [NSDate date];
	NSString* stamp;
	NSString* user;
	NSDateFormatter *formatter;
	
	formatter = [[NSDateFormatter alloc] 
			initWithDateFormat: @"%H:%M %d/%m"
			allowNaturalLanguage: NO];

	user = NSUserName();
	if([user isEqual: @""])
		user = @"Unknown";

	stamp = [NSString stringWithFormat: @"%@\t%@ -\n", 
			user, 
			[formatter stringForObjectValue: date]];
	[formatter release];
	return stamp;
}

void AdGSLErrorHandler(const char* reason, const char* file, int line, int gsl_errno)
{
	NSString* reasonObj, *fileObj;
	NSError* error;
	NSException* exception;

	//Convert the char arrays to NSString objects
	reasonObj = [NSString stringWithCString: reason];
	fileObj = [NSString stringWithCString: file];
	
	error = AdCreateError(GSLErrorDomain, gsl_errno,
			[NSString stringWithFormat: 
				@"GSL Error: %@",
				reasonObj],
			[NSString stringWithFormat: 
				@"Function %@. Line: %d",
				fileObj, line], nil);
	exception = [NSException exceptionWithName: NSInternalInconsistencyException
			reason: reasonObj 
			userInfo: [NSDictionary dictionaryWithObject: error 
				forKey: NSUnderlyingErrorKey]]; 
	[exception raise];				
}