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
|
/* Low-level minimization
*
* Written by Konrad Hinsen
*/
#include "MMTK/universe.h"
#include "MMTK/forcefield.h"
#include "MMTK/trajectory.h"
/* Utility functions */
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
/* Operations on vector arrays */
static void
copy_vectors(vector3 *s, vector3 *d, int n)
{
double *src = (double *)s;
double *dest = (double *)d;
n *= 3;
while (n--)
*dest++ = *src++;
}
static void
add_vectors(vector3 *s, vector3 *d, int n)
{
double *src = (double *)s;
double *dest = (double *)d;
n *= 3;
while (n--)
*dest++ += *src++;
}
static void
scale_vectors(vector3 *v, double f, int n)
{
double *vec = (double *)v;
n *= 3;
while (n--)
*vec++ *= f;
}
/* Allocate and initialize Output variable descriptors */
static PyTrajectoryVariable *
get_data_descriptors(PyArrayObject *configuration, PyArrayObject *gradients,
double *p_energy, double *norm,
double *box_size, int box_size_length)
{
static PyTrajectoryVariable vars[6];
if (vars != NULL) {
vars[0].name = "potential_energy";
vars[0].text = "Potential energy: %lf, ";
vars[0].unit = energy_unit_name;
vars[0].type = PyTrajectory_Scalar;
vars[0].class = PyTrajectory_Energy;
vars[0].value.dp = p_energy;
vars[1].name = "gradient_norm";
vars[1].text = "Gradient norm: %lf\n";
vars[1].unit = energy_gradient_unit_name;
vars[1].type = PyTrajectory_Scalar;
vars[1].class = PyTrajectory_Energy;
vars[1].value.dp = norm;
vars[2].name = "configuration";
vars[2].text = "Configuration:\n";
vars[2].unit = length_unit_name;
vars[2].type = PyTrajectory_ParticleVector;
vars[2].class = PyTrajectory_Configuration;
vars[2].value.array = configuration;
vars[3].name = "gradients";
vars[3].text = "Energy gradients:\n";
vars[3].unit = energy_gradient_unit_name;
vars[3].type = PyTrajectory_ParticleVector;
vars[3].class = PyTrajectory_Gradients;
vars[3].value.array = gradients;
vars[4].name = NULL;
if (box_size != NULL) {
vars[4].name = "box_size";
vars[4].text = "Box size:";
vars[4].unit = length_unit_name;
vars[4].type = PyTrajectory_BoxSize;
vars[4].class = PyTrajectory_Configuration;
vars[4].value.dp = box_size;
vars[4].length = box_size_length;
vars[5].name = NULL;
}
}
return vars;
}
/* Steepest descent minimizer */
static PyObject *
steepestDescent(PyObject *dummy, PyObject *args)
{
PyObject *universe;
PyUniverseSpecObject *universe_spec;
PyArrayObject *configuration;
PyArrayObject *fixed;
PyListObject *spec_list;
PyFFEvaluatorObject *evaluator;
PyTrajectoryOutputSpec *output;
vector3 *x, *f;
long *fix;
int atoms, moving_atoms;
int steps;
double step_size, gradient_convergence;
char *description;
PyArrayObject *gradients;
PyTrajectoryVariable *data_descriptors;
energy_data p_energy;
double norm, factor;
double min_energy, min_norm;
vector3 *min_configuration = NULL, *min_gradients = NULL;
int i, j;
/* Parse and check arguments */
if (!PyArg_ParseTuple(args, "OO!O!O!iddO!s", &universe,
&PyArray_Type, &configuration,
&PyArray_Type, &fixed,
&PyFFEvaluator_Type, &evaluator,
&steps, &step_size, &gradient_convergence,
&PyList_Type, &spec_list, &description))
return NULL;
universe_spec = (PyUniverseSpecObject *)
PyObject_GetAttrString(universe, "_spec");
if (universe_spec == NULL)
return NULL;
/* Create gradient array */
#if defined(NUMPY)
gradients = (PyArrayObject *)PyArray_Copy(configuration);
#else
gradients = (PyArrayObject *)PyArray_FromDims(configuration->nd,
configuration->dimensions,
PyArray_DOUBLE);
#endif
if (gradients == NULL)
return NULL;
/* Set some convenient variables */
atoms = configuration->dimensions[0];
x = (vector3 *)configuration->data;
f = (vector3 *)gradients->data;
fix = (long *)fixed->data;
moving_atoms = atoms;
for (j = 0; j < atoms; j++)
if (fix[j])
moving_atoms--;
/* Prepare output data descriptors */
data_descriptors = get_data_descriptors(configuration, gradients,
&p_energy.energy, &norm,
universe_spec->geometry_data,
universe_spec->geometry_data_length);
/* Allocate arrays to keep track of current best point */
min_configuration = (vector3 *)malloc(atoms*sizeof(vector3));
min_gradients = (vector3 *)malloc(atoms*sizeof(vector3));
if (min_configuration == NULL || min_gradients == NULL) {
PyErr_SetString(PyExc_MemoryError, "");
goto error2;
}
/* Initialize output */
output = PyTrajectory_OutputSpecification(universe, spec_list,
description,
data_descriptors);
if (output == NULL)
goto error2;
/* Get write access for the minimization, switching to
read access only during energy evaluation */
#ifdef WITH_THREAD
evaluator->tstate_save = PyEval_SaveThread();
#endif
PyUniverseSpec_StateLock(universe_spec, -1);
/* Minimization main loop */
p_energy.gradients = (PyObject *)gradients;
p_energy.gradient_fn = NULL;
p_energy.force_constants = NULL;
p_energy.fc_fn = NULL;
min_energy = 0.; /* initialize to make gcc happy */
min_norm = 0.; /* initialize to make gcc happy */
for (i = 0; i < steps; i++) {
PyUniverseSpec_StateLock(universe_spec, -2);
PyUniverseSpec_StateLock(universe_spec, 1);
(*evaluator->eval_func)(evaluator, &p_energy, configuration, i > 0);
PyUniverseSpec_StateLock(universe_spec, 2);
if (p_energy.error) {
#ifdef WITH_THREAD
PyEval_RestoreThread(evaluator->tstate_save);
#endif
goto error;
}
PyUniverseSpec_StateLock(universe_spec, -1);
norm = 0.;
for (j = 0; j < atoms; j++)
if (!fix[j])
norm += f[j][0]*f[j][0] + f[j][1]*f[j][1] + f[j][2]*f[j][2];
norm = sqrt(norm/moving_atoms);
if (i == 0 || p_energy.energy < min_energy) {
min_energy = p_energy.energy;
min_norm = norm;
copy_vectors(x, min_configuration, atoms);
copy_vectors(f, min_gradients, atoms);
step_size *= 1.1;
}
else {
p_energy.energy = min_energy;
norm = min_norm;
copy_vectors(min_configuration, x, atoms);
copy_vectors(min_gradients, f, atoms);
step_size *= 0.5;
}
if (norm < gradient_convergence)
break;
if (PyTrajectory_Output(output, i, data_descriptors,
&evaluator->tstate_save) == -1) {
PyUniverseSpec_StateLock(universe_spec, -2);
#ifdef WITH_THREAD
PyEval_RestoreThread(evaluator->tstate_save);
#endif
goto error;
}
factor = step_size/norm;
for (j = 0; j < atoms; j++)
if (!fix[j]) {
x[j][0] -= factor*f[j][0];
x[j][1] -= factor*f[j][1];
x[j][2] -= factor*f[j][2];
}
universe_spec->correction_function(x, atoms, universe_spec->geometry_data);
}
/* Restore minimum and do output */
p_energy.energy = min_energy;
norm = min_norm;
copy_vectors(min_configuration, x, atoms);
copy_vectors(min_gradients, f, atoms);
if (PyTrajectory_Output(output, i, data_descriptors,
&evaluator->tstate_save) == -1) {
PyUniverseSpec_StateLock(universe_spec, -2);
#ifdef WITH_THREAD
PyEval_RestoreThread(evaluator->tstate_save);
#endif
goto error;
}
/* Clean up and return None */
PyUniverseSpec_StateLock(universe_spec, -2);
#ifdef WITH_THREAD
PyEval_RestoreThread(evaluator->tstate_save);
#endif
PyTrajectory_OutputFinish(output, i, 0, 1, data_descriptors);
free(min_configuration);
free(min_gradients);
Py_DECREF(gradients);
Py_INCREF(Py_None);
return Py_None;
/* Clean up and return error */
error:
PyTrajectory_OutputFinish(output, i, 1, 1, data_descriptors);
error2:
if (min_configuration != NULL)
free(min_configuration);
if (min_gradients != NULL)
free(min_gradients);
Py_DECREF(gradients);
return NULL;
}
/* Conjugate gradient minimizer */
static PyObject *
conjugateGradient(PyObject *dummy, PyObject *args)
{
PyObject *universe;
PyUniverseSpecObject *universe_spec;
PyArrayObject *configuration;
PyArrayObject *fixed;
PyListObject *spec_list;
PyFFEvaluatorObject *evaluator;
PyTrajectoryOutputSpec *output;
vector3 *x, *f1, *f2, *h;
long *fix;
int atoms, moving_atoms;
int steps;
double step_size, gradient_convergence;
char *description;
PyArrayObject *gradients1, *gradients2, *direction;
PyTrajectoryVariable *data_descriptors;
energy_data p_energy;
double norm_sq, last_norm_sq, dot, norm;
double norm_h, line_convergence, sign, step;
double last, a, b, ea, eb, da, db;
int i, j, reset_count, niter;
/* Parse and check arguments */
if (!PyArg_ParseTuple(args, "OO!O!O!iddO!s", &universe,
&PyArray_Type, &configuration,
&PyArray_Type, &fixed,
&PyFFEvaluator_Type, &evaluator,
&steps, &step_size, &gradient_convergence,
&PyList_Type, &spec_list, &description))
return NULL;
universe_spec = (PyUniverseSpecObject *)
PyObject_GetAttrString(universe, "_spec");
if (universe_spec == NULL)
return NULL;
/* Create gradient and direction arrays */
#if defined(NUMPY)
gradients1 = (PyArrayObject *)PyArray_Copy(configuration);
#else
gradients1 = (PyArrayObject *)PyArray_FromDims(configuration->nd,
configuration->dimensions,
PyArray_DOUBLE);
#endif
if (gradients1 == NULL)
return NULL;
#if defined(NUMPY)
gradients2 = (PyArrayObject *)PyArray_Copy(configuration);
#else
gradients2 = (PyArrayObject *)PyArray_FromDims(configuration->nd,
configuration->dimensions,
PyArray_DOUBLE);
#endif
if (gradients2 == NULL) {
Py_DECREF(gradients1);
return NULL;
}
#if defined(NUMPY)
direction = (PyArrayObject *)PyArray_Copy(configuration);
#else
direction = (PyArrayObject *)PyArray_FromDims(configuration->nd,
configuration->dimensions,
PyArray_DOUBLE);
#endif
if (direction == NULL) {
Py_DECREF(gradients1);
Py_DECREF(gradients2);
return NULL;
}
/* Set some convenient variables */
atoms = configuration->dimensions[0];
x = (vector3 *)configuration->data;
f1 = (vector3 *)gradients1->data;
f2 = (vector3 *)gradients2->data;
h = (vector3 *)direction->data;
fix = (long *)fixed->data;
moving_atoms = atoms;
for (j = 0; j < atoms; j++)
if (fix[j])
moving_atoms--;
/* Prepare output data descriptors */
data_descriptors = get_data_descriptors(configuration, gradients1,
&p_energy.energy, &norm,
universe_spec->geometry_data,
universe_spec->geometry_data_length);
/* Initialize output */
output = PyTrajectory_OutputSpecification(universe, spec_list,
description,
data_descriptors);
if (output == NULL)
goto error2;
/* Minimization main loop */
#ifdef WITH_THREAD
evaluator->tstate_save = PyEval_SaveThread();
#endif
reset_count = 0;
p_energy.gradients = (PyObject *)gradients1;
p_energy.gradient_fn = NULL;
p_energy.force_constants = NULL;
p_energy.fc_fn = NULL;
PyUniverseSpec_StateLock(universe_spec, 1);
(*evaluator->eval_func)(evaluator, &p_energy, configuration, 0);
PyUniverseSpec_StateLock(universe_spec, 2);
if (p_energy.error) {
#ifdef WITH_THREAD
PyEval_RestoreThread(evaluator->tstate_save);
#endif
i = 0;
goto error;
}
/* Get write access for the minimization, switching to
read access only during energy evaluation */
PyUniverseSpec_StateLock(universe_spec, -1);
norm_sq = 0.;
for (i = 0; i < steps; i++) {
last_norm_sq = norm_sq;
norm_sq = 0.;
for (j = 0; j < atoms; j++)
if (!fix[j])
norm_sq += f1[j][0]*f1[j][0] + f1[j][1]*f1[j][1] + f1[j][2]*f1[j][2];
norm = sqrt(norm_sq/moving_atoms);
if (norm < gradient_convergence)
break;
if (norm > 50.*gradient_convergence)
reset_count++;
if (PyTrajectory_Output(output, i, data_descriptors,
&evaluator->tstate_save) == -1) {
PyUniverseSpec_StateLock(universe_spec, -2);
#ifdef WITH_THREAD
PyEval_RestoreThread(evaluator->tstate_save);
#endif
goto error;
}
if (i == 0)
copy_vectors(f1, h, atoms);
else {
dot = 0.;
for (j = 0; j < atoms; j++)
if (!fix[j]) {
dot += f1[j][0]*f2[j][0] + f1[j][1]*f2[j][1] + f1[j][2]*f2[j][2];
f2[j][0] = f1[j][0];
f2[j][1] = f1[j][1];
f2[j][2] = f1[j][2];
}
if (reset_count == 5*atoms) {
for (j = 0; j < atoms; j++) {
h[j][0] = 0.;
h[j][1] = 0.;
h[j][2] = 0.;
}
reset_count = 0;
}
else
scale_vectors(h, (norm_sq-dot)/last_norm_sq, atoms);
add_vectors(f1, h, atoms);
}
line_convergence = 1.e-3*norm;
if (line_convergence < gradient_convergence)
line_convergence = gradient_convergence;
/* Line minimization */
#define eval(p) \
{ \
double d = (p)-last; \
int j; \
for (j = 0; j < atoms; j++) \
if (!fix[j]) { \
x[j][0] += sign*d*h[j][0]; \
x[j][1] += sign*d*h[j][1]; \
x[j][2] += sign*d*h[j][2]; \
} \
PyUniverseSpec_StateLock(universe_spec, -2); \
PyUniverseSpec_StateLock(universe_spec, 1); \
(*evaluator->eval_func)(evaluator, &p_energy, configuration, 0); \
PyUniverseSpec_StateLock(universe_spec, 2); \
if (p_energy.error) { \
PyEval_RestoreThread(evaluator->tstate_save); \
goto error; \
} \
PyUniverseSpec_StateLock(universe_spec, -1); \
dot = 0.; \
for (j = 0; j < atoms; j++) \
if (!fix[j]) \
dot += f1[j][0]*h[j][0] + f1[j][1]*h[j][1] + f1[j][2]*h[j][2]; \
dot /= (sign*norm_h); \
last = (p); \
}
norm_h = 0.;
for (j = 0; j < atoms; j++)
if (!fix[j])
norm_h += h[j][0]*h[j][0] + h[j][1]*h[j][1] + h[j][2]*h[j][2];
norm_h = sqrt(norm_h);
dot = 0.;
for (j = 0; j < atoms; j++)
if (!fix[j])
dot += f1[j][0]*h[j][0] + f1[j][1]*h[j][1] + f1[j][2]*h[j][2];
sign = (dot > 0.) ? -1. : 1.;
dot /= (sign*norm_h);
step = step_size/norm_h;
last = 0.;
a = b = 0.;
ea = eb = p_energy.energy;
da = db = dot;
niter = 0;
while (1) {
a = b; ea = eb; da = db;
b += step;
eval(b); eb = p_energy.energy; db = dot;
if (db > 0)
break;
if (++niter == 100)
break;
}
niter = 0;
while (1) {
double new = ((eb-ea)/norm_h+a*da-b*db)/(da-db);
if (new < a || new > b)
new = 0.5*(a+b);
eval(new);
if (dot < 0) {
a = new; ea = p_energy.energy; da = dot;
}
else {
b = new; eb = p_energy.energy; db = dot;
}
if (db < 0.01*line_convergence)
break;
if (++niter == 100)
break;
}
#undef eval
universe_spec->correction_function(x, atoms, universe_spec->geometry_data);
}
/* Final output */
if (PyTrajectory_Output(output, i, data_descriptors,
&evaluator->tstate_save) == -1)
goto error;
/* Clean up and return None */
PyUniverseSpec_StateLock(universe_spec, -2);
#ifdef WITH_THREAD
PyEval_RestoreThread(evaluator->tstate_save);
#endif
PyTrajectory_OutputFinish(output, i, 0, 1, data_descriptors);
Py_DECREF(gradients1);
Py_DECREF(gradients2);
Py_INCREF(Py_None);
return Py_None;
/* Clean up and return error */
error:
PyTrajectory_OutputFinish(output, i, 1, 1, data_descriptors);
error2:
Py_DECREF(gradients1);
Py_DECREF(gradients2);
Py_DECREF(direction);
return NULL;
}
/*
* List of functions defined in the module
*/
static PyMethodDef minimization_methods[] = {
{"steepestDescent", steepestDescent, 1},
{"conjugateGradient", conjugateGradient, 1},
{NULL, NULL} /* sentinel */
};
/* Initialization function for the module */
DL_EXPORT(void)
initMMTK_minimization(void)
{
/* Create the module and add the functions */
Py_InitModule("MMTK_minimization", minimization_methods);
/* Import the array module */
#ifdef import_array
import_array();
#endif
/* Import MMTK modules */
import_MMTK_universe();
import_MMTK_forcefield();
import_MMTK_trajectory();
/* Check for errors */
if (PyErr_Occurred())
Py_FatalError("can't initialize module MMTK_minimization");
}
|