summaryrefslogtreecommitdiff
path: root/sim/src/minstructure.c
blob: 032afeea0ac4abab32f9a2356764494dcf94ea13 (plain)
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
// Copyright 2005-2006 Nanorex, Inc.  See LICENSE file for details. 

#include "simulator.h"

static char const rcsid[] = "$Id$";

#define COARSE_TOLERANCE 1e-8
#define FINE_TOLERANCE 1e-10

static struct part *Part;

static void
findRMSandMaxForce(struct configuration *p, double *pRMS, double *pMaxForce)
{
    struct xyz f;
    int i;
    double forceSquared;
    double sum_forceSquared = 0.0;
    double max_forceSquared = -1.0;

    // wware 060109  python exception handling
    NULLPTR(p);
    NULLPTR(pRMS);
    NULLPTR(pMaxForce);
    for (i=0; i<Part->num_atoms; i++) {
        if (Part->atoms[i]->isGrounded) {
            continue;
        }
	f = ((struct xyz *)p->gradient)[i];
	forceSquared = vdot(f,f);
	sum_forceSquared += forceSquared;
	if (forceSquared > max_forceSquared) {
	    max_forceSquared = forceSquared;
	}
    }
    *pRMS = sqrt(sum_forceSquared / Part->num_atoms);
    *pMaxForce = sqrt(max_forceSquared);
}

static FILE *minimizeCountFile;

static void
countMinimizeEvaulations(char which)
{
    if (minimizeCountFile == NULL) {
        minimizeCountFile = fopen("/tmp/minimizecounts", "a");
        if (minimizeCountFile == NULL) {
            perror("/tmp/minimizecounts");
            exit(1);
        }
    }
    fputc(which, minimizeCountFile);
    fflush(minimizeCountFile);
}

// This is the potential function which is being minimized.
static void
minimizeStructurePotential(struct configuration *p)
{
    int i;
    struct jig *jig;
    
    updateVanDerWaals(Part, p, (struct xyz *)p->coordinate);
    p->functionValue = calculatePotential(Part, (struct xyz *)p->coordinate);
    for (i=0; i<Part->num_jigs; i++) {
        jig = Part->jigs[i];
        switch (jig->type) {
        case RotaryMotor:
            p->functionValue +=
                jigMinimizePotentialRotaryMotor(Part, jig,
                                                (struct xyz *)p->coordinate,
                                                p->coordinate + jig->coordinateIndex);
            break;
        case LinearMotor:
            p->functionValue +=
                jigMinimizePotentialLinearMotor(Part, jig,
                                                (struct xyz *)p->coordinate,
                                                p->coordinate + jig->coordinateIndex);
            break;
        default:
            break;
        }
    }
    //writeMinimizeMovieFrame(OutputFile, Part, 0, (struct xyz *)p->coordinate, p->functionValue, p->parameter,
    //                        Iteration++, "potential", p->functionDefinition->message);
    if (DEBUG(D_MINIMIZE_POTENTIAL_MOVIE)) { // -D3
	writeSimpleMovieFrame(Part, (struct xyz *)p->coordinate, NULL, "potential %e %e", p->functionValue, p->parameter);
    }
    if (DEBUG(D_MINIMIZE_PARAMETER_GUESS)) {
        countMinimizeEvaulations('p');
    }
}

static double
clamp(double min, double max, double value)
{
    if (value > max) return max;
    if (value < min) return min;
    return value;
}

static double last_rms_force = 0.0;
static double last_max_force = 0.0;
static FILE *parameterGuessFile = NULL;

// A rotary motor should turn no farther than this during a single
// linear minimization.
#define MAX_RADIANS_PER_STEP 0.4

//#define PLOT_LINEAR_MINIMIZATION

// This is the gradient of the potential function which is being minimized.
static void
minimizeStructureGradient(struct configuration *p)
{
    int i;
    double rms_force;
    double max_force;
    double parameterLimit;
    double motorGradient;
    double plimit;
    struct xyz *forces;
    struct jig *jig;
    struct functionDefinition *fd = p->functionDefinition;

#ifdef PLOT_LINEAR_MINIMIZATION
    double parameter;
    double potential;
    struct xyz *position;
    struct configuration *newLocation;
    int j;
    struct stretch *stretch;
    struct bond *bond;
    double r;
    struct xyz rv;
    double rSquared;
#endif

    updateVanDerWaals(Part, p, (struct xyz *)p->coordinate); BAIL();
    if (DEBUG(D_GRADIENT_FROM_POTENTIAL) || DEBUG(D_GRADIENT_COMPARISON)) { // -D10 || -D18
        fd->gradient_delta = 1e-12; // pm
	evaluateGradientFromPotential(p); BAIL();
        for (i=fd->dimension-1; i>=0; i--) {
            p->gradient[i] *= 1e6; // convert uN to pN
        }
        if (DEBUG(D_MINIMIZE_GRADIENT_MOVIE) && DEBUG(D_GRADIENT_COMPARISON)) { // -D4
            struct xyz offset = { 0.0, 10.0, 0.0 };
            
            forces = (struct xyz *)p->gradient;
            for (i=0; i<Part->num_atoms; i++) {
                writeSimpleForceVectorOffset((struct xyz *)p->coordinate, i, &forces[i], 6, 1.0, offset); // yellow
            }
        }
    }
    if (!DEBUG(D_GRADIENT_FROM_POTENTIAL)) { // ! -D10
	calculateGradient(Part, (struct xyz *)p->coordinate, (struct xyz *)p->gradient);
	BAIL();
    }

    parameterLimit = MAXDOUBLE;
    for (i=0; i<Part->num_jigs; i++) {
        jig = Part->jigs[i];
        switch (jig->type) {
        case RotaryMotor:
            jigMinimizeGradientRotaryMotor(Part, jig,
                                           (struct xyz *)p->coordinate,
                                           (struct xyz *)p->gradient,
                                           p->coordinate + jig->coordinateIndex,
                                           p->gradient + jig->coordinateIndex);
            motorGradient = fabs(*(p->gradient + jig->coordinateIndex));
	    CHECKNAN(motorGradient);
            if (motorGradient < 1e-8) {
                motorGradient = 1e-8;
            }
            plimit = MAX_RADIANS_PER_STEP / motorGradient;
            if (plimit < parameterLimit) {
                parameterLimit = plimit;
            }
            break;
        case LinearMotor:
            jigMinimizeGradientLinearMotor(Part, jig,
                                           (struct xyz *)p->coordinate,
                                           (struct xyz *)p->gradient,
                                           p->coordinate + jig->coordinateIndex,
                                           p->gradient + jig->coordinateIndex);
            break;
        default:
            break;
        }
    }
    fd->parameter_limit = parameterLimit;

    // dynamics wants gradient pointing downhill, we want it uphill
    //for (i=0; i<3*Part->num_atoms; i++) {
    //  p->gradient[i] = -p->gradient[i];
    //}
    findRMSandMaxForce(p, &rms_force, &max_force); BAIL();

    // The initial parameter guess function is empirically determined.
    // The regression tests were run with D_MINIMIZE_PARAMETER_GUESS
    // enabled (compiled on in simulator.c).  Plots of the max_force
    // vs minimum parameter value columns were examined with gnuplot.
    // A functional form was determined which stayed within the main
    // body of the data points.  Final determination was based on the
    // evaluation counts also output with that debugging flag on.
    // Given that max_force is non-negative, the current form doesn't
    // need the upper range limit in the clamp.
    fd->initial_parameter_guess = clamp(1e-20, 1e3,
                                        0.7 / (max_force + 1000.0) +
                                        0.1 / (max_force + 20.0));

#ifdef PLOT_LINEAR_MINIMIZATION
    for (parameter = -fd->initial_parameter_guess;
         parameter < fd->initial_parameter_guess;
         parameter += fd->initial_parameter_guess / 500) {
        newLocation = gradientOffset(p, parameter);
        potential = evaluate(newLocation);
        position = (struct xyz *)newLocation->coordinate;
        printf("%e %e ", parameter, potential);
        for (j=0; j<Part->num_stretches; j++) {
            stretch = &Part->stretches[j];
            bond = stretch->b;
            vsub2(rv, position[bond->a2->index], position[bond->a1->index]);
            rSquared = vdot(rv, rv);
            r = sqrt(rSquared);
            potential = stretchPotential(Part, stretch, stretch->stretchType, r);
            printf("%f %f ", r, potential);
        }
        printf("\n");
        SetConfiguration(&newLocation, NULL);
    }
    printf("\n\n");
#endif
    
    writeMinimizeMovieFrame(OutputFile, Part, 0, (struct xyz *)p->coordinate, rms_force, max_force, Iteration++, 0,
			    fd->tolerance == COARSE_TOLERANCE ? "gradient" : "gradient fine", fd->message, evaluate(p));
    if (DEBUG(D_MINIMIZE_GRADIENT_MOVIE)) { // -D4
	writeSimpleMovieFrame(Part, (struct xyz *)p->coordinate, (struct xyz *)p->gradient, "gradient %e %e", rms_force, max_force);
    }
    if (DEBUG(D_MINIMIZE_PARAMETER_GUESS)) {
        countMinimizeEvaulations('g');
        if (parameterGuessFile == NULL) {
            parameterGuessFile = fopen("/tmp/parameterguesses", "a");
            if (parameterGuessFile == NULL) {
                perror("/tmp/parameterguesses");
                exit(1);
            }
        } else {
            fprintf(parameterGuessFile, "%e %e %e\n", last_rms_force, last_max_force, p->parameter);
            fflush(parameterGuessFile);
        }
        last_rms_force = rms_force;
        last_max_force = max_force;
    }
}

static int
minimizeStructureTermination(struct functionDefinition *fd,
                             struct configuration *previous,
                             struct configuration *current)
{
    double fp;
    double fq;
    double rms_force;
    double max_force;
    double tolerance = fd->tolerance;

    fp = evaluate(previous); BAILR(0);
    fq = evaluate(current); BAILR(0);

    evaluateGradient(current); BAILR(0);
    findRMSandMaxForce(current, &rms_force, &max_force); BAILR(0);
    if (tolerance == COARSE_TOLERANCE &&
        rms_force < MinimizeThresholdCutoverRMS &&
        max_force < MinimizeThresholdCutoverMax) {
      fd->tolerance = FINE_TOLERANCE;
      fd->algorithm = PolakRibiereConjugateGradient;
      fd->linear_algorithm = LinearMinimize;
    }
    if (tolerance == FINE_TOLERANCE &&
        (rms_force > MinimizeThresholdCutoverRMS * 1.5 ||
         max_force > MinimizeThresholdCutoverMax * 1.5)) {
      fd->tolerance = COARSE_TOLERANCE;
      fd->algorithm = SteepestDescent;
      fd->linear_algorithm = LinearBracket;
    }
    if (rms_force < MinimizeThresholdEndRMS &&
        max_force < MinimizeThresholdEndMax) {
      return 1;
    }

    return defaultTermination(fd, previous, current);
}

static void
minimizeStructureConstraints(struct configuration *p) 
{
    int i;
    int j;
    double dist;
    struct jig *jig;
    struct xyz *positions = (struct xyz *)p->coordinate;
    struct xyz delta;
    struct atom *a;
    int index;
    
    for (i=0; i<Part->num_jigs; i++) {
        jig = Part->jigs[i];
        switch (jig->type) {
        case Ground:
            for (j=0; j<jig->num_atoms; j++) {
                a = jig->atoms[j];
                index = a->index;
                positions[index] = Part->positions[index];
            }
            break;
        case LinearMotor:
            for (j=0; j<jig->num_atoms; j++) {
                a = jig->atoms[j];
                index = a->index;
                // restrict motion of atom to lay along axis from initial position
                // dist = (positions[index] - Part->positions[index]) dot jig->j.lmotor.axis
                // positions[index] = Part->positions[index] + jig->j.lmotor.axis * dist
                vsub2(delta, positions[index], Part->positions[index]);
                dist = vdot(delta, jig->j.lmotor.axis);
                vmul2c(delta, jig->j.lmotor.axis, dist);
                vadd2(positions[index], Part->positions[index], delta);
            }
            break;
        default:
            break;
        }
    }
}

static struct functionDefinition minimizeStructureFunctions;

void
minimizeStructure(struct part *part)
{
    int iter;
    struct configuration *initial;
    struct configuration *final;
    int i;
    int j;
    int jigDegreesOfFreedom;
    int coordinateCount;
    double rms_force;
    double max_force;
    double model_energy;
    struct jig *jig;

    NULLPTR(part);
    Part = part;

    jigDegreesOfFreedom = 0;
    coordinateCount = part->num_atoms * 3;
    for (i=0; i<Part->num_jigs; i++) {
        jig = Part->jigs[i];
        jig->coordinateIndex = coordinateCount + jigDegreesOfFreedom;
        jigDegreesOfFreedom += jig->degreesOfFreedom;
    }

    initializeFunctionDefinition(&minimizeStructureFunctions,
                                 minimizeStructurePotential,
                                 coordinateCount + jigDegreesOfFreedom,
                                 1024);
    BAIL();
    
    minimizeStructureFunctions.dfunc = minimizeStructureGradient;
    minimizeStructureFunctions.termination = minimizeStructureTermination;
    minimizeStructureFunctions.constraints = minimizeStructureConstraints;
    minimizeStructureFunctions.tolerance = COARSE_TOLERANCE;
    minimizeStructureFunctions.algorithm = SteepestDescent;
    minimizeStructureFunctions.linear_algorithm = LinearBracket;
    
    initial = makeConfiguration(&minimizeStructureFunctions);
    for (i=0, j=0; i<part->num_atoms; i++) {
	initial->coordinate[j++] = part->positions[i].x;
	initial->coordinate[j++] = part->positions[i].y;
	initial->coordinate[j++] = part->positions[i].z;
    }

    // To compare the torsion gradient code to the results of a
    // numerical differentiation, put the following lines in an .mmp
    // file, uncomment the define TORSION_DEBUG line, and run:
    // simulator -m -x TorsionDebug.mmp
    // Then, run:
    // glviewer < TorsionDebug.xyz
#if 0
atom 1 (6) (400, 0, 0) def
atom 2 (6) (250, 0, 0) def
info atom atomtype = sp2
bond1 1
atom 3 (6) (-250, 0, 0) def
info atom atomtype = sp2
bond2 2
atom 4 (6) (-400, 500, 0) def
bond1 3
#endif
    
    //#define TORSION_DEBUG
#ifdef TORSION_DEBUG
    debug_flags |= D_MINIMIZE_GRADIENT_MOVIE
        | D_GRADIENT_COMPARISON
        | D_SKIP_VDW
        | D_SKIP_BEND
        | D_SKIP_STRETCH;
    double theta;
    for (theta=0.0; theta<Pi; theta+=Pi/180.0) {
        initial->coordinate[1] = 50.0 * cos(theta);
        initial->coordinate[2] = 50.0 * sin(theta);
        evaluateGradient(initial);
        free(initial->gradient);
        initial->gradient = NULL;
    }
    exit(0);
#endif
    
    final = minimize(initial, &iter, NumFrames * 100);

    if (final != NULL) {
	// wware 060109  python exception handling
	evaluateGradient(final); BAIL();
	findRMSandMaxForce(final, &rms_force, &max_force); BAIL();

	writeMinimizeMovieFrame(OutputFile, part, 1, (struct xyz *)final->coordinate, rms_force, max_force,
				Iteration, 1, "final structure", minimizeStructureFunctions.message, evaluate(final));

	if (DEBUG(D_MINIMIZE_FINAL_PRINT)) { // -D 11
	    for (i=0, j=0; i<part->num_atoms; i++) {
		part->positions[i].x = final->coordinate[j++];
		part->positions[i].y = final->coordinate[j++];
		part->positions[i].z = final->coordinate[j++];
	    }
	    printPart(stdout, part);
	}
    }

    model_energy = evaluate(final);
    SetConfiguration(&initial, NULL);
    SetConfiguration(&final, NULL);
    if (model_energy > 0.25) {
	done("Final forces: rms %f pN, high %f pN, model energy: %.3f aJ evals: %d,%d",
	     rms_force,
	     max_force,
	     model_energy,
             minimizeStructureFunctions.functionEvaluationCount,
             minimizeStructureFunctions.gradientEvaluationCount);
    } else {
	done("Final forces: rms %f pN, high %f pN, model energy: %.3e aJ evals: %d,%d",
	     rms_force,
	     max_force,
	     model_energy,
             minimizeStructureFunctions.functionEvaluationCount,
             minimizeStructureFunctions.gradientEvaluationCount);
    }
}

/*
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 8
 * End:
 */