This repository was archived by the owner on Jun 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelaxation.cpp
More file actions
500 lines (388 loc) · 15.8 KB
/
Copy pathRelaxation.cpp
File metadata and controls
500 lines (388 loc) · 15.8 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
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
// Relaxation.cpp: implementation of the Relaxation class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "LatticeRelaxation.h"
#include "Relaxation.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#include <algorithm>
/**
* Default constructor for Relaxation class
*/
Relaxation::~Relaxation()
{
}
/**
* Parametrized constructor for Relaxation class
*
* Takes a pointer to CLatticeRelaxationDoc and ModelingOptions instance as parameters and sets their values
* to local member variables
*/
Relaxation::Relaxation(CLatticeRelaxationDoc* doc, ModelingOptions modelingOptions)
: Modeling(doc), modelingOptions(modelingOptions), lattice(m_Doc->modelingProject->activeLattice), atomsDeque(lattice->atoms)
{
lattice = m_Doc->modelingProject->activeLattice;
atomsDeque = lattice->atoms;
}
/**
* Executes migration modelling process for an atom defined in modellingProject variable
*
* Modeling Scenario: migrating atom is immobile and traped along migration direction in a
* given number of steps; On every step the lack lattice is relaxed to find configuration with
* minimal energy
*
* Migrating atom is specified by m_Doc->modelingProject->migratingAtom
* Migration final atom is m_Doc->modelingProject->migratingToPoint
* Number of migration steps is m_Doc->modelingProject->migratingSteps
*
* Returns instance of MigrationResults class with information about modeling results
*/
MigrationResults Relaxation::migration(LPVOID pParam)
{
// Prepare MigrationResults to fulfill and return it later in function
MigrationResults migrationResults;
ModelingProject* modelingProject = m_Doc->modelingProject;
// Calculate migration step vector
Vector migrationStep = modelingProject->migratingToPoint - modelingProject->migratingAtom->r;
migrationStep /= modelingProject->migratingSteps;
for (int i = 0; i <= modelingProject->migratingSteps; i++) {
// Relax lattice for every step
MigrationStepResults migrationStepResult = relaxLattice(pParam);
// Save relaxation results information to migration results
migrationStepResult.migratingAtomCoordinates = modelingProject->migratingAtom->r;
migrationResults.migrationStepResults.push_back(migrationStepResult);
// Set forces of all atoms to 0
Lattice* lattice = m_Doc->modelingProject->activeLattice;
std::deque<Atom*>& atomsDeque = lattice->atoms;
std::deque<Atom*>::iterator atomsIterator;
for (atomsIterator = atomsDeque.begin(); atomsIterator != atomsDeque.end(); atomsIterator++) {
Atom* atom = *atomsIterator;
atom->v = Vector(0,0,0);
}
// Move atom one step forward
modelingProject->migratingAtom->r += migrationStep;
}
// Return migrating atom one step behind
modelingProject->migratingAtom->r -= migrationStep;
return migrationResults;
}
/**
* Execute lattice relaxation modelling process
*
* Depending on modeling options set in modelingOptions member variable this method
* execute modeling process wither with or without elastic corrections
*
* If we want to take dilatation volume into account, modelingOptions.calculateDilatationVolume
* must be true. In this case this function calls correctShell() function
* Otherwise relaxLattice() is called
*/
RelaxationResults Relaxation::relaxation(LPVOID pParam)
{
if (!modelingOptions.calculateDilatationVolume) {
// Relax crystallite without calculating dilatation volume and without elastic corrections
// Just delegate calculations to relaxLattice() method
return relaxLattice(pParam);
} else {
// Relax crystallite with elastic corrections;
// Their number is set in modelingOptions.elasticCorrectionsAmmount
const int correctionsAmmount = modelingOptions.elasticCorrectionsAmmount;
// On every correction step delegate calculation to correctShell() method
RelaxationResults relaxationResults = correctShell(pParam);
for (int i = 1; i < correctionsAmmount; i++) {
RelaxationResults correctedRelaxationResults = correctShell(pParam);
// Amend relaxation results taking in view new correction step
relaxationResults.calculationTime += correctedRelaxationResults.calculationTime;
relaxationResults.modelTime += correctedRelaxationResults.modelTime;
relaxationResults.numberOfDempthings += correctedRelaxationResults.numberOfDempthings;
relaxationResults.numberOfStemps += correctedRelaxationResults.numberOfStemps;
relaxationResults.complexEnergyAfterRelaxationCorrected = correctedRelaxationResults.complexEnergyAfterRelaxation;
relaxationResults.relaxationEnergy = relaxationResults.complexEnergyAfterRelaxationCorrected - relaxationResults.complexEnergyBeforeRelaxation;
}
return relaxationResults;
}
}
/**
* This method calculates relaxed atomistic configuration and after that corrects immutable shell
* taking into account point defect dilatation volume
*
* For finding relaxed configuration delegates call to relaxLattice() method
*
* After that we calculate forces experiencing by shell atoms and calculate dilatation volume
* from the difference of these forces before and after creating given point defect
*
* Shifts atoms in immovable shell based on calculated dilatation volume
*/
RelaxationResults Relaxation::correctShell(LPVOID pParam) {
// Find relaxed configuration
RelaxationResults relaxationResults = relaxLattice(pParam);
// Calculate forces experiencing by atoms in immovable shell
resetShellForces();
calculateForces(true);
// Calculate dilatation volume
double D11 = 0, D22 = 0, D33 = 0;
for (std::deque<Atom*>::iterator atomsIterator = atomsDeque.begin(); atomsIterator != atomsDeque.end(); atomsIterator++) {
Atom* atom = *atomsIterator;
if (!atom->inShell) continue;
Vector dF = atom->v - atom->vSaved;
D11 += atom->r.x * dF.x;
D22 += atom->r.y * dF.y;
D33 += atom->r.z * dF.z;
}
double K = modelingOptions.bulkModulus * 1e11;
double dilVolume = 2e9 * (D11 + D22 + D33) / ((3*K) * (2.86 * 2.86 * 2.86));
// Correct coordinates of all atoms in immovable shell taking point defect
// dilatation volume into account
for (atomsIterator = atomsDeque.begin(); atomsIterator != atomsDeque.end(); atomsIterator++) {
Atom* atom = *atomsIterator;
if (!atom->inShell) continue;
double r = atom->r.getLength();
double dR = dilVolume / (4 * 3.14 * r * r);
atom->r *= (1 - (dR / r));
}
// Save calculated forces
saveForces();
// Set dilatation volume to result and return it
relaxationResults.dilatationVolume = dilVolume;
return relaxationResults;
}
/**
* Find a relaxed configuration of a given crystallite
*/
RelaxationResults Relaxation::relaxLattice(LPVOID pParam)
{
// Prepare some initial variables
RelaxationResults relaxationResults;
DWORD relaxationStart = GetTickCount();
std::deque<Atom*>::iterator atomsIterator;
CSingleLock lock(&m_Doc->m_CS);
double kineticE = 0.0;
double prevKineticE = 0.0;
double delt = modelingOptions.startDelt;
int numDempth = 0;
int lastDempthingStep = 0;
double modelTime = 0.0;
lattice->calculateAtomNeighbours(modelingOptions.maximumActionDistance); // Build neighbour table
double startPotE = calculatePotentialEnergy(); // Calculating initial potential energy
// Execute modelling loop
for (int step = 0; step < modelingOptions.maximumNumberOfSteps; step++) {
lock.Lock(); // take synchronize lock
modelTime += delt; // increase physical time
calculateForces(); // calculate forces acting on atoms in model
kineticE = integrateVelocities(delt); // Change atoms position, q vectors and calculate qinetic energy
// Dempthing
if (modelingOptions.dampingEnabled) {
if ((step - lastDempthingStep > 5) && (kineticE < prevKineticE)) {
numDempth++; // increase number of demphings
delt+=modelingOptions.ddelt; // increase delta time
dampModel();
lastDempthingStep = step; // save step when damping was done
}
}
prevKineticE = kineticE; // save kinetic energy on this step; required for demphing
lock.Unlock(); // unlock critical section
// Maximum number of dampings condition
if (numDempth > modelingOptions.maximumNumberOfDemphings) break;
// Maximum delta time condition
if (delt >= modelingOptions.maxdelt) break;
// Update view of the model
if ((step % 5) == 0) {
m_Doc->UpdateAllViews(NULL);
}
} // eo steps cycle
// Calculate some result values, set them to relaxationResults variable and return it
double finalPotE = calculatePotentialEnergy();
if (!modelingOptions.dampingEnabled) {
finalPotE += kineticE;
}
DWORD relaxationEnd = GetTickCount();
DWORD relaxationElapsed = relaxationEnd - relaxationStart;
relaxationResults.complexEnergyBeforeRelaxation = startPotE / 160.219;
relaxationResults.complexEnergyAfterRelaxation = finalPotE / 160.219;
relaxationResults.complexEnergyAfterRelaxationCorrected = relaxationResults.complexEnergyAfterRelaxation;
relaxationResults.relaxationEnergy = (startPotE - finalPotE) / 160.219;
relaxationResults.modelTime = modelTime;
relaxationResults.numberOfStemps = step;
relaxationResults.numberOfDempthings = numDempth;
relaxationResults.calculationTime = relaxationElapsed;
return relaxationResults;
}
/**
* Damps the model - recuilds atom nethgbour tables, rollback atom coordinates,
* and sets to zero all Qi vectors
*/
void Relaxation::dampModel()
{
lattice->calculateAtomNeighbours(modelingOptions.maximumActionDistance); // Rebuild neighbours table
std::deque<Atom*>::iterator atomsIterator;
// Set to zero Qi vectors and rollback one step atoms coordinates
for (atomsIterator = atomsDeque.begin(); atomsIterator != atomsDeque.end(); atomsIterator++) {
Atom* atom = *atomsIterator;
if (!atom->moveable) continue;
atom->q1 = Vector(0,0,0);
atom->q2 = Vector(0,0,0);
atom->q3 = Vector(0,0,0);
atom->q4 = Vector(0,0,0);
atom->q5 = Vector(0,0,0);
if (atom == m_Doc->modelingProject->migratingAtom) continue;
atom->r = atom->rSaved;
}
}
/**
* Calculates new atom positions from the forces they're experiencing
*
* For integrating we use Nordseick method
*
* Returns complex kinetic energy
*/
double Relaxation::integrateVelocities(double delt)
{
static const double c0 = 3.0 / 16.0;
static const double c1 = 25.0 / 360.0;
static const double c3 = 11.0 / 18.0;
static const double c4 = 1.0 / 6.0;
static const double c5 = 1.0 / 60.0;
double kineticE = 0.0;
for (std::deque<Atom*>::iterator atomsIterator = atomsDeque.begin(); atomsIterator != atomsDeque.end(); atomsIterator++) {
Atom* atom = *atomsIterator;
if (!atom->moveable) continue; // Don't move immovable atoms
if (atom == m_Doc->modelingProject->migratingAtom) continue; // Don't move migrating atom
atom->rSaved = atom->r; // Save current atom position
atom->r += atom->q1 + atom->q2 + atom->q3 + atom->q4 + atom->q5;
atom->q1 += 2.0 * atom->q2 + 3.0 * atom->q3+ 4.0 * atom->q4 + 5.0 * atom->q5;
atom->q2 += 3.0 * atom->q3 + 6.0 * atom->q4 + 10.0 * atom->q5;
atom->q3 += 4.0 * atom->q4 + 10.0 * atom->q5;
atom->q4 += 5.0 * atom->q5;
double g = 0.5 * delt * delt / atom->atomType->mass;
Vector p = g * atom->v - atom->q2;
atom->r += c0 * p;
atom->q1 += c1 * p;
atom->q2 += p;
atom->q3 += c3 * p;
atom->q4 += c4 * p;
atom->q5 += c5 * p;
kineticE += atom->q1.getLength() * 0.25 / g;
atom->v = Vector(0,0,0);
}
return kineticE;
}
/**
* For all atoms in model this method saves their forces to vSaved property
*/
void Relaxation::saveForces()
{
for (std::deque<Atom*>::iterator atomsIterator = atomsDeque.begin(); atomsIterator != atomsDeque.end(); atomsIterator++) {
Atom* atom = *atomsIterator;
if (atom->moveable) continue;
atom->vSaved = atom->v;
}
}
/**
* Resets forces of all atoms in model to 0
*/
void Relaxation::resetShellForces()
{
for (std::deque<Atom*>::iterator atomsIterator = atomsDeque.begin(); atomsIterator != atomsDeque.end(); atomsIterator++) {
Atom* atom = *atomsIterator;
if (!atom->moveable) {
atom->v = Vector();
}
}
}
/**
* Calculates forces that are atoms in model experiencing
*
* If we use embedded atom method, forces are calculated as energy gradient - each atom is shifted by dR
* in 3 directions, and energy is calculated for all these 4 positions. Deriviatives are calculated as dE/dR
*
* In case of pairwise potentials, this function uses energy distribution loaded from file
*/
void Relaxation::calculateForces(bool includeImmoveableAtoms)
{
for (std::deque<Atom*>::iterator atomsIterator = atomsDeque.begin(); atomsIterator != atomsDeque.end(); atomsIterator++) {
Atom* atomI = *atomsIterator;
if (!includeImmoveableAtoms && !atomI->moveable) continue;
if (modelingOptions.useEmbeddedAtomMethod) {
// Embedded atom method
Vector dPot;
// Calculate energy in given position
double decPot = calculatePotentialEnergyPerAtom(atomI);
for (int k = 0; k < 3; k++) {
atomI->r.coordinate[k] += modelingOptions.diffStep; // Shift atom by modelingOptions.diffStep in all three directions
double incPot = calculatePotentialEnergyPerAtom(atomI); // Calculate atom energy in shifted position
dPot.coordinate[k] = (incPot - decPot) / modelingOptions.diffStep; // Calculate gradient coordinate
atomI->r.coordinate[k] -= modelingOptions.diffStep; // Put atom back
}
atomI->v -= dPot;
} else {
// Pairwise potentials case
std::vector<Atom*>::iterator neigboursAtomsIterator;
for (neigboursAtomsIterator = atomI->neigbours.begin(); neigboursAtomsIterator != atomI->neigbours.end(); neigboursAtomsIterator++) {
Atom* atomJ = *neigboursAtomsIterator;
Potential* pot = getPotential(atomI->atomType,atomJ->atomType); // Get potential describing atom types pair
Vector diff = atomI->r - atomJ->r;
double r = diff.getLength(); // Calculate the distance between atoms
// Calculate forces using distribution set by potential
double qinEnergy = pot->forceDistribution(r);
diff *= 0.5 * qinEnergy / r;
atomI->v += diff;
}
}
}
}
/**
* Calculates and returns the complex energy of the whole crystallite
*/
double Relaxation::calculatePotentialEnergy(bool includeImmoveableAtoms)
{
double potE = 0.0;
for (std::deque<Atom*>::iterator atomsIterator = atomsDeque.begin(); atomsIterator != atomsDeque.end(); atomsIterator++) {
Atom* atomI = *atomsIterator;
if (atomI->inShell) continue;
double atomPotEnergy = calculatePotentialEnergyPerAtom(atomI);
potE += atomPotEnergy;
}
return potE;
}
/**
* Calculates and returns the energy per given atom
*/
double Relaxation::calculatePotentialEnergyPerAtom(Atom* atomI)
{
std::vector<Atom*>::iterator neigboursAtomsIterator;
double atomPotEnergy = 0.0;
for (neigboursAtomsIterator = atomI->neigbours.begin(); neigboursAtomsIterator != atomI->neigbours.end(); neigboursAtomsIterator++) {
Atom* atomJ = *neigboursAtomsIterator;
Potential* pot = getPotential(atomI->atomType,atomJ->atomType);
Vector diff = atomI->r - atomJ->r;
double r = diff.getLength();
atomPotEnergy += pot->energyDistribution(r) / 2;
}
if (modelingOptions.useEmbeddedAtomMethod) {
double f = 0.0;
for (neigboursAtomsIterator = atomI->neigbours.begin(); neigboursAtomsIterator != atomI->neigbours.end(); neigboursAtomsIterator++) {
Atom* atomJ = *neigboursAtomsIterator;
Vector diff = atomI->r - atomJ->r;
double r = diff.getLength();
Potential* pot = getPotential(atomI->atomType,atomJ->atomType);
f += pot->electronicDensityDistribution(r);
}
atomPotEnergy += atomI->atomType->embeddingEnergy(f) * 160.219; // 1 ev = 1.602176565(35)×10−19 J
}
return atomPotEnergy;
}
Potential* Relaxation::getPotential(AtomType* atomType1, AtomType* atomType2)
{
Potential* pot;
pot = m_Doc->modelingProject->potentials[std::pair<AtomType*,AtomType*>(atomType1,atomType2)];
if (NULL == pot) {
pot = m_Doc->modelingProject->potentials[std::pair<AtomType*,AtomType*>(atomType2,atomType1)];
}
if (NULL == pot) {
throw PotentialNotLoadedException(atomType1,atomType2);
}
return pot;
}