Skip to content

Commit 9b13f6f

Browse files
Merge pull request #1414 from Iximiel/feature/atomicDistributionUsability
2 parents f6a51a0 + 10f320e commit 9b13f6f

8 files changed

Lines changed: 1298 additions & 689 deletions

File tree

regtest/tools/rt-make-AtomicDistribution/main.cpp

Lines changed: 148 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "plumed/tools/AtomDistribution.h"
22
#include "plumed/tools/Random.h"
33
#include "plumed/tools/Vector.h"
4+
#include "plumed/tools/Tools.h"
5+
#include "plumed/tools/Pbc.h"
46
#include <array>
57
#include <fstream>
68
#include <memory>
@@ -15,24 +17,49 @@ void atomsInBoxCheck(
1517
const std::vector<double> &box,
1618
const std::string_view header,
1719
std::ostream & ofs) {
18-
Vector lowbound = atoms[0];
19-
Vector upbound= atoms[0];
20-
for (unsigned i =1; i < atoms.size(); ++i) {
21-
for (unsigned j=0; j<3; ++j) {
22-
if (atoms[i][j] < lowbound[j]) {
23-
lowbound[j] = atoms[i][j];
20+
Pbc mybox;
21+
mybox.setBox(Tensor{
22+
box[0],
23+
box[1],
24+
box[2],
25+
box[3],
26+
box[4],
27+
box[5],
28+
box[6],
29+
box[7],
30+
box[8]});
31+
ofs << "Atoms are within box dimensions:\n";
32+
if (!mybox.isOrthorombic()) {
33+
bool inbox = true;
34+
for (const auto& atom : atoms) {
35+
auto scaled = mybox.realToScaled(atom);
36+
inbox &= scaled[0]<1.0;
37+
inbox &= scaled[1]<1.0;
38+
inbox &= scaled[2]<1.0;
39+
if (!inbox) {
40+
break;
2441
}
25-
if (atoms[i][j] > upbound[j]) {
26-
upbound[j] = atoms[i][j];
42+
}
43+
ofs <<header <<" all atoms within the non orthorombic box: "<< inbox << "\n";
44+
} else {
45+
Vector lowbound = atoms[0];
46+
Vector upbound= atoms[0];
47+
for (unsigned i =1; i < atoms.size(); ++i) {
48+
for (unsigned j=0; j<3; ++j) {
49+
if (atoms[i][j] < lowbound[j]) {
50+
lowbound[j] = atoms[i][j];
51+
}
52+
if (atoms[i][j] > upbound[j]) {
53+
upbound[j] = atoms[i][j];
54+
}
2755
}
2856
}
29-
}
30-
// box is orhtorombic an starts in 0,0,0:
31-
//shifting lowbound and upbound to chek the box:
32-
upbound-=lowbound;
33-
ofs << "Atoms are within box dimensions:\n";
34-
for (unsigned j=0; j<3; ++j) {
35-
ofs <<header <<" all atoms in box along " <<xyz[j]<<" : "<< (upbound[j]<box[j*3+j]) << "\n";
57+
// box is orhtorombic an starts in 0,0,0:
58+
//shifting lowbound and upbound to chek the box:
59+
upbound-=lowbound;
60+
for (unsigned j=0; j<3; ++j) {
61+
ofs <<header <<" all atoms in box along " <<xyz[j]<<" : "<< (upbound[j]<box[j*3+j]) << "\n";
62+
}
3663
}
3764
}
3865

@@ -63,14 +90,15 @@ void replyTrajCheck(std::string_view kind,
6390
unsigned nat = atoms.size();
6491
const auto oldNat=nat;
6592

66-
std::unique_ptr<PLMD::AtomDistribution> rep= std::make_unique<repliedTrajectory>([&]() {
93+
auto rep= [&]() {
6794
auto d = AtomDistribution::getAtomDistribution(kind);
6895
d->frame(atoms,basebox,0,rng);
69-
return d;
96+
auto mod="reply "+ std::to_string(num[0]) + " "
97+
+ std::to_string(num[1]) + " "
98+
+ std::to_string(num[2]);
99+
return AtomDistribution::decorateAtomDistribution(std::move(d),mod);
70100
}
71-
(),
72-
num[0], num[1], num[2],
73-
nat);
101+
();
74102

75103
//this must return true
76104
ofs <<header<< " rep->overrideNat(nat)=" <<
@@ -125,20 +153,16 @@ void scaleTrajCheck(std::string_view kind,
125153
rng.setSeed(12345);
126154
std::vector<Vector> baseatoms(200);
127155
std::vector<double> basebox(9);
128-
std::unique_ptr<PLMD::AtomDistribution> scaled= std::make_unique<scaledTrajectory>([&]() {
156+
auto scaled= [&]() {
129157
std::unique_ptr<PLMD::AtomDistribution> d;
130-
if (kind == "sphere-reply212") {
131-
d = std::make_unique<repliedTrajectory>(
132-
AtomDistribution::getAtomDistribution("sphere"),
133-
2,1,2,baseatoms.size()/(2*1*2));
134-
} else {
135158
d = AtomDistribution::getAtomDistribution(kind);
136-
}
137159
d->frame(baseatoms,basebox,0,rng);
138-
return d;
160+
161+
auto mod="scale "+ std::to_string(scale) + " ";
162+
return
163+
AtomDistribution::decorateAtomDistribution(std::move(d),mod);
139164
}
140-
(),
141-
scale);
165+
();
142166

143167
std::vector<Vector> atoms(200);
144168
std::vector<double> box(9);
@@ -151,7 +175,8 @@ void scaleTrajCheck(std::string_view kind,
151175
ofs << header << "The atoms are scaled correctly:\t";
152176
bool correct = true;
153177
for(unsigned i =0; i< atoms.size() && correct; ++i) {
154-
correct = (abs(scale*baseatoms[i][0] - atoms[i][0]) < 1000*PLMD::epsilon)&&
178+
correct = (abs(scale*baseatoms[i][0] - atoms[i][0]) < 1000*PLMD::epsilon)
179+
&&
155180
(abs(scale*baseatoms[i][1] - atoms[i][1]) < 1000*PLMD::epsilon)&&
156181
(abs(scale*baseatoms[i][2] - atoms[i][2]) < 1000*PLMD::epsilon);
157182

@@ -161,11 +186,87 @@ void scaleTrajCheck(std::string_view kind,
161186
atomsInBoxCheck(atoms,box,header,ofs);
162187
ofs << "New box has the correct dimensions:\n";
163188
for (unsigned j=0; j<3; ++j) {
164-
ofs <<header <<" correct box dimension " <<xyz[j]<<" : "
189+
ofs <<header <<" correct box dimension " <<xyz[j]
190+
<<" : "
165191
<< ((scale*basebox[j*3+j] - box[j*3+j]) < 1000*PLMD::epsilon) << "\n";
166192
}
167193
}
168194

195+
void fixTrajCheck(std::string_view kind,
196+
std::ostream & ofs) {
197+
std::stringstream ss;
198+
ss << "[fixTrajCheck -" << kind << "-]:";
199+
auto header = ss.str();
200+
ofs << header << "\n";
201+
//reinitialized each time for stability
202+
Random rng;
203+
rng.setSeed(12345);
204+
std::vector<Vector> baseatoms(200);
205+
std::vector<double> basebox(9);
206+
// sphere generates a new configuration at each step
207+
auto d = AtomDistribution::getAtomDistribution(std::string(kind)+"|fix");
208+
d->frame(baseatoms,basebox,0,rng);
209+
std::vector<Vector> atoms(200);
210+
std::vector<double> box(9);
211+
//generating the next frame
212+
d->frame(atoms,box,0,rng);
213+
214+
ofs << header << "The atoms are fixed correctly:\t";
215+
bool correct = true;
216+
for(unsigned i =0; i< atoms.size() && correct; ++i) {
217+
correct = (abs(baseatoms[i][0] - atoms[i][0]) < 1000*PLMD::epsilon)
218+
&&
219+
(abs(baseatoms[i][1] - atoms[i][1]) < 1000*PLMD::epsilon)&&
220+
(abs(baseatoms[i][2] - atoms[i][2]) < 1000*PLMD::epsilon);
221+
222+
}
223+
ofs << correct << "\n";
224+
}
225+
226+
void forceBoxCheck(std::string_view kind, std::ostream & ofs) {
227+
std::string header= "[forceBoxCheck -"+ std::string(kind) + "-]:";
228+
ofs << header << "\n";
229+
{
230+
auto d = AtomDistribution::getAtomDistribution(std::string(kind)+"|box 1 2 3" );
231+
//reinitialized each time for stability
232+
Random rng;
233+
std::vector<Vector> atoms(200);
234+
std::vector<double> box(9);
235+
d->frame(atoms,box,0,rng);
236+
ofs << header << "The box is changed as asked (ortho): ";
237+
238+
bool success = (box[0] - 1.0)< 1000 * PLMD::epsilon &&
239+
box[1] < 1000 * PLMD::epsilon &&
240+
box[2] < 1000 * PLMD::epsilon &&
241+
box[3] < 1000 * PLMD::epsilon &&
242+
(box[4] - 2.0) < 1000 * PLMD::epsilon &&
243+
box[5] < 1000 * PLMD::epsilon &&
244+
box[6] < 1000 * PLMD::epsilon &&
245+
box[7] < 1000 * PLMD::epsilon &&
246+
(box[8] - 3.0) < 1000 * PLMD::epsilon;
247+
ofs << success << "\n";
248+
}
249+
{
250+
auto d = AtomDistribution::getAtomDistribution(std::string(kind)+"|box 1 2 3 4 5 6 7 8 9" );
251+
//reinitialized each time for stability
252+
Random rng;
253+
std::vector<Vector> atoms(200);
254+
std::vector<double> box(9);
255+
d->frame(atoms,box,0,rng);
256+
ofs << header << "The box is changed as asked (9 elements): ";
257+
258+
bool success = (box[0] - 1.0)< 1000 * PLMD::epsilon &&
259+
(box[1] - 2.0) < 1000 * PLMD::epsilon &&
260+
(box[2] - 3.0) < 1000 * PLMD::epsilon &&
261+
(box[3] - 4.0) < 1000 * PLMD::epsilon &&
262+
(box[4] - 5.0) < 1000 * PLMD::epsilon &&
263+
(box[5] - 6.0) < 1000 * PLMD::epsilon &&
264+
(box[6] - 7.0) < 1000 * PLMD::epsilon &&
265+
(box[7] - 8.0) < 1000 * PLMD::epsilon &&
266+
(box[8] - 9.0) < 1000 * PLMD::epsilon;
267+
ofs << success << "\n";
268+
}
269+
}
169270
int main() {
170271
std::ofstream ofs("output");
171272
ofs << std::boolalpha;
@@ -174,7 +275,11 @@ int main() {
174275
"cube",
175276
"sphere",
176277
"globs",
177-
"sc"
278+
"sc",
279+
"fcc",
280+
"bcc",
281+
"ifcc",
282+
"ibcc"
178283
}) {
179284
basecheck(kind,ofs);
180285
ofs << "\n";
@@ -193,8 +298,17 @@ int main() {
193298
scaleTrajCheck("sphere",num,ofs);
194299
scaleTrajCheck("sc",num,ofs);
195300
scaleTrajCheck("globs",num,ofs);
196-
scaleTrajCheck("sphere-reply212",num,ofs);
301+
scaleTrajCheck("sphere|reply 2 1 2",num,ofs);
197302
ofs << "\n";
198303
}
304+
forceBoxCheck("sc",ofs);
305+
forceBoxCheck("fcc",ofs);
306+
//I actually do not know how to test wiggle in a sensible way
307+
// cube, globs and sphere are generate a randm configuration at each step
308+
fixTrajCheck("cube",ofs);
309+
fixTrajCheck("globs",ofs);
310+
fixTrajCheck("sphere",ofs);
311+
fixTrajCheck("sphere|reply 1 1 2",ofs);
312+
fixTrajCheck("ifcc|wiggle 0.5",ofs);
199313
return 0;
200314
}

regtest/tools/rt-make-AtomicDistribution/output.reference

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ Atoms are within box dimensions:
2828
[baseCheck -sc-]: all atoms in box along y : true
2929
[baseCheck -sc-]: all atoms in box along z : true
3030

31+
[baseCheck -fcc-]:
32+
Atoms are within box dimensions:
33+
[baseCheck -fcc-]: all atoms within the non orthorombic box: true
34+
35+
[baseCheck -bcc-]:
36+
Atoms are within box dimensions:
37+
[baseCheck -bcc-]: all atoms within the non orthorombic box: true
38+
39+
[baseCheck -ifcc-]:
40+
Atoms are within box dimensions:
41+
[baseCheck -ifcc-]: all atoms in box along x : true
42+
[baseCheck -ifcc-]: all atoms in box along y : true
43+
[baseCheck -ifcc-]: all atoms in box along z : true
44+
45+
[baseCheck -ibcc-]:
46+
Atoms are within box dimensions:
47+
[baseCheck -ibcc-]: all atoms in box along x : true
48+
[baseCheck -ibcc-]: all atoms in box along y : true
49+
[baseCheck -ibcc-]: all atoms in box along z : true
50+
3151
[replyTrajCheck -sphere 1, 2, 3-]:
3252
[replyTrajCheck -sphere 1, 2, 3-]: rep->overrideNat(nat)=true
3353
[replyTrajCheck -sphere 1, 2, 3-]: new number of atoms: ( should be 1200) : 1200
@@ -162,16 +182,16 @@ New box has the correct dimensions:
162182
[scaleTrajCheck -globs*0.5-]: correct box dimension x : true
163183
[scaleTrajCheck -globs*0.5-]: correct box dimension y : true
164184
[scaleTrajCheck -globs*0.5-]: correct box dimension z : true
165-
[scaleTrajCheck -sphere-reply212*0.5-]:
166-
[scaleTrajCheck -sphere-reply212*0.5-]:The atoms are scaled correctly: true
185+
[scaleTrajCheck -sphere|reply 2 1 2*0.5-]:
186+
[scaleTrajCheck -sphere|reply 2 1 2*0.5-]:The atoms are scaled correctly: true
167187
Atoms are within box dimensions:
168-
[scaleTrajCheck -sphere-reply212*0.5-]: all atoms in box along x : true
169-
[scaleTrajCheck -sphere-reply212*0.5-]: all atoms in box along y : true
170-
[scaleTrajCheck -sphere-reply212*0.5-]: all atoms in box along z : true
188+
[scaleTrajCheck -sphere|reply 2 1 2*0.5-]: all atoms in box along x : true
189+
[scaleTrajCheck -sphere|reply 2 1 2*0.5-]: all atoms in box along y : true
190+
[scaleTrajCheck -sphere|reply 2 1 2*0.5-]: all atoms in box along z : true
171191
New box has the correct dimensions:
172-
[scaleTrajCheck -sphere-reply212*0.5-]: correct box dimension x : true
173-
[scaleTrajCheck -sphere-reply212*0.5-]: correct box dimension y : true
174-
[scaleTrajCheck -sphere-reply212*0.5-]: correct box dimension z : true
192+
[scaleTrajCheck -sphere|reply 2 1 2*0.5-]: correct box dimension x : true
193+
[scaleTrajCheck -sphere|reply 2 1 2*0.5-]: correct box dimension y : true
194+
[scaleTrajCheck -sphere|reply 2 1 2*0.5-]: correct box dimension z : true
175195

176196
[scaleTrajCheck -sphere*3-]:
177197
[scaleTrajCheck -sphere*3-]:The atoms are scaled correctly: true
@@ -203,16 +223,16 @@ New box has the correct dimensions:
203223
[scaleTrajCheck -globs*3-]: correct box dimension x : true
204224
[scaleTrajCheck -globs*3-]: correct box dimension y : true
205225
[scaleTrajCheck -globs*3-]: correct box dimension z : true
206-
[scaleTrajCheck -sphere-reply212*3-]:
207-
[scaleTrajCheck -sphere-reply212*3-]:The atoms are scaled correctly: true
226+
[scaleTrajCheck -sphere|reply 2 1 2*3-]:
227+
[scaleTrajCheck -sphere|reply 2 1 2*3-]:The atoms are scaled correctly: true
208228
Atoms are within box dimensions:
209-
[scaleTrajCheck -sphere-reply212*3-]: all atoms in box along x : true
210-
[scaleTrajCheck -sphere-reply212*3-]: all atoms in box along y : true
211-
[scaleTrajCheck -sphere-reply212*3-]: all atoms in box along z : true
229+
[scaleTrajCheck -sphere|reply 2 1 2*3-]: all atoms in box along x : true
230+
[scaleTrajCheck -sphere|reply 2 1 2*3-]: all atoms in box along y : true
231+
[scaleTrajCheck -sphere|reply 2 1 2*3-]: all atoms in box along z : true
212232
New box has the correct dimensions:
213-
[scaleTrajCheck -sphere-reply212*3-]: correct box dimension x : true
214-
[scaleTrajCheck -sphere-reply212*3-]: correct box dimension y : true
215-
[scaleTrajCheck -sphere-reply212*3-]: correct box dimension z : true
233+
[scaleTrajCheck -sphere|reply 2 1 2*3-]: correct box dimension x : true
234+
[scaleTrajCheck -sphere|reply 2 1 2*3-]: correct box dimension y : true
235+
[scaleTrajCheck -sphere|reply 2 1 2*3-]: correct box dimension z : true
216236

217237
[scaleTrajCheck -sphere*10-]:
218238
[scaleTrajCheck -sphere*10-]:The atoms are scaled correctly: true
@@ -244,14 +264,30 @@ New box has the correct dimensions:
244264
[scaleTrajCheck -globs*10-]: correct box dimension x : true
245265
[scaleTrajCheck -globs*10-]: correct box dimension y : true
246266
[scaleTrajCheck -globs*10-]: correct box dimension z : true
247-
[scaleTrajCheck -sphere-reply212*10-]:
248-
[scaleTrajCheck -sphere-reply212*10-]:The atoms are scaled correctly: true
267+
[scaleTrajCheck -sphere|reply 2 1 2*10-]:
268+
[scaleTrajCheck -sphere|reply 2 1 2*10-]:The atoms are scaled correctly: true
249269
Atoms are within box dimensions:
250-
[scaleTrajCheck -sphere-reply212*10-]: all atoms in box along x : true
251-
[scaleTrajCheck -sphere-reply212*10-]: all atoms in box along y : true
252-
[scaleTrajCheck -sphere-reply212*10-]: all atoms in box along z : true
270+
[scaleTrajCheck -sphere|reply 2 1 2*10-]: all atoms in box along x : true
271+
[scaleTrajCheck -sphere|reply 2 1 2*10-]: all atoms in box along y : true
272+
[scaleTrajCheck -sphere|reply 2 1 2*10-]: all atoms in box along z : true
253273
New box has the correct dimensions:
254-
[scaleTrajCheck -sphere-reply212*10-]: correct box dimension x : true
255-
[scaleTrajCheck -sphere-reply212*10-]: correct box dimension y : true
256-
[scaleTrajCheck -sphere-reply212*10-]: correct box dimension z : true
274+
[scaleTrajCheck -sphere|reply 2 1 2*10-]: correct box dimension x : true
275+
[scaleTrajCheck -sphere|reply 2 1 2*10-]: correct box dimension y : true
276+
[scaleTrajCheck -sphere|reply 2 1 2*10-]: correct box dimension z : true
257277

278+
[forceBoxCheck -sc-]:
279+
[forceBoxCheck -sc-]:The box is changed as asked (ortho): true
280+
[forceBoxCheck -sc-]:The box is changed as asked (9 elements): true
281+
[forceBoxCheck -fcc-]:
282+
[forceBoxCheck -fcc-]:The box is changed as asked (ortho): true
283+
[forceBoxCheck -fcc-]:The box is changed as asked (9 elements): true
284+
[fixTrajCheck -cube-]:
285+
[fixTrajCheck -cube-]:The atoms are fixed correctly: true
286+
[fixTrajCheck -globs-]:
287+
[fixTrajCheck -globs-]:The atoms are fixed correctly: true
288+
[fixTrajCheck -sphere-]:
289+
[fixTrajCheck -sphere-]:The atoms are fixed correctly: true
290+
[fixTrajCheck -sphere|reply 1 1 2-]:
291+
[fixTrajCheck -sphere|reply 1 1 2-]:The atoms are fixed correctly: true
292+
[fixTrajCheck -ifcc|wiggle 0.5-]:
293+
[fixTrajCheck -ifcc|wiggle 0.5-]:The atoms are fixed correctly: true

0 commit comments

Comments
 (0)