|
| 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 | +} |
0 commit comments