-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_filament_energy.cpp
191 lines (123 loc) · 3 KB
/
compute_filament_energy.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "header.h"
double compute_filament_energy();
double compute_filament_energy_diff(int ranchain,int ranmonomer,vector<double> & dx);
extern double r[M][N][dim];
double compute_filament_energy(){
double E=0;
double u1[dim]={0},u2[dim]={0};
for(int i=0;i<M;i++){
double sep=0;
for(int k=0;k<dim;k++){
u1[k]=r[i][1][k]-r[i][0][k];
sep+=u1[k]*u1[k];
}
sep=sqrt(sep);
for(int k=0;k<dim;k++){
u1[k]/=sep;
}
//have first normalized bond vector.
E+=kfs/2*(sep-aback)*(sep-aback);
for(int j=1;j<N-1;j++){
double sep=0;
for(int k=0;k<dim;k++){
u2[k]=r[i][j+1][k]-r[i][j][k];
sep+=u2[k]*u2[k];
}
sep=sqrt(sep);
for(int k=0;k<dim;k++){
u2[k]/=sep;
}
//have second normalized bond vector.
E+=kfs/2*(sep-aback)*(sep-aback);
for(int k=0;k<dim;k++){
E-=kfb*u1[k]*u2[k];
//dot product of the bond vectors
}
E+=kfb; //arbitrary offset, if the bonds are straight the energy is zero.
for(int k=0;k<dim;k++){
u1[k]=u2[k];
}
//swap the new bond vector into the old.
}
}
return E;
}
double compute_filament_energy_diff(int ranchain,int ranmonomer,vector<double> & dx){
double dE=0;
double u1[dim]={0},u2[dim]={0};
int imin=max(0,ranmonomer-2);
int imax=min(N-1,ranmonomer+2);
double sep=0;
sep=0;
for(int k=0;k<dim;k++){
u1[k]=r[ranchain][imin+1][k]-r[ranchain][imin][k];
sep+=u1[k]*u1[k];
}
sep=sqrt(sep);
for(int k=0;k<dim;k++){
u1[k]/=sep;
}
//have first normalized bond vector.
dE-=kfs/2*(sep-aback)*(sep-aback);
for(int i=imin+1;i<imax;i++){
sep=0;
for(int k=0;k<dim;k++){
u2[k]=r[ranchain][i+1][k]-r[ranchain][i][k];
sep+=u2[k]*u2[k];
}
sep=sqrt(sep);
for(int k=0;k<dim;k++){
u2[k]/=sep;
}
//have second normalized bond vector.
dE-=kfs/2*(sep-aback)*(sep-aback);
for(int k=0;k<dim;k++){
dE+=kfb*u1[k]*u2[k];
//dot product of the bond vectors
}
for(int k=0;k<dim;k++){
u1[k]=u2[k];
}
//swap the new bond vector into the old.
}
for(int k=0;k<dim;k++){
r[ranchain][ranmonomer][k]+=dx[k]; //here we're updating the positions of the new monomer. We have to undo this later
}
//update the bond's position
sep=0;
for(int k=0;k<dim;k++){
u1[k]=r[ranchain][imin+1][k]-r[ranchain][imin][k];
sep+=u1[k]*u1[k];
}
sep=sqrt(sep);
for(int k=0;k<dim;k++){
u1[k]/=sep;
}
//have first normalized bond vector.
dE+=kfs/2*(sep-aback)*(sep-aback);
for(int i=imin+1;i<imax;i++){
sep=0;
for(int k=0;k<dim;k++){
u2[k]=r[ranchain][i+1][k]-r[ranchain][i][k];
sep+=u2[k]*u2[k];
}
sep=sqrt(sep);
for(int k=0;k<dim;k++){
u2[k]/=sep;
}
//have second normalized bond vector.
dE+=kfs/2*(sep-aback)*(sep-aback);
for(int k=0;k<dim;k++){
dE-=kfb*u1[k]*u2[k];
//dot product of the bond vectors
}
for(int k=0;k<dim;k++){
u1[k]=u2[k];
}
//swap the new bond vector into the old.
}
for(int k=0;k<dim;k++){
r[ranchain][ranmonomer][k]-=dx[k]; //undo the update
}
return dE;
}