Skip to content

Commit 2c424df

Browse files
authored
adding a new test to verify that the accelerable switching function returns the correct values (plumed#1300)
Co-authored-by: Daniele Rapetti <5535617+Iximiel@users.noreply.github.com>
1 parent 940f6e8 commit 2c424df

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+581
-0
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type=make
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
#include "plumed/tools/SwitchingFunction.h"
3+
#include <fstream>
4+
#include <iostream>
5+
#include <iomanip>
6+
#include <utility>
7+
8+
9+
using PLMD::SwitchingFunction;
10+
using PLMD::SwitchingFunctionAccelerable;
11+
12+
13+
struct callSQR {
14+
constexpr static auto kind="sqr_";
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+
constexpr static auto kind="";
26+
template<typename T>
27+
static std::pair <double,double> call(const T& sw, double point) {
28+
double deriv;
29+
double value = sw.calculate(point,deriv);
30+
return std::make_pair(value,deriv);
31+
}
32+
};
33+
34+
template<typename caller>
35+
void test(const std::string& name, std::string definition,bool stretch=true) {
36+
if (!stretch) {
37+
definition += " NOSTRETCH ";
38+
}
39+
40+
std::ofstream os("out_"+std::string(caller::kind)+name+(stretch ? "" : "_nostretch"));
41+
os <<std::fixed<<std::setprecision(6);
42+
SwitchingFunction sw;
43+
std::string error;
44+
sw.set(definition,error);
45+
if (!error.empty()) {
46+
std::cerr<<error<<"\n";
47+
}
48+
49+
error.clear();
50+
SwitchingFunctionAccelerable swa;
51+
swa.set(definition,error);
52+
if (!error.empty()) {
53+
std::cerr<<error<<"\n";
54+
}
55+
os << "point :\tvalue deriv\tvalue_acc deriv_acc\tvalue_delta deriv_delta\n";
56+
for (int i=0; i<10; i++) {
57+
double point=i/2.50;
58+
double deriv;
59+
double value;
60+
os<< point <<" :\t";
61+
std::tie(value,deriv) = caller::call(sw, point);
62+
os << value << " " << deriv <<"\t";
63+
double deriv_acc;
64+
double value_acc;
65+
std::tie(value_acc,deriv_acc) = caller::call(swa, point);
66+
os << value_acc << " " << deriv_acc <<"\t";
67+
os << value-value_acc << " " << deriv-deriv_acc <<"\n";
68+
}
69+
}
70+
71+
72+
int main() {
73+
for(const auto &x: {
74+
std::pair<std::string,std::string> {"COSINUS R_0=2.6","cosinus"},
75+
{"EXP R_0=0.8 D_0=0.5 D_MAX=2.6","exp"},
76+
{"GAUSSIAN R_0=1.0 D_0=0.0 D_MAX=2.6","fastgaussian"},
77+
{"GAUSSIAN R_0=1.0 D_0=0.3 D_MAX=2.6","gaussian"},
78+
{"RATIONAL R_0=1.3 NN=6 MM=10 D_MAX=2.6","fastrational"},
79+
{"RATIONAL R_0=1.3 D_MAX=2.6","fastrational_MMeq2MM"},
80+
{"RATIONAL R_0=1.3 NN=5 MM=11 D_MAX=2.6","rational"},
81+
{"RATIONAL R_0=1.3 NN=5 D_MAX=2.6","rational_MMeq2MM"},
82+
{"Q R_0=1.0 D_0=0.3 BETA=1.0 LAMBDA=1.0 REF=1.3 D_MAX=2.6","q"},
83+
{"SMAP R_0=1.3 A=3 B=2 D_MAX=2.6","smap"},
84+
{"TANH R_0=1.3 D_MAX=2.6","tanh"}
85+
}) {
86+
auto [definition, name] = x;
87+
test<callplain> (name,definition);
88+
test<callplain> (name,definition,false);
89+
test<callSQR> (name,definition);
90+
test<callSQR> (name,definition,false);
91+
92+
}
93+
94+
return 0;
95+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
point : value deriv value_acc deriv_acc value_delta deriv_delta
2+
0.000000 : 1.000000 0.000000 1.000000 0.000000 0.000000 0.000000
3+
0.400000 : 0.942728 -0.269965 0.942728 -0.269965 0.000000 0.000000
4+
0.800000 : 0.784032 -0.239042 0.784032 -0.239042 0.000000 0.000000
5+
1.200000 : 0.560268 -0.192227 0.560268 -0.192227 0.000000 0.000000
6+
1.600000 : 0.322698 -0.135791 0.322698 -0.135791 0.000000 0.000000
7+
2.000000 : 0.125745 -0.077044 0.125745 -0.077044 0.000000 0.000000
8+
2.400000 : 0.014529 -0.023170 0.014529 -0.023170 0.000000 0.000000
9+
2.800000 : 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
11+
3.600000 : 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 value_acc deriv_acc value_delta deriv_delta
2+
0.000000 : 1.000000 0.000000 1.000000 0.000000 0.000000 0.000000
3+
0.400000 : 0.942728 -0.269965 0.942728 -0.269965 0.000000 0.000000
4+
0.800000 : 0.784032 -0.239042 0.784032 -0.239042 0.000000 0.000000
5+
1.200000 : 0.560268 -0.192227 0.560268 -0.192227 0.000000 0.000000
6+
1.600000 : 0.322698 -0.135791 0.322698 -0.135791 0.000000 0.000000
7+
2.000000 : 0.125745 -0.077044 0.125745 -0.077044 0.000000 0.000000
8+
2.400000 : 0.014529 -0.023170 0.014529 -0.023170 0.000000 0.000000
9+
2.800000 : 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
11+
3.600000 : 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 value_acc deriv_acc value_delta deriv_delta
2+
0.000000 : 1.000000 0.000000 1.000000 0.000000 0.000000 0.000000
3+
0.400000 : 1.000000 0.000000 1.000000 0.000000 0.000000 0.000000
4+
0.800000 : 0.662867 -1.157757 0.662867 -1.157757 0.000000 0.000000
5+
1.200000 : 0.371321 -0.468143 0.371321 -0.468143 0.000000 0.000000
6+
1.600000 : 0.194489 -0.212958 0.194489 -0.212958 0.000000 0.000000
7+
2.000000 : 0.087234 -0.103332 0.087234 -0.103332 0.000000 0.000000
8+
2.400000 : 0.022182 -0.052228 0.022182 -0.052228 0.000000 0.000000
9+
2.800000 : 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
11+
3.600000 : 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 value_acc deriv_acc value_delta deriv_delta
2+
0.000000 : 1.000000 0.000000 1.000000 0.000000 0.000000 0.000000
3+
0.400000 : 1.000000 0.000000 1.000000 0.000000 0.000000 0.000000
4+
0.800000 : 0.687289 -1.073889 0.687289 -1.073889 0.000000 0.000000
5+
1.200000 : 0.416862 -0.434231 0.416862 -0.434231 0.000000 0.000000
6+
1.600000 : 0.252840 -0.197531 0.252840 -0.197531 0.000000 0.000000
7+
2.000000 : 0.153355 -0.095847 0.153355 -0.095847 0.000000 0.000000
8+
2.400000 : 0.093014 -0.048445 0.093014 -0.048445 0.000000 0.000000
9+
2.800000 : 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
11+
3.600000 : 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 value_acc deriv_acc value_delta deriv_delta
2+
0.000000 : 1.000000 0.000000 1.000000 0.000000 0.000000 0.000000
3+
0.400000 : 0.920406 -0.955654 0.920406 -0.955654 0.000000 0.000000
4+
0.800000 : 0.716496 -0.751744 0.716496 -0.751744 0.000000 0.000000
5+
1.200000 : 0.468662 -0.503909 0.468662 -0.503909 0.000000 0.000000
6+
1.600000 : 0.252590 -0.287837 0.252590 -0.287837 0.000000 0.000000
7+
2.000000 : 0.104858 -0.140106 0.104858 -0.140106 0.000000 0.000000
8+
2.400000 : 0.022866 -0.058113 0.022866 -0.058113 0.000000 0.000000
9+
2.800000 : 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
11+
3.600000 : 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 value_acc deriv_acc value_delta deriv_delta
2+
0.000000 : 1.000000 0.000000 1.000000 0.000000 0.000000 0.000000
3+
0.400000 : 0.923116 -0.923116 0.923116 -0.923116 0.000000 0.000000
4+
0.800000 : 0.726149 -0.726149 0.726149 -0.726149 0.000000 0.000000
5+
1.200000 : 0.486752 -0.486752 0.486752 -0.486752 0.000000 0.000000
6+
1.600000 : 0.278037 -0.278037 0.278037 -0.278037 0.000000 0.000000
7+
2.000000 : 0.135335 -0.135335 0.135335 -0.135335 0.000000 0.000000
8+
2.400000 : 0.056135 -0.056135 0.056135 -0.056135 0.000000 0.000000
9+
2.800000 : 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
11+
3.600000 : 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 value_acc deriv_acc value_delta deriv_delta
2+
0.000000 : 1.000000 0.000000 1.000000 0.000000 0.000000 0.000000
3+
0.400000 : 0.999104 -0.033405 0.999104 -0.033405 0.000000 0.000000
4+
0.800000 : 0.950037 -0.422254 0.950037 -0.422254 0.000000 0.000000
5+
1.200000 : 0.672132 -0.809075 0.672132 -0.809075 0.000000 0.000000
6+
1.600000 : 0.312593 -0.444722 0.312593 -0.444722 0.000000 0.000000
7+
2.000000 : 0.112650 -0.162544 0.112650 -0.162544 0.000000 0.000000
8+
2.400000 : 0.023987 -0.060151 0.023987 -0.060151 0.000000 0.000000
9+
2.800000 : 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
11+
3.600000 : 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

0 commit comments

Comments
 (0)