-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforce_numerical.cpp
74 lines (61 loc) · 1.92 KB
/
force_numerical.cpp
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
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include "initializenl.h"
#include "loadconfig.h"
#include "constants.h"
#include "potential_calc.h"
#include "force_numerical.h"
using namespace std;
void force_numerical(neighborlist_type *neighlist, config_type *fromfile, potential *run_parameters)
{
//two point method
double h=0.0001;//division for accuracy
double erg1, erg2;
double **tempr, **tempf;
tempr = new double *[fromfile->N];
tempf= new double *[fromfile->N];
for (long i=0; i<fromfile->N; i++)
{
tempr[i] = new double [Dim];
tempf[i] = new double [Dim];
}
for (long i=0; i<fromfile->N;i++)
{
for(int j=0; j<Dim; j++)
{
tempr[i][j]=fromfile->r[i][j];
}
}
ofstream myfile ;
myfile.open ("force_comparison.txt", std::ios_base::app);
for (long i=0; i<fromfile->N;i++)
{
for(int j=0; j<Dim; j++)
{
fromfile->r[i][j] = tempr[i][j] +h;
potential_calc(neighlist,fromfile, run_parameters);//calculating the force fields for x+h
fromfile->r[i][j] = tempr[i][j];
erg1 = run_parameters->PotEnergy;
fromfile->r[i][j] = tempr[i][j] -h;
potential_calc(neighlist,fromfile, run_parameters);//calculating the force fields for x-h
fromfile->r[i][j] = tempr[i][j];
erg2 = run_parameters->PotEnergy;
tempf[i][j] = -(erg1-erg2)/(2*h);
potential_calc(neighlist,fromfile, run_parameters);//calculating the force fields for x using this for analytical values
}
myfile <<i<<" " <<setprecision(15)<< run_parameters->force[i][0] << " " << tempf[i][0] << " " << run_parameters->force[i][1] << " " <<tempf[i][1] << " " << run_parameters->force[i][2] << " " <<tempf[i][2] << endl;
}
myfile.close();
for (long i=0; i<fromfile->N; i++)
{
delete [] tempr[i] ;
delete [] tempf[i] ;
}
delete [] tempr;
delete [] tempf;
exit(1);
}