[go: up one dir, main page]

Menu

[r5]: / trunk / src / cdmc.c  Maximize  Restore  History

Download this file

402 lines (335 with data), 8.9 kB

  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
/*
xmd - molecular dynamics for metals and ceramics
By Jonathan Rifkin <jon.rifkin@uconn.edu>
Copyright 1995-2004 Jonathan Rifkin
This program 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 program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
************************************************************************
Include Files
************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "strsub.h"
#include "parse.h"
#include "cdhouse.h"
#include "cdsubs.h"
#include "cdstep.h"
/*
************************************************************************
Global variables
************************************************************************
*/
/* HALT SIGNAL */
extern int isig;
/* GLOBAL VARIABLES */
extern Simulation_t *s;
extern Particle_t *a;
/*
************************************************************************
Local Function Prototypes
************************************************************************
*/
/* LOCAL FUNCTION PROTOTYPES */
double __eharm (Particle_t *, double, double);
/*
************************************************************************
Exported Functions
************************************************************************
*/
/* perform monte carlo minimization */
void runmc (Particle_t *a, Simulation_t *s, int nstep, double temp)
{
double etmp, eorg, eold, ecur, enew, edel, efac, tfac;
int j,reject;
long navgold, nacc=0, istep;
double esum;
double *cold = NULL;
double *cnew = NULL;
char *tname;
double eavg=0, evar0=0, evar1=0;
/* VARIALBES FOR SAVING COMPARE COORDINATES */
FILE *CompareFile;
/* INITIALIZE RANDOM JUMPING */
int rand_half = RAND_MAX >> 1;
double rsize = s->size/rand_half;
ALLOCATE (cnew, double, 3*a->np)
/* Open energy output file */
OpenStepOutput ( &(s->EnergyStepOutput) );
/* Open Compare file */
if (s->UseCompare)
{
/* GET FILE NAME */
tname = s->CompareFileName;
if (*tname=='+')
tname++;
/* OPEN FILE FOR FIRST TIME AS WRITE */
if (s->NewCompareFile)
{
s->NewCompareFile = FALSE;
CompareFile = fopen (tname, "wb");
}
/* OPEN FILE AS APPEND */
else
CompareFile = fopen (tname, "ab");
if (CompareFile==NULL)
{
printf ("ERROR: Cannot open COMPARE file %s.\n",
s->CompareFileName);
CleanAfterError();
}
}
/* USE HARMONIC ENERGY AS "BASE" ENERGY */
if (s->UseCompare && s->UseInverseCompare)
{
ecur = __eharm (a, s->CompareRadius, s->CompareEnergy);
}
/* USE FULL ENERGY AS "BASE" ENERGY */
else
{
energy_all (a, s, 0);
ecur = a->epot;
}
/* SAVE OLD PARTICLE POINTER - CHANGE TO NEW PARTICLES */
cold = a->cur;
a->cur = cnew;
navgold = a->navg;
esum = 0.0;
if (temp!=0)
tfac = 1.0/(BK*temp);
eorg = ecur;
/* monte carlo minimization */
for (istep=0;istep<nstep && isig!=1;istep++)
{
a->step++;
s->nstep++;
eold = ecur;
/* assign random displacements */
for (j=0;j<3*a->np;j++)
cnew[j] = cold[j] + rsize * (rand() - rand_half);
/* USE HARMONIC ENERGY AS "BASE" ENERGY */
if (s->UseCompare && s->UseInverseCompare)
{
enew = __eharm (a, s->CompareRadius, s->CompareEnergy);
}
/* USE FULL ENERGY AS "BASE" ENERGY */
else
{
energy_all (a, s, 0);
enew = a->epot;
}
edel = enew-eold;
reject = (edel>0);
if (reject)
{
if (temp!=0)
{
efac = tfac*edel;
if (efac<20)
reject = rand() > RAND_MAX*exp(-efac);
}
}
if (reject)
{
s->nrej++;
ecur = eold;
}
else
{
for (j=0;j<3*a->np;j++)
cold[j] = cnew[j];
ecur = enew;
s->nacc++;
nacc++;
}
/* perform running sums */
esum += ecur;
/* Save energy if saving in effect and step accepted */
if (ForceStepOutput ( &(s->EnergyStepOutput), !reject) )
fprintf
(
s->EnergyStepOutput.File,
"%li %le\n",
a->step, s->eunit*ecur/a->np
);
/* COMPARE LATTICE */
if (!reject && s->UseCompare)
{
/* USE HARMONIC ENERGY AS "COMPARISON" ENERGY */
if (!s->UseInverseCompare)
{
edel = __eharm (a, s->CompareRadius, s->CompareEnergy) - ecur;
}
/* USE FULL ENERGY AS "COMPARISON" ENERGY */
else
{
energy_all (a, s, 0);
edel = a->epot - ecur;
}
/* WRITE RESULTS TO FILE */
fwrite (&a->step, sizeof(a->step), 1, CompareFile);
fwrite (&ecur, sizeof(ecur), 1, CompareFile);
fwrite (&edel, sizeof(edel ), 1, CompareFile);
}
/* CORRELATION INFO */
etmp = ecur-eorg;
evar0 += etmp*etmp;
evar1 += etmp*(eold-eorg);
eavg += etmp;
}
/* restore particle pointer to original storage location */
a->cur = cold;
if (navgold+a->navg>0)
a->epavg = (navgold*a->epavg + esum) / (navgold+a->navg);
FREE (cnew)
/* CLOSE FILES */
CloseStepOutput ( &(s->EnergyStepOutput) );
if (s->UseCompare)
fclose (CompareFile);
/* PRINT NUMBER ACCEPTED */
if (istep>0)
{
double corr1;
printf ("Number of Metropolis steps performed %li.\n", istep);
printf ("Number of Metropolis steps accepted %li (%6.2f).\n",
nacc, (double) nacc / istep);
eavg /= istep;
evar0 = evar0 / istep - eavg*eavg;
evar1 = evar1 / istep - eavg*eavg;
if (evar0>0)
corr1 = evar1 / evar0;
if (evar0<=0.0 || corr1>=1.0)
printf ("Correlation length: infinite or unknown.\n");
else
printf ("Estimated correlation length: %lf\n", 1.0 / (1.0 - corr1) );
}
}
void read_compare (char *instr)
{
char *tokstr, *fname;
double *ctemp;
/* GET FIRST "OFF" or "ON" */
tokstr = strhed (&instr);
if (!strcmpi(tokstr,"ON"))
s->UseCompare = TRUE;
else if (!strcmpi(tokstr,"OFF"))
{
s->UseCompare = FALSE;
return;
}
else
{
printf (" *** ERRROR: UNKNOWN OPTION");
return;
}
/* CHECK FOR PRESENCE OF REFERENCE PARTICLES WHEN DOING COMPARE */
if (a->ref == NULL)
{
printf ("ERROR (read_compare): Must have reference particles.\n");
CleanAfterError();
}
/* CHECK FOR INVERSE OPTION */
tokstr = strhed (&instr);
if (!strcmpi(tokstr,"INV"))
{
s->UseInverseCompare = TRUE;
tokstr = strhed (&instr);
}
else
s->UseInverseCompare = FALSE;
/* READ RADIUS */
s->CompareRadius = dblstrf(&tokstr) * 1e-8;
/* READ FILE NAME */
fname = strhed (&instr);
strcpy (s->CompareFileName, fname);
/* DETERMINE IF FILE OPENED AS WRITE (NOT APPEND) FIRST TIME */
if (*(s->CompareFileName)=='+')
s->NewCompareFile = FALSE;
else
s->NewCompareFile = TRUE;
/* GET REFERENCE ENERGY */
ctemp = a->cur;
a->cur = a->ref;
energy_all (a, s, 0);
s->CompareEnergy = a->epot;
a->cur = ctemp;
}
/*
************************************************************************
Local Functions
************************************************************************
*/
/* CALCULATE HARMONIC APPROXIMATION TO ENERGY */
double __eharm (Particle_t *a, double CompareRadius, double eref)
{
double *ctmp = NULL;
double epot, pre_epot, *pre_cur;
double *box, box2[3], dmag, t, fac;
int i,j, n, *surf;
/* SET UP BOUNDARY CONDITIONS */
surf = a->surf;
box = a->bcur;
for (i=0;i<3;i++)
box2[i] = box[i]/2;
/* CALCULATE MAGNITUDE OF DISPLACEMENT */
dmag = 0;
n = 0;
for (i=0;i<a->np;i++)
for (j=0;j<3;j++)
{
t = fabs(a->cur[n] - a->ref[n]);
if (!surf[j])
if (t>box2[j])
t = box[j] - t;
dmag += t*t;
n++;
}
/* RETURN IF NO DISPLACEMENT RELATIVE TO REFERENCE LATTICE */
if (dmag==0.0)
return (eref);
/* ALLOCATE TEMPERORARY COORDINATES */
ALLOCATE (ctmp, double, 3*a->np);
/* DETERMINE DISPLACEMENT SCALING FACTOR */
fac = CompareRadius / sqrt(dmag);
/* CALCULATE NEW COORDINATES */
n = 0;
for (i=0;i<a->np;i++)
for (j=0;j<3;j++)
{
t = a->cur[n] - a->ref[n];
if (!surf[j])
if (t>box2[j])
t -= box[j];
else if (t<-box2[j])
t += box[j];
ctmp[n] = a->ref[n] + fac*t;
n++;
}
/* CALCULATE ENERGY FOR SMALL PERTUBATION */
pre_cur = a->cur;
pre_epot = a->epot;
a->cur = ctmp;
energy_all (a, s, 0);
/* CONVERT TO HARMONIC ENERGY FOR FULL DISPLACEMENT */
epot = eref + (a->epot - eref) / (fac*fac);
/* REPLACE ORIGINAL VALUES */
a->cur = pre_cur;
a->epot = pre_epot;
/* FREE STORAGES */
FREE (ctmp)
/* RETURN ENERGY */
return (epot);
}