Skip to content

Commit 5aa9d0a

Browse files
Merge pull request #1325 from Iximiel/fix2.10/switchingTests
2 parents 55f0e3e + c5bb5b7 commit 5aa9d0a

28 files changed

+449
-8
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include ../../scripts/test.make
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type=make
2+
3+
function plumed_regtest_after() {
4+
#zfix add a space when removing the minus sign, this should address that
5+
for file in *.reference; do
6+
new="${file%.reference}"
7+
if test -f "$new"; then
8+
tmp=$(mktemp "./${new}.XXX")
9+
# sed -E "s/( 0\.0+\b)/ \1/g" "$new" > "${tmp}"
10+
perl -pe "s/( 0\.0+\b)/ \1/g" "$new" > "${tmp}"
11+
mv "$tmp" "$new"
12+
fi
13+
done
14+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
2+
#include "plumed/tools/SwitchingFunction.h"
3+
#include <fstream>
4+
#include <iostream>
5+
#include <iomanip>
6+
#include <ostream>
7+
#include <utility>
8+
9+
10+
using PLMD::SwitchingFunction;
11+
//using PLMD::SwitchingFunctionAccelerable;
12+
13+
14+
struct callSQR {
15+
template<typename T>
16+
static std::pair <double,double> call(const T& sw, double point) {
17+
double deriv;
18+
point *= point;
19+
double value = sw.calculateSqr(point,deriv);
20+
return std::make_pair(value,deriv);
21+
}
22+
};
23+
24+
struct callplain {
25+
template<typename T>
26+
static std::pair <double,double> call(const T& sw, const double point) {
27+
double deriv;
28+
double value = sw.calculate(point,deriv);
29+
return std::make_pair(value,deriv);
30+
}
31+
};
32+
33+
template<typename caller,typename SW>
34+
std::ostream& calculateAndSubtract(const SW& sw,
35+
const double point,
36+
const double value,
37+
const double derivative,
38+
std::ostream& out) {
39+
auto [myval,mydev] = caller::call(sw,point);
40+
out << (myval -value)<< " " << (mydev - derivative)<<" " ;
41+
return out;
42+
}
43+
44+
45+
void test(const std::string& name, std::string definition,std::string custom,bool stretch=true) {
46+
if (!stretch) {
47+
definition += " NOSTRETCH ";
48+
custom += " NOSTRETCH ";
49+
}
50+
51+
std::ofstream os("out_"+name+(stretch ? "" : "_nostretch"));
52+
os <<std::fixed<<std::setprecision(6);
53+
SwitchingFunction sw;
54+
std::string error;
55+
sw.set(definition,error);
56+
if (!error.empty()) {
57+
std::cerr<<error<<"\n";
58+
}
59+
error.clear();
60+
61+
SwitchingFunction swc;
62+
swc.set(custom,error);
63+
if (!error.empty()) {
64+
std::cerr<<error<<"\n";
65+
}
66+
error.clear();
67+
/*
68+
SwitchingFunctionAccelerable swa;
69+
swa.set(definition,error);
70+
if (!error.empty()) {
71+
std::cerr<<error<<"\n";
72+
}
73+
*/
74+
os << "point : value deriv vsq_delta dsq_delta "
75+
"vAcc_delta dAcc_delta "
76+
//"vAccsq_delta dAccsq_delta "
77+
"vcus_delta dcus_delta "
78+
"vsqcus_delta dsqcus_delta\n";
79+
for (int i=0; i<10; i++) {
80+
double point=i/2.50;
81+
double derivative;
82+
double value;
83+
os<< point <<" : ";
84+
std::tie(value,derivative) = callplain::call(sw, point);
85+
os << value << " " << derivative <<" ";
86+
calculateAndSubtract<callSQR>(sw,point, value, derivative,os) << " ";
87+
//calculateAndSubtract<callplain>(swa,point, value, derivative,os) << " ";
88+
//calculateAndSubtract<callSQR>(swa,point, value, derivative,os) << " ";
89+
calculateAndSubtract<callplain>(swc,point, value, derivative,os) << " ";
90+
calculateAndSubtract<callSQR>(swc,point, value, derivative,os) << "\n";
91+
}
92+
}
93+
94+
95+
int main() {
96+
//the test will output the value of the switch along with de difference of it and
97+
//the same calculation under different settings (with squared/non squared input,
98+
//with the "accelerable version and with custom)
99+
for(const auto &x: {
100+
std::tuple<std::string,std::string,std::string> {
101+
"COSINUS R_0=2.6",
102+
"CUSTOM FUNC=0.5*(cos(x*pi)+1) R_0=2.6 D_MAX=2.6",
103+
"cosinus"
104+
}, {
105+
"EXP R_0=0.8 D_0=0.5 D_MAX=2.6",
106+
"CUSTOM FUNC=exp(-x) R_0=0.8 D_0=0.5 D_MAX=2.6",
107+
"exp"
108+
}, {
109+
"GAUSSIAN R_0=1.0 D_0=0.0 D_MAX=2.6",
110+
"CUSTOM FUNC=exp(-x^2/2) R_0=1.0 D_0=0.0 D_MAX=2.6",
111+
"fastgaussian"
112+
}, {
113+
"GAUSSIAN R_0=1.0 D_0=0.3 D_MAX=2.6",
114+
"CUSTOM FUNC=exp(-x^2/2) R_0=1.0 D_0=0.3 D_MAX=2.6",
115+
"gaussian"
116+
}, {
117+
"RATIONAL R_0=1.3 NN=6 MM=10 D_MAX=2.6",
118+
"CUSTOM FUNC=(1-x^6)/(1-x^10) D_MAX=2.6 R_0=1.3",
119+
"fastrational"
120+
}, {
121+
"RATIONAL R_0=1.3 D_MAX=2.6",
122+
"CUSTOM FUNC=(1-x^6)/(1-x^12) D_MAX=2.6 R_0=1.3",
123+
"fastrational_NNeq2MM"
124+
}, {
125+
"RATIONAL R_0=1.3 NN=5 MM=11 D_MAX=2.6",
126+
"CUSTOM FUNC=(1-x^5)/(1-x^11) D_MAX=2.6 R_0=1.3",
127+
"rational"
128+
}, {
129+
"RATIONAL R_0=1.3 NN=5 D_MAX=2.6",
130+
"CUSTOM FUNC=(1-x^5)/(1-x^10) D_MAX=2.6 R_0=1.3",
131+
"rational_NNeq2MM"
132+
}, {
133+
"Q R_0=1.0 D_0=0.3 BETA=5.0 LAMBDA=1.0 REF=1.3 D_MAX=2.6",
134+
"CUSTOM FUNC=1/(1+exp(5.0*((x+0.3)-1.3))) R_0=1.0 D_0=0.3 D_MAX=2.6",
135+
"q"
136+
}, {
137+
"TANH R_0=1.3 D_MAX=2.6",
138+
"CUSTOM FUNC=1-tanh(x) R_0=1.3 D_MAX=2.6",
139+
"tanh"
140+
}, {
141+
"SMAP R_0=1.3 A=3 B=2 D_MAX=2.6",
142+
"CUSTOM FUNC=(1+(2^(3/2)-1)*x^3)^(-2/3) R_0=1.3 D_MAX=2.6",
143+
"smap"
144+
}, {
145+
"CUBIC D_MAX=2.6 D_0=0.6",
146+
//Here R_O = DMAX-D_0
147+
"CUSTOM FUNC=(x-1)^2*(1+2*x) R_0=2.0 D_0=0.6 D_MAX=2.6",
148+
"cubic"
149+
}
150+
}) {
151+
auto [definition, custom, name] = x;
152+
test (name,definition,custom);
153+
test (name,definition,custom,false);
154+
155+
}
156+
157+
return 0;
158+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
point : value deriv vsq_delta dsq_delta vAcc_delta dAcc_delta vcus_delta dcus_delta vsqcus_delta dsqcus_delta
2+
0.000000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
3+
0.400000 : 0.942728 -0.701909 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4+
0.800000 : 0.784032 -0.621510 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5+
1.200000 : 0.560268 -0.499790 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6+
1.600000 : 0.322698 -0.353058 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7+
2.000000 : 0.125745 -0.200314 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8+
2.400000 : 0.014529 -0.060243 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
9+
2.800000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
10+
3.200000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
11+
3.600000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
point : value deriv vsq_delta dsq_delta vAcc_delta dAcc_delta vcus_delta dcus_delta vsqcus_delta dsqcus_delta
2+
0.000000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
3+
0.400000 : 0.942728 -0.701909 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4+
0.800000 : 0.784032 -0.621510 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5+
1.200000 : 0.560268 -0.499790 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6+
1.600000 : 0.322698 -0.353058 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7+
2.000000 : 0.125745 -0.200314 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8+
2.400000 : 0.014529 -0.060243 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
9+
2.800000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
10+
3.200000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
11+
3.600000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
point : value deriv vsq_delta dsq_delta vAcc_delta dAcc_delta vcus_delta dcus_delta vsqcus_delta dsqcus_delta
2+
0.000000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
3+
0.400000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4+
0.800000 : 0.972000 -0.337500 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5+
1.200000 : 0.784000 -0.525000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6+
1.600000 : 0.500000 -0.468750 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7+
2.000000 : 0.216000 -0.315000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8+
2.400000 : 0.028000 -0.112500 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
9+
2.800000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
10+
3.200000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
11+
3.600000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
point : value deriv vsq_delta dsq_delta vAcc_delta dAcc_delta vcus_delta dcus_delta vsqcus_delta dsqcus_delta
2+
0.000000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
3+
0.400000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4+
0.800000 : 0.972000 -0.337500 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5+
1.200000 : 0.784000 -0.525000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6+
1.600000 : 0.500000 -0.468750 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7+
2.000000 : 0.216000 -0.315000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8+
2.400000 : 0.028000 -0.112500 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
9+
2.800000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
10+
3.200000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
11+
3.600000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
point : value deriv vsq_delta dsq_delta vAcc_delta dAcc_delta vcus_delta dcus_delta vsqcus_delta dsqcus_delta
2+
0.000000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
3+
0.400000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4+
0.800000 : 0.662867 -1.157757 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5+
1.200000 : 0.371321 -0.468143 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6+
1.600000 : 0.194489 -0.212958 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7+
2.000000 : 0.087234 -0.103332 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8+
2.400000 : 0.022182 -0.052228 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
9+
2.800000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
10+
3.200000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
11+
3.600000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
point : value deriv vsq_delta dsq_delta vAcc_delta dAcc_delta vcus_delta dcus_delta vsqcus_delta dsqcus_delta
2+
0.000000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
3+
0.400000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4+
0.800000 : 0.687289 -1.073889 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5+
1.200000 : 0.416862 -0.434231 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6+
1.600000 : 0.252840 -0.197531 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7+
2.000000 : 0.153355 -0.095847 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8+
2.400000 : 0.093014 -0.048445 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
9+
2.800000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
10+
3.200000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
11+
3.600000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
point : value deriv vsq_delta dsq_delta vAcc_delta dAcc_delta vcus_delta dcus_delta vsqcus_delta dsqcus_delta
2+
0.000000 : 1.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
3+
0.400000 : 0.920406 -0.955654 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
4+
0.800000 : 0.716496 -0.751744 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
5+
1.200000 : 0.468662 -0.503909 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
6+
1.600000 : 0.252590 -0.287837 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
7+
2.000000 : 0.104858 -0.140106 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
8+
2.400000 : 0.022866 -0.058113 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
9+
2.800000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
10+
3.200000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
11+
3.600000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

0 commit comments

Comments
 (0)