OpenMD 3.2
Molecular Dynamics in the Open
Loading...
Searching...
No Matches
VelocityField.cpp
1/*
2 * Copyright (c) 2004-present, The University of Notre Dame. All rights
3 * reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 * SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your
32 * research, please cite the following paper when you publish your work:
33 *
34 * [1] Drisko et al., J. Open Source Softw. 9, 7004 (2024).
35 *
36 * Good starting points for code and simulation methodology are:
37 *
38 * [2] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005).
39 * [3] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006).
40 * [4] Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).
41 * [5] Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011).
42 * [6] Kuang & Gezelter, Mol. Phys., 110, 691-701 (2012).
43 * [7] Lamichhane, Gezelter & Newman, J. Chem. Phys. 141, 134109 (2014).
44 * [8] Bhattarai, Newman & Gezelter, Phys. Rev. B 99, 094106 (2019).
45 * [9] Drisko & Gezelter, J. Chem. Theory Comput. 20, 4986-4997 (2024).
46 */
47
49
50#include "utils/simError.h"
51
52namespace OpenMD {
53
54 VelocityField::VelocityField(SimInfo* info) : info_(info) {
55 vfParams_ = info_->getSimParams()->getVelocityFieldParameters();
56 initialize();
57 }
58
59 void VelocityField::initialize() {
60 if (initialized_) return;
61 initialized_ = true;
62
63 if (vfParams_ == NULL || !vfParams_->getUseVelocityField()) {
64 doVelocityField_ = false;
65 return;
66 }
67
68 // ----- which parts were requested? -----
69 bool haveSR = vfParams_->haveStrainRate();
70 bool haveSD1 = vfParams_->haveStrainDirection1();
71 bool haveSD2 = vfParams_->haveStrainDirection2();
72 bool haveVort = vfParams_->haveVorticity();
73 bool haveBG = vfParams_->haveBackgroundVelocity();
74
75 bool anyStrain = haveSR || haveSD1 || haveSD2;
76 bool fullStrain = haveSR && haveSD1 && haveSD2;
77
78 if (anyStrain && !fullStrain) {
79 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
80 "VelocityField: an incomplete rate-of-strain block was given.\n"
81 "\tThe constant-stress part requires all three of strainRate,\n"
82 "\tstrainDirection1, and strainDirection2 together (set\n"
83 "\tstrainDirection2 = strainDirection1 for uniaxial extension).\n");
84 painCave.isFatal = 1;
85 simError();
86 }
87
88 if (!fullStrain && !haveVort && !haveBG) {
89 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
90 "VelocityField: nothing to do. Specify a rate-of-strain block\n"
91 "\t(strainRate, strainDirection1, strainDirection2), a vorticity\n"
92 "\tvector, a backgroundVelocity, or some combination of these.\n");
93 painCave.isFatal = 1;
94 simError();
95 }
96
97 E_ = Mat3x3d(0.0);
98 W_ = Mat3x3d(0.0);
99
100 // ----- constant-stress (rate-of-strain) part -----
101 // Symmetric, traceless tensor built from two directions and a rate,
102 // exactly the UniformGradient construction with g -> strainRate:
103 // E_ij = sr * [ 1/2 (a_i b_j + a_j b_i) - 1/3 (a.b) delta_ij ]
104 if (fullStrain) {
105 RealType sr = vfParams_->getStrainRate();
106
107 std::vector<RealType> d1 = vfParams_->getStrainDirection1();
108 std::vector<RealType> d2 = vfParams_->getStrainDirection2();
109 if (d1.size() != 3 || d2.size() != 3) {
110 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
111 "VelocityField: strainDirection1 and strainDirection2 must\n"
112 "\teach have 3 components (got %zu and %zu).\n",
113 d1.size(), d2.size());
114 painCave.isFatal = 1;
115 simError();
116 }
117
118 Vector3d a(d1[0], d1[1], d1[2]);
119 Vector3d b(d2[0], d2[1], d2[2]);
120 a.normalize();
121 b.normalize();
122 RealType cpsi = dot(a, b);
123
124 E_(0, 0) = sr * (a.x() * b.x() - cpsi / 3.0);
125 E_(0, 1) = 0.5 * sr * (a.x() * b.y() + a.y() * b.x());
126 E_(0, 2) = 0.5 * sr * (a.x() * b.z() + a.z() * b.x());
127 E_(1, 0) = E_(0, 1);
128 E_(1, 1) = sr * (a.y() * b.y() - cpsi / 3.0);
129 E_(1, 2) = 0.5 * sr * (a.y() * b.z() + a.z() * b.y());
130 E_(2, 0) = E_(0, 2);
131 E_(2, 1) = E_(1, 2);
132 E_(2, 2) = sr * (a.z() * b.z() - cpsi / 3.0);
133 }
134
135 // ----- constant-vorticity part -----
136 // W = 1/2 [omega]_x, so that W r = 1/2 (omega x r) and curl(W r) = omega.
137 if (haveVort) {
138 std::vector<RealType> w = vfParams_->getVorticity();
139 if (w.size() != 3) {
140 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
141 "VelocityField: vorticity must have 3 components (got %zu).\n",
142 w.size());
143 painCave.isFatal = 1;
144 simError();
145 }
146
147 W_(0, 1) = -0.5 * w[2];
148 W_(0, 2) = 0.5 * w[1];
149 W_(1, 0) = 0.5 * w[2];
150 W_(1, 2) = -0.5 * w[0];
151 W_(2, 0) = -0.5 * w[1];
152 W_(2, 1) = 0.5 * w[0];
153 }
154
155 // ----- optional uniform background velocity -----
156 if (vfParams_->haveBackgroundVelocity()) {
157 std::vector<RealType> v0 = vfParams_->getBackgroundVelocity();
158 if (v0.size() != 3) {
159 snprintf(painCave.errMsg, MAX_SIM_ERROR_MSG_LENGTH,
160 "VelocityField: backgroundVelocity must have 3 components\n"
161 "\t(got %zu).\n",
162 v0.size());
163 painCave.isFatal = 1;
164 simError();
165 }
166 v0_ = Vector3d(v0[0], v0[1], v0[2]);
167 }
168
169 // ----- assemble and canonicalize from K -----
170 K_ = E_ + W_;
171
172 // Recompute E_, W_, omega_ from K_ as the single source of truth.
173 E_ = 0.5 * (K_ + K_.transpose());
174 W_ = 0.5 * (K_ - K_.transpose());
175 omega_.x() = K_(2, 1) - K_(1, 2);
176 omega_.y() = K_(0, 2) - K_(2, 0);
177 omega_.z() = K_(1, 0) - K_(0, 1);
178
179 doVelocityField_ = true;
180 }
181} // namespace OpenMD
Linear (homogeneous) ambient velocity field, queried at a location.
One of the heavy-weight classes of OpenMD, SimInfo maintains objects and variables relating to the cu...
Definition SimInfo.hpp:96
This basic Periodic Table class was originally taken from the data.cpp file in OpenBabel.
Real dot(const DynamicVector< Real > &v1, const DynamicVector< Real > &v2)
Returns the dot product of two DynamicVectors.