File: snapShot.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 239,848 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 93
file content (396 lines) | stat: -rw-r--r-- 8,335 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
//

#include <BALL/MOLMEC/COMMON/snapShot.h>
#include <BALL/KERNEL/system.h>

using namespace std;

namespace BALL
{

	// The default constructor of the SnapShot class. 
  SnapShot::SnapShot()
		:	index_(0),
			number_of_atoms_(0),
			potential_energy_(0.0),
			kinetic_energy_(0.0),
			atom_positions_(0),
			atom_velocities_(0),
			atom_forces_(0)
	{
	}


	// The copy constructor of the SnapShot class. 
	SnapShot::SnapShot (const SnapShot& snapshot)
		:	index_(snapshot.index_),
			number_of_atoms_(snapshot.number_of_atoms_),
			potential_energy_(snapshot.potential_energy_),
			kinetic_energy_(snapshot.kinetic_energy_),
			atom_positions_(snapshot.atom_positions_),
			atom_velocities_(snapshot.atom_velocities_),
			atom_forces_(snapshot.atom_forces_)
	{
	}


	// The destructor just deletes all STL components (superfluous and just
	// for clarity) 
	SnapShot::~SnapShot()
	{
		clear();
	}


	// The assignment operator 
	const SnapShot& SnapShot::operator = (const SnapShot& snapshot)
	{
		index_ = snapshot.index_;
		number_of_atoms_ = snapshot.number_of_atoms_;
		potential_energy_ = snapshot.potential_energy_;
		kinetic_energy_ = snapshot.kinetic_energy_;
		atom_positions_ = snapshot.atom_positions_;
		atom_velocities_ = snapshot.atom_velocities_;
		atom_forces_ = snapshot.atom_forces_;

		return *this;
	}


	void SnapShot::clear()
	{
		index_ = 0;
		number_of_atoms_ = 0;
		potential_energy_ = 0.0;
		kinetic_energy_ = 0.0;

		atom_positions_.clear();
		atom_velocities_.clear();
		atom_forces_.clear();
	}


	bool SnapShot::operator == (const SnapShot& snapshot) const
	{
		return
			   (index_ == snapshot.index_)
			&& (number_of_atoms_ == snapshot.number_of_atoms_)
			&& (potential_energy_ == snapshot.potential_energy_)
			&& (kinetic_energy_ == snapshot.kinetic_energy_)
			&& (atom_positions_ == snapshot.atom_positions_)
			&& (atom_velocities_ == snapshot.atom_velocities_)
			&& (atom_forces_ == snapshot.atom_forces_);
	}


	bool SnapShot::isValid() const
	{
		return 	number_of_atoms_ > 0 &&
					 	number_of_atoms_ == atom_forces_.size() &&
						number_of_atoms_ == atom_velocities_.size() &&
						number_of_atoms_ == atom_positions_.size();
	}


	void SnapShot::setIndex(Size index)
	{
		index_ = index;
	}


	Size SnapShot::getIndex() const
	{
		return index_;
	}


	void SnapShot::setNumberOfAtoms(Size number_of_atoms)
	{
		number_of_atoms_ = number_of_atoms;
	}


	Size SnapShot::getNumberOfAtoms() const
	{
		return number_of_atoms_;
	}


	DoubleReal SnapShot::getPotentialEnergy() const
	{
		return potential_energy_;
	}


	void SnapShot::setPotentialEnergy(DoubleReal potential_energy)
	{
		potential_energy_ = potential_energy;
	}


	DoubleReal SnapShot::getKineticEnergy() const
	{
		return kinetic_energy_;
	}


	void SnapShot::setKineticEnergy(DoubleReal kinetic_energy)
	{
		kinetic_energy_ = kinetic_energy;
	}


	void SnapShot::setAtomPositions(const ::std::vector<Vector3>& atom_postions)
	{
		atom_positions_ = atom_postions;
	}


	const ::std::vector<Vector3>& SnapShot::getAtomPositions() const
	{
		return atom_positions_;
	}


	void SnapShot::setAtomVelocities(const ::std::vector<Vector3>& atom_velocities)
	{
		atom_velocities_ = atom_velocities;
	}


	const ::std::vector<Vector3>& SnapShot::getAtomVelocities() const
	{
		return atom_velocities_;
	}


	void SnapShot::setAtomForces(const ::std::vector<Vector3>& atom_forces)
	{
		atom_forces_ = atom_forces;
	}


	const ::std::vector<Vector3>& SnapShot::getAtomForces() const
	{
		return atom_forces_;
	}


	void SnapShot::takeSnapShot(const System& system)
	{
		number_of_atoms_ = system.countAtoms();

		AtomConstIterator atom_it = system.beginAtom();

		// reserve memory
		atom_positions_.resize(number_of_atoms_);
		atom_velocities_.resize(number_of_atoms_);
		atom_forces_.resize(number_of_atoms_);

		// This is the data section of the snapshot object 
		for (Size i = 0; +atom_it; ++atom_it, ++i)
		{
			atom_positions_[i] = atom_it->getPosition();
			atom_velocities_[i] = atom_it->getVelocity();
			atom_forces_[i] = atom_it->getForce();
		}
	}


	void SnapShot::applySnapShot(System& system) const
	{
		if (system.countAtoms() != number_of_atoms_)
		{
			Log.error () << "SnapShot::applySnapShot(): "
				<< "Atom counts do not match: System: " << system.countAtoms()
				<< " SnapShot: " << number_of_atoms_ << endl;
			return;
		}

		AtomIterator atom_it = system.beginAtom();

		for (Size i = 0; +atom_it; ++atom_it, ++i)
		{
			if (!atom_positions_.empty())
			{
				atom_it->setPosition(atom_positions_[i]);
			}
			if (!atom_velocities_.empty())
			{
				atom_it->setVelocity(atom_velocities_[i]);
			}
			if (!atom_forces_.empty())
			{
				atom_it->setForce(atom_forces_[i]);
			}
		}
	}


	void SnapShot::getAtomPositions(const System& system)
	{
		// obtain the number of atoms
		number_of_atoms_ = system.countAtoms();

		// reserve memory 
		atom_positions_.resize(number_of_atoms_);

		// copy data
		AtomConstIterator atom_it = system.beginAtom();
		for (Size i = 0; +atom_it; ++atom_it, ++i)
		{
			atom_positions_[i] = atom_it->getPosition();
		}
	}


	void SnapShot::setAtomPositions(System& system) const
	{
		if (system.countAtoms() != number_of_atoms_)
		{
			Log.error () << "SnapShot::setAtomPositions(): "
				<< "Atom counts do not match: System: " << system.countAtoms()
				<< " SnapShot: " << number_of_atoms_ << endl;
			return;
		}

		AtomIterator atom_it = system.beginAtom();

		for (Size i = 0; +atom_it; ++atom_it, ++i)
		{
			atom_it->setPosition(atom_positions_[i]);
		}
	}


	void SnapShot::getAtomVelocities(const System& system)
	{
		number_of_atoms_ = system.countAtoms();
		atom_velocities_.resize(number_of_atoms_);
		AtomConstIterator atom_it = system.beginAtom();
		for (Size i = 0; +atom_it; ++atom_it, ++i)
		{
			atom_velocities_[i] = atom_it->getVelocity();
		}
	}


	void SnapShot::setAtomVelocitites(System& system) const
	{
		if (system.countAtoms() != number_of_atoms_)
		{
			Log.error () << "SnapShot::setAtomVelocitites(): "
				<< "Atom counts do not match: System: " << system.countAtoms()
				<< " SnapShot: " << number_of_atoms_ << endl;
			return;
		}

		AtomIterator atom_it = system.beginAtom();

		// This is the data section of the snapshot object 
		for (Size i = 0; +atom_it; ++atom_it, ++i)
		{
			atom_it->setVelocity(atom_velocities_[i]);
		}
	}


	void SnapShot::getAtomForces(const System& system)
	{
		number_of_atoms_ = system.countAtoms();
		atom_forces_.resize(number_of_atoms_);
		AtomConstIterator atom_it = system.beginAtom();
		for (Size i = 0; +atom_it; ++atom_it, ++i)
		{
			atom_forces_[i] = atom_it->getForce();
		}
	}


	void SnapShot::setAtomForces(System& system) const
	{
		if (system.countAtoms() != number_of_atoms_)
		{
			Log.error () << "SnapShot::setAtomForces(): "
				<< "Atom counts do not match: System: " << system.countAtoms()
				<< " SnapShot: " << number_of_atoms_ << endl;
			return;
		}

		AtomIterator atom_it = system.beginAtom();

		// This is the data section of the snapshot object 
		for (Size i = 0; +atom_it; ++atom_it, ++i)
		{
			atom_it->setForce(atom_forces_[i]);
		}
	}


	::std::ostream& operator << (::std::ostream& os, const SnapShot& ss)
	{
		os << ss.getNumberOfAtoms() << endl << endl;

		vector<Vector3> data = ss.getAtomPositions();

		os << "Atom positions:" << endl;
		vector<Vector3>::const_iterator it = data.begin();
		for (; it != data.end(); ++it)
		{
			os << *it << endl;
		}

		os << endl << "Atom velocities:" << endl;

		data = ss.getAtomVelocities();
		for (it = data.begin(); it != data.end(); ++it)
		{
			os << *it << endl;
		}

		os << endl << "Atom forces:" << endl;

		data = ss.getAtomForces();
		for (it = data.begin(); it != data.end(); ++it)
		{
			os << *it << endl;
		}
		return os;
	}


	::std::istream& operator >> (::std::istream& is, SnapShot& ss)
	{
		// ?????:
		// this istream does not check anything!

		Size number;
		is >> number;

		vector<Vector3> data;
		data.resize(number);

		for (Size i = 0; i < number; ++i)
		{
			is >> data[i];
		}
		ss.setAtomPositions(data);

		for (Size i = 0; i < number; ++i)
		{
			is >> data[i];
		}
		ss.setAtomVelocities(data);

		for (Size i = 0; i < number; ++i)
		{
			is >> data[i];
		}
		ss.setAtomForces(data);
		return is;
	}

}	// end of namespace BALL