File: lennardJones.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 239,924 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; javascript: 164; makefile: 88
file content (375 lines) | stat: -rw-r--r-- 8,515 bytes parent folder | download | duplicates (8)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//

#include <BALL/MOLMEC/PARAMETER/lennardJones.h>
#include <BALL/MOLMEC/PARAMETER/forceFieldParameters.h>

using namespace std;

namespace BALL 
{

	LennardJones::LennardJones() 
		:	ParameterSection(),
			A_(0),
			B_(0),
			N_(0),
			Aij_(0),
			Bij_(0),
			is_defined_(0),
			format_(EPSILON_R_FORMAT),
			names_()
	{
	}


	LennardJones::LennardJones(const LennardJones& lj) 
		:	ParameterSection(lj),
			A_(lj.A_),
			B_(lj.B_),
			N_(lj.N_),
			Aij_(lj.Aij_),
			Bij_(lj.Bij_),
			is_defined_(lj.is_defined_),
			format_(lj.format_),
			names_(lj.names_)
	{
	}


	LennardJones::~LennardJones() 
	{
		clear();
	}


	void LennardJones::clear() 
	{
		// clear allocated parameter fields
		A_.clear();
		B_.clear();
		N_.clear();
		Aij_.clear();
		Bij_.clear();
		is_defined_.clear();
		format_ = EPSILON_R_FORMAT;
		names_.clear();

		ParameterSection::clear();
	}


	const LennardJones& LennardJones::operator = (const LennardJones& lj)
		
	{
		ParameterSection::operator = (lj);
		A_ = lj.A_;
		B_ = lj.B_;
		N_ = lj.N_;
		Aij_ = lj.Aij_;
		Bij_ = lj.Bij_;
		is_defined_ = lj.is_defined_;
		format_ = lj.format_;
		names_ = lj.names_;

		return *this;
	}


	bool LennardJones::extractSection
		(Parameters& parameters, const String& section_name) 
	{
		return ParameterSection::extractSection(parameters, section_name);
	}


	bool LennardJones::extractSection
		(ForceFieldParameters& parameters, const String& section_name) 
	{
		// check whether the parameters are valid
		if (!parameters.isValid())
		{
			return false;
		}
		
		// extract the basis information
		ParameterSection::extractSection(parameters, section_name);
		bool use_geometric_mean = false;


		// check whether all variables we need are defined, terminate otherwise
		if ((!hasVariable("A") || !hasVariable("B"))
				&& (!hasVariable("epsilon") || !hasVariable("R")))
		{
			Log.error() << "LennardJones::extractSection: Lennard Jones parameter section requires two variable columns:"		
				<< "A/B or epsilon/R" << endl;

			return false;

		} 
		else 
		{
			// format_ == A_B_FORMAT:				parameters are in A/B format
			// format_ == EPSILON_R_FORMAT:	parameters are in epsilon/R format
			// format_ == SLATER_KIRKWOOD_FORMAT:	parameters are in epsilon/R format
			if (hasVariable("epsilon") && hasVariable("R"))
			{
				format_ = EPSILON_R_FORMAT;
				if (options.has("radius_averaging"))
				{
					if (options["radius_averaging"] == "arithmetic")
					{
						use_geometric_mean = false;
					}
					else if (options["radius_averaging"] == "geometric")
					{
						use_geometric_mean = true;
					}
					else
					{
						Log.warn() << "AmberNonBonded: unknown method for averaging LJ radii: '" 
								<< options["radius_averaging"] << "'. Using arithmetic mean." << std::endl;
					}
				}
			} 
			else if (hasVariable("A") && hasVariable("B"))
			{
				format_ = A_B_FORMAT;
			}		
			else if (hasVariable("alpha") && hasVariable("N") && hasVariable("R"))
			{
				// ?????
				format_ = SLATER_KIRKWOOD_FORMAT;
				Log.error() << "LennardJones::extractSection: Slater Kirkwood format not yet supported!" << endl;

				return false;
			}
							 
		}

		// build a two dimensional array of the atom types
		// loop variable
		Size	i;
		const AtomTypes&	atom_types = parameters.getAtomTypes();
		number_of_atom_types_ = atom_types.getNumberOfTypes();
		
		// allocate two onedimensional fields for the two parameters
		A_.resize(number_of_atom_types_);
		B_.resize(number_of_atom_types_);
		Aij_.resize(number_of_atom_types_ * number_of_atom_types_);
		Bij_.resize(number_of_atom_types_ * number_of_atom_types_);
		is_defined_.resize(number_of_atom_types_);

		for (i = 0; i < number_of_atom_types_; i++) 
		{
			is_defined_[i] = false;
		}

		// the indices of the columns containing the values
		Size index_A = 0;
		Size index_B = 0;
		if (format_ == A_B_FORMAT)
		{
			index_A = getColumnIndex("A");
			index_B = getColumnIndex("B");
		} 
		else if (format_ == EPSILON_R_FORMAT) 
		{
			index_A = getColumnIndex("epsilon");
			index_B = getColumnIndex("R");
		}
		else if (format_ == SLATER_KIRKWOOD_FORMAT) 
		{
			index_A = getColumnIndex("epsilon");
			index_B = getColumnIndex("R");
		}

		// try to identify the units of A and B
		// and set the two conversion factors
		double factor_A = 1.0;
		double factor_B = 1.0;
		
		if (format_ == A_B_FORMAT)
		{
			if (options.has("unit_A"))
			{ //?????
			}
		}
		// in EPSILON_R_FORMAT epsilon is in A and R is in B
		if (format_ == EPSILON_R_FORMAT)
		{
			if (options.has("unit_epsilon"))
			{
				if (options["unit_epsilon"] == "kcal/mol")
				{
					factor_A = Constants::JOULE_PER_CAL;
				}
				if (options["unit_epsilon"] == "cal/mol")
				{
					factor_A = Constants::JOULE_PER_CAL * 0.001;					
				}
				if (options["unit_epsilon"] == "J/mol")
				{
					factor_A = 0.001;
				}
			}

			if (options.has("unit_R"))
			{
				if (options["unit_R"] == "pm")
				{
					factor_B = 0.1;
				}
			}
		}

		Atom::Type		atom_type;
		String				key;
		for (i = 0; i < getNumberOfKeys(); ++i)
		{
			// get the key
			key = getKey(i);
			if (atom_types.hasType(key))
			{
				// get the two parameters
				atom_type = atom_types.getType(key);
				
				// retrieve the two values
				float A = getValue(i, index_A).toFloat() * factor_A;
				float B = getValue(i, index_B).toFloat() * factor_B;


				// store the values
				is_defined_[atom_type] = true;
				A_[atom_type] = A;
				B_[atom_type] = B;


				// check for the sign of the parameters: they have to be positive!
				if ((A < 0) || (B < 0))
				{
					if (format_ == EPSILON_R_FORMAT)
					{
						Log.warn() << "LennardJones::extractSection: VdW parameter may not be negative: type = " << atom_type << " (" << key << "), eps = " << A 
											 << ", r = " << B << endl;
					} 
					else 
					{
						Log.warn() << "LennardJones::extractSection: VdW parameter may not be negative: type = " << atom_type << " (" << key << "), A = " << A 
											 << ", B = " << B << endl;
					}
				}

			} 
			else	
			{
				Log.warn() << "LennardJones::extractSection: unknown atom type in Lennard Jones parameters: " << key << "   i = " << i << endl;
			}
		}

		// now assemble all Lennard Jones parameter for all known atom types
			
		for (i = 0; i < number_of_atom_types_; i++)
		{
			for (Size j = i; j < number_of_atom_types_; j++)
			{
				// calculate the two indices for the Aij/Bij fields
				Index index = (Index)(i + number_of_atom_types_ * j);
				Index sym_index = (Index)(j + number_of_atom_types_ * i);

				if (is_defined_[j] && is_defined_[i])
				{
					// calculate the values for A and B if in eps/R format
					if (format_ == EPSILON_R_FORMAT)
					{
						double R;
						if (!use_geometric_mean)
						{
							R = B_[i] + B_[j];
						}
						else
						{
							R = 2.0 * sqrt(B_[i] * B_[j]);
						}
						double R3 = R * R * R;
						double R6 = R3 * R3;
						double epsilon = sqrt(A_[i] * A_[j]);
						Aij_[index] = epsilon * R6 * R6;
						Bij_[index] = 2.0 * epsilon * R6;
					} 
					else 
					{
						// compute and assign Aij/Bij:
						// Aij = Ai * Aj, Aji = Aij
						// Bij = Bi * Bj, Bji = Bij
						Aij_[index] = A_[i] * A_[j];
						Bij_[index] = B_[i] * B_[j];
					}
					Aij_[sym_index] = Aij_[index];
					Bij_[sym_index] = Bij_[index];
				} 
				else	
				{
					Aij_[index] = 0.0;
					Bij_[index] = 0.0;
					Aij_[sym_index] = 0.0;
					Bij_[sym_index] = 0.0;
				}				
			}
		}

		return true;
	}


	bool LennardJones::hasParameters(Atom::Type I, Atom::Type J) const
		
	{
		if (I < 0 || I >= (Index)number_of_atom_types_)
		{
			return false;
		}

		if (J < 0 || J >= (Index)number_of_atom_types_)
		{
			return false;
		}

		return (is_defined_[I] && is_defined_[J]);
	}


	LennardJones::Values LennardJones::getParameters(Atom::Type I, Atom::Type J) const 
	{
		LennardJones::Values parameters;
		assignParameters(parameters, I, J);
		return parameters;
	}


	bool LennardJones::assignParameters(LennardJones::Values& parameters, Atom::Type I, Atom::Type J) const 
		
	{
		if (hasParameters(I, J)) 
		{
			parameters.A = Aij_[I * number_of_atom_types_ + J];
			parameters.B = Bij_[I * number_of_atom_types_ + J];
			
			return true;
		}

		return false;
	}


	bool LennardJones::operator == (const LennardJones& lj) const 
	{
		return (ParameterSection::operator == (lj)
						&& (A_ 		== lj.A_) 
						&& (B_ 		== lj.B_)
						&& (Aij_ 	== lj.Aij_) 
						&& (Bij_ 	== lj.Bij_));
	}
	 
} // namespace BALL