File: bendComponent.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (185 lines) | stat: -rw-r--r-- 4,038 bytes parent folder | download | duplicates (4)
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
#include <BALL/MOLMEC/COMMON/bendComponent.h>

#include <BALL/MOLMEC/COMMON/forceField.h>

namespace BALL
{

	BendComponent::BendComponent()
	{
	}

	BendComponent::BendComponent(ForceField& ff)
		: ForceFieldComponent(ff)
	{
	}

	BendComponent::~BendComponent()
	{
	}

	// calculates the current energy of this component
	double BendComponent::updateEnergy()
	{
		energy_ = 0;

		// abort for an empty vector
		if (bend_.empty())
		{
			return 0.0;
		}

		Vector3 v1, v2;
		bool use_selection = getForceField()->getUseSelection();
		QuadraticAngleBend::Data* bend_it = &(bend_[0]);
		QuadraticAngleBend::Data* bend_end = &(bend_[bend_.size() - 1]);
		for (; bend_it <= bend_end ; ++bend_it)
		{
			const Atom* atom1 = bend_it->atom1;
			const Atom* atom2 = bend_it->atom2;
			const Atom* atom3 = bend_it->atom3;
			if (!use_selection ||
					(   atom1->isSelected()
					 || atom2->isSelected()
					 || atom3->isSelected()))
			{
				v1 = atom1->getPosition() - atom2->getPosition();
				v2 = atom3->getPosition() - atom2->getPosition();
				double square_length = v1.getSquareLength() * v2.getSquareLength();

				if (square_length == 0.0)
				{
					continue;
				}

				double costheta = v1 * v2 / sqrt(square_length);
				double theta;
				if (costheta > 1.0)
				{
					theta = 0.0;
				}
				else if (costheta < -1.0)
				{
					theta = Constants::PI;
				}
				else
				{
					theta = acos(costheta);
				}

				theta -= bend_it->values.theta0;
				energy_ += bend_it->values.k * theta * theta;
			}
		}

		return energy_;
	}

	// calculates and adds its forces to the current forces of the force field
	void BendComponent::updateForces()
	{
		if ((getForceField() == 0) || (getForceField()->getSystem() == 0))
		{
			return;
		}

		bool use_selection = getForceField()->getUseSelection();
		for (Size i = 0; i < bend_.size(); i++)
		{
			Atom* atom1 = bend_[i].atom1;
			Atom* atom2 = bend_[i].atom2;
			Atom* atom3 = bend_[i].atom3;

			if (!use_selection
					|| atom1->isSelected()
					|| atom2->isSelected()
					|| atom3->isSelected())
			{

				// Calculate the vector between atom1 and atom2,
				// test if the vector has length larger than 0 and normalize it

				Vector3 v1 = atom1->getPosition() - atom2->getPosition();
				Vector3 v2 = atom3->getPosition() - atom2->getPosition();
				double length = v1.getLength();

				if (length == 0.0) continue;
				double inverse_length_v1 = 1.0 / length;
				v1 *= inverse_length_v1 ;

				// Calculate the vector between atom3 and atom2,
				// test if the vector has length larger than 0 and normalize it

				length = v2.getLength();
				if (length == 0.0) continue;
				double inverse_length_v2 = 1/length;
				v2 *= inverse_length_v2;

				// Calculate the cos of theta and then theta
				double costheta = v1 * v2;
				double theta;
				if (costheta > 1.0)
				{
					theta = 0.0;
				}
				else if (costheta < -1.0)
				{
					theta = Constants::PI;
				}
				else
				{
					theta = acos(costheta);
				}

				// unit conversion: kJ/(mol A) -> N
				// kJ -> J: 1e3
				// A -> m : 1e10
				// J/mol -> mol: Avogadro
				double factor = 1e13 / Constants::AVOGADRO * 2 * bend_[i].values.k * (theta - bend_[i].values.theta0);

				// Calculate the cross product of v1 and v2, test if it has length unequal 0,
				// and normalize it.

				Vector3 cross = v1 % v2;
				if ((length = cross.getLength()) != 0)
				{
					cross *= (1.0 / length);
				}
				else
				{
					continue;
				}

				Vector3 n1 = v1 % cross;
				Vector3 n2 = v2 % cross;
				n1 *= factor * inverse_length_v1;
				n2 *= factor * inverse_length_v2;

				if (!use_selection)
				{
					atom1->getForce() -= n1;
					atom2->getForce() += n1;
					atom2->getForce() -= n2;
					atom3->getForce() += n2;
				}
				else
				{
					if (atom1->isSelected())
					{
						atom1->getForce() -= n1;
					}

					if (atom2->isSelected())
					{
						atom2->getForce() += n1;
						atom2->getForce() -= n2;
					}
					if (atom3->isSelected())
					{
						atom3->getForce() += n2;
					}
				}
			}
		}
	}
}