~sfiorucci/ptools/test_seb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "forcefield.h"
#include "geometry.h"
#include "rmsd.h"
#include "pdbio.h"


#include <fstream>
#include <math.h>  //for fabs()
#include <sstream> //for istringstream


using std::ios_base;

// temporary:
// convert a std::string in an other type if >> is correctly defined 
#include <sstream>
template<typename T>
bool from_string( const std::string & Str, T & Dest )
{
    // create a stream from the input string
    std::istringstream iss( Str );
    // try the conversion toward Dest
    return (iss >> Dest) != 0;
}





namespace PTools
{



void PrintVec(const Vdouble& vec)
{
    std::cout << "Vector: " ;
    for (uint i=0; i<vec.size(); i++)
        std::cout << vec[i] << "  " ;

    std::cout << "\n";

}



#ifdef AUTO_DIFF

static bool numerical_warning=false;
void ForceField::NumDerivatives(const Vdouble& stateVars, Vdouble& delta, bool print)
{

    if (!numerical_warning)
    {
        std::cout << "Warning: using numerical derivatives in production may lead to poor performances\n" ;
        numerical_warning=true;
    }
    for (uint j=0; j<ProblemSize(); j++)
    {

        std::vector<dbl> newvars1 = stateVars;
        std::vector<dbl> newvars2 = stateVars;



//         dbl h=1.0/20000.0;

        imag(newvars1[j])=1;
        dbl F1=Function(newvars1);
//         newvars2[j]-=h;
//         dbl F2=Function(newvars2);
//         dbl diff=(F1-F2)/(2*h) ;
        delta[j]=imag(F1);
        if (print) std::cout << "function : " << real(F1) << std::endl ;
    }

    if (print)
    {
        std::cout << "Numerical derivatives: \n";
        for (uint i=0; i<ProblemSize(); i++)
        {
            std::cout <<  "Derivatives[" << i<< "]"  <<  real(delta[i]) << std::endl ;
        }
        std::cout << "\n";

    }
}
#else
void ForceField::NumDerivatives(const Vdouble& stateVars, Vdouble& delta, bool print)
{
    std::cout << "This feature was not enabled at compile-time. Please recompile with #define AUTO_DIFF \n";
}

#endif  //AUTO_DIFF





}//namespace PTools