39 void* ptr = malloc(size);
42 perror(
"malloc error");
48 static void*
Realloc(
void* src,
size_t size)
52 void* ptr = realloc(src, size);
55 perror(
"realloc error");
61 static void*
Calloc(
size_t size,
size_t elem)
63 void* ptr = calloc(size, elem);
66 perror(
"calloc error");
74 memset(dest, 0,
sizeof(
BigReal)*num_constraints);
75 for(
int i = 0; i < 6*num_constraints; i++)
80 dest[row] += B->
aij[i] * src[col];
86 memset(dest, 0,
sizeof(
BigReal)*num_atoms*3);
87 for(
int i = 0; i < 6*num_constraints; i++)
92 dest[row] += B->
aij[i] * src[col];
100 for(
int i = 0; i < num_atoms; ++i)
107 multiplyR(dest, tmp);
113 for(
int i = 0; i < num_constraints; ++i)
114 dest[i] = src1[i]+a*src2[i];
120 for(
int i = 0; i < num_constraints; ++i)
134 memcpy(p, r,
sizeof(
BigReal)*num_constraints);
135 for(
int i = 0; i < maxLoops; ++i)
139 BigReal alpha = r2_old/vecDot(p, tmp);
141 vecAdd(r,r,tmp,-alpha);
172 static int intCmp(
const void* a1,
const void* a2)
184 int LincsSolver::Map(
int i)
186 int* ptr = (
int*)bsearch(&i, globalIndex, num_atoms,
sizeof(
int), &
intCmp);
189 fprintf(stderr,
"error mapping for %d\n", i);
192 return (
int)((size_t)(ptr-globalIndex));
212 for(
int i = 0; i < 3; ++i)
220 int LincsSolver::partition(
int left,
int right)
222 int pivot = globalIndex[right];
225 for (
int j = left; j <= right- 1; j++)
229 if (globalIndex[j] <= pivot)
239 swapInt(globalIndex, i+1, right);
247 void LincsSolver::sortAtom(
int left,
int right)
251 int pi = partition(left, right);
254 sortAtom(left, pi-1);
255 sortAtom(pi+1, right);
259 void LincsSolver::sortConstraints()
264 void LincsSolver::showDataRead(
void* Data,
void* Constraints)
269 fprintf(stdout,
"Atom data:\n");
270 for(
int i = 0; i < num_atoms; ++i)
271 fprintf(stdout,
"%d %f %f %f\n", data[i].globalIndex, data[i].posx, data[i].posy, data[i].posz);
273 fprintf(stdout,
"\n");
274 for(
int i = 0; i < num_atoms; ++i)
275 fprintf(stdout,
"%d %f %f %f\n", globalIndex[i], ref[3*i+0], ref[3*i+1], ref[3*i+2]);
277 fprintf(stdout,
"\nconstraints:\n");
278 for(
int i = 0; i < num_constraints; ++i)
279 fprintf(stdout,
"%d %d %f\n", constraints[i].i, constraints[i].j, constraints[i].dist);
281 fprintf(stdout,
"\n");
282 for(
int i = 0; i < num_constraints; ++i)
283 fprintf(stdout,
"%d %d %f\n", constraintList[i].i, constraintList[i].j, constraintList[i].dist);
287 void LincsSolver::checkConvergence()
289 for(
int idx = 0; idx < num_constraints; ++idx)
291 int i = constraintList[idx].
i;
292 int j = constraintList[idx].
j;
296 BigReal x1, x2, y1, y2, z1, z2;
303 BigReal r = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2));
307 void LincsSolver::copy(
void* Data,
void* Constraints)
312 globalIndex = (
int*)
Malloc(
sizeof(
int)*size);
313 patchID = (
int*)
Malloc(
sizeof(
int)*size);
314 offset = (
int*)
Malloc(
sizeof(
int)*size);
322 patchID [count] = data->
patchID;
323 offset [count] = data->
offset;
325 ref [3*count+0] = data->
posx;
326 ref [3*count+1] = data->
posy;
327 ref [3*count+2] = data->
posz;
334 globalIndex = (
int*)
Realloc(globalIndex,
sizeof(
int)*size);
335 patchID = (
int*)
Realloc(patchID,
sizeof(
int)*size);
336 offset = (
int*)
Realloc(offset,
sizeof(
int)*size);
342 globalIndex = (
int*)
Realloc(globalIndex,
sizeof(
int)*num_atoms);
343 patchID = (
int*)
Realloc(patchID,
sizeof(
int)*num_atoms);
344 offset = (
int*)
Realloc(offset,
sizeof(
int)*num_atoms);
352 while(constraints->
i > -1)
355 int i = constraints->
i;
356 int j = constraints->
j;
363 constraintList[count].
i = i;
364 constraintList[count].
j = j;
365 constraintList[count++].
dist = (constraints++)->dist;
373 num_constraints = count;
378 void LincsSolver::buildBMatrix()
382 B->
IA = (
int*)
Malloc(
sizeof(
int)*6*num_constraints);
383 B->
JA = (
int*)
Malloc(
sizeof(
int)*6*num_constraints);
384 for(
int idx = 0; idx < num_constraints; ++idx)
386 int i = constraintList[idx].
i;
387 int j = constraintList[idx].
j;
391 for(
int k = 0; k < 6; ++k)
392 B->
IA[6*idx+k] = idx;
393 for(
int k = 0; k < 3; ++k)
395 B->
JA[6*idx+k] = 3*col_i+k;
396 B->
JA[6*idx+3+k] = 3*col_j+k;
408 d2 = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2);
410 B->
aij[6*idx+0] = (x1-x2)/d2;
411 B->
aij[6*idx+1] = (y1-y2)/d2;
412 B->
aij[6*idx+2] = (z1-z2)/d2;
413 B->
aij[6*idx+3] = -B->
aij[6*idx+0];
414 B->
aij[6*idx+4] = -B->
aij[6*idx+1];
415 B->
aij[6*idx+5] = -B->
aij[6*idx+2];
422 copy(data, constraints);
423 sortAtom(0, num_atoms-1);
427 showDataRead(data, constraints);
437 for(
int i = 0; i < num_constraints; ++i)
438 b[i] -= constraintList[i].dist;
439 memset(x, 0 ,
sizeof(
BigReal)*num_constraints);
440 conjugateGradient(x, b);
443 for(
int i = 0; i < num_atoms; ++i)
446 ref[3*i+0] -= tmp[3*i+0]*m;
447 ref[3*i+1] -= tmp[3*i+1]*m;
448 ref[3*i+2] -= tmp[3*i+2]*m;
451 for(
int idx = 0; idx < num_constraints; ++idx)
453 int i = constraintList[idx].
i;
454 int j = constraintList[idx].
j;
458 BigReal x1, x2, y1, y2, z1, z2;
465 BigReal l = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2);
467 b[idx] -= sqrt(2.0*d*d-l);
469 memset(x, 0 ,
sizeof(
BigReal)*num_constraints);
470 conjugateGradient(x, b);
472 for(
int i = 0; i < num_atoms; ++i)
475 ref[3*i+0] -= tmp[3*i+0]*m;
476 ref[3*i+1] -= tmp[3*i+1]*m;
477 ref[3*i+2] -= tmp[3*i+2]*m;
486 void LincsSolver::destroy()
500 if(globalIndex != NULL)
503 if(constraintList != NULL)
504 free(constraintList);
505 constraintList = NULL;
static void * Realloc(void *src, size_t size)
static int intCmp(const void *a1, const void *a2)
static void swapBigReal(BigReal *v, int a, int b)
void solve()
interface to run the solver with CG.
static void swapInt(int *v, int a, int b)
void setUpSolver(atomData *, constraintTuple *)
interface to set up the solver with two given arrays of data.
static void swapVector3(BigReal *v, int a, int b)
static int intPairCmp(const void *a1, const void *a2)
void NAMD_die(const char *err_msg)
static void * Calloc(size_t size, size_t elem)
static void * Malloc(size_t size)
struct constraintTuple constraintTuple