• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

step/stepcore

  • sources
  • kde-4.12
  • kdeedu
  • step
  • stepcore
gas.cc
Go to the documentation of this file.
1 /* This file is part of StepCore library.
2  Copyright (C) 2007 Vladimir Kuznetsov <ks.vladimir@gmail.com>
3 
4  StepCore library is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  StepCore library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with StepCore; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include "gas.h"
20 #include "types.h"
21 #include "constants.h"
22 #include <cstdlib>
23 #include <QtGlobal>
24 
25 namespace StepCore
26 {
27 
28 STEPCORE_META_OBJECT(GasParticle, QT_TRANSLATE_NOOP("ObjectClass", "GasParticle"), QT_TR_NOOP("Gas particle"), 0, STEPCORE_SUPER_CLASS(Particle),)
29 
30 STEPCORE_META_OBJECT(GasLJForce, QT_TRANSLATE_NOOP("ObjectClass", "GasLJForce"), QT_TR_NOOP("Lennard-Jones force"), 0,
31  STEPCORE_SUPER_CLASS(Item) STEPCORE_SUPER_CLASS(Force),
32  STEPCORE_PROPERTY_RW(double, depth, QT_TRANSLATE_NOOP("PropertyName", "depth"), QT_TRANSLATE_NOOP("Units", "J"), QT_TR_NOOP("Potential depth"), depth, setDepth)
33  STEPCORE_PROPERTY_RW(double, rmin, QT_TRANSLATE_NOOP("PropertyName", "rmin"), QT_TRANSLATE_NOOP("Units", "m"), QT_TR_NOOP("Distance at which the force is zero"), rmin, setRmin)
34  STEPCORE_PROPERTY_RW(double, cutoff, QT_TRANSLATE_NOOP("PropertyName", "cutoff"), QT_TRANSLATE_NOOP("Units", "m"), QT_TR_NOOP("Cut-off distance"), cutoff, setCutoff))
35 
36 STEPCORE_META_OBJECT(GasLJForceErrors, QT_TRANSLATE_NOOP("ObjectClass", "GasLJForceErrors"), QT_TR_NOOP("Errors class for GasLJForce"), 0,
37  STEPCORE_SUPER_CLASS(ObjectErrors),
38  STEPCORE_PROPERTY_RW(double, depthVariance, QT_TRANSLATE_NOOP("PropertyName", "depthVariance"), QT_TRANSLATE_NOOP("Units", "J"),
39  QT_TR_NOOP("Potential depth variance"), depthVariance, setDepthVariance)
40  STEPCORE_PROPERTY_RW(double, rminVariance, QT_TRANSLATE_NOOP("PropertyName", "rminVariance"), QT_TRANSLATE_NOOP("Units", "m"),
41  QT_TR_NOOP("Variance of the distance at which the force is zero"), rminVariance, setRminVariance))
42 
43 // XXX: Check units for 2d
44 // XXX: add cmPosition and cmVelocity
45 STEPCORE_META_OBJECT(Gas, QT_TRANSLATE_NOOP("ObjectClass", "Gas"), QT_TR_NOOP("Particle gas"), 0, STEPCORE_SUPER_CLASS(ItemGroup),
46  STEPCORE_PROPERTY_RW(StepCore::Vector2d, measureRectCenter, QT_TRANSLATE_NOOP("PropertyName", "measureRectCenter"), QT_TRANSLATE_NOOP("Units", "m"),
47  QT_TR_NOOP("Center of the rect for measurements"), measureRectCenter, setMeasureRectCenter)
48  STEPCORE_PROPERTY_RW(StepCore::Vector2d, measureRectSize, QT_TRANSLATE_NOOP("PropertyName", "measureRectSize"), QT_TRANSLATE_NOOP("Units", "m"),
49  QT_TR_NOOP("Size of the rect for measurements"), measureRectSize, setMeasureRectSize)
50  STEPCORE_PROPERTY_R_D(double, rectVolume, QT_TRANSLATE_NOOP("PropertyName", "rectVolume"), STEPCORE_FROM_UTF8(QT_TRANSLATE_NOOP("Units", "m²")),
51  QT_TR_NOOP("Volume of the measureRect"), rectVolume)
52  STEPCORE_PROPERTY_R_D(double, rectParticleCount, QT_TRANSLATE_NOOP("PropertyName", "rectParticleCount"), STEPCORE_UNITS_1,
53  QT_TR_NOOP("Count of particles in the measureRect"), rectParticleCount)
54  STEPCORE_PROPERTY_R_D(double, rectConcentration, QT_TRANSLATE_NOOP("PropertyName", "rectConcentration"), STEPCORE_FROM_UTF8(QT_TRANSLATE_NOOP("Units", "1/m²")),
55  QT_TR_NOOP("Concentration of particles in the measureRect"), rectConcentration)
56  STEPCORE_PROPERTY_R_D(double, rectPressure, QT_TRANSLATE_NOOP("PropertyName", "rectPressure"), QT_TRANSLATE_NOOP("Units", "Pa"),
57  QT_TR_NOOP("Pressure of particles in the measureRect"), rectPressure)
58  STEPCORE_PROPERTY_R_D(double, rectTemperature, QT_TRANSLATE_NOOP("PropertyName", "rectTemperature"), QT_TRANSLATE_NOOP("Units", "K"),
59  QT_TR_NOOP("Temperature of particles in the measureRect"), rectTemperature)
60  STEPCORE_PROPERTY_R_D(double, rectMeanKineticEnergy, QT_TRANSLATE_NOOP("PropertyName", "rectMeanKineticEnergy"), QT_TRANSLATE_NOOP("Units", "J"),
61  QT_TR_NOOP("Mean kinetic energy of particles in the measureRect"), rectMeanKineticEnergy)
62  STEPCORE_PROPERTY_R_D(StepCore::Vector2d, rectMeanVelocity, QT_TRANSLATE_NOOP("PropertyName", "rectMeanVelocity"), QT_TRANSLATE_NOOP("Units", "m/s"),
63  QT_TR_NOOP("Mean velocity of particles in the measureRect"), rectMeanVelocity)
64  STEPCORE_PROPERTY_R_D(double, rectMeanParticleMass, QT_TRANSLATE_NOOP("PropertyName", "rectMeanParticleMass"), QT_TRANSLATE_NOOP("Units", "kg"),
65  QT_TR_NOOP("Mean mass of particles in the measureRect"), rectMeanParticleMass)
66  STEPCORE_PROPERTY_R_D(double, rectMass, QT_TRANSLATE_NOOP("PropertyName", "rectMass"), QT_TRANSLATE_NOOP("Units", "kg"),
67  QT_TR_NOOP("Total mass of particles in the measureRect"), rectMass)
68  )
69 
70 STEPCORE_META_OBJECT(GasErrors, QT_TRANSLATE_NOOP("ObjectClass", "GasErrors"), QT_TR_NOOP("Errors class for Gas"), 0, STEPCORE_SUPER_CLASS(ObjectErrors),
71  STEPCORE_PROPERTY_R_D(double, rectPressureVariance, QT_TRANSLATE_NOOP("PropertyName", "rectPressureVariance"), QT_TRANSLATE_NOOP("Units", "Pa"),
72  QT_TR_NOOP("Variance of pressure of particles in the measureRect"), rectPressureVariance)
73  STEPCORE_PROPERTY_R_D(double, rectTemperatureVariance, QT_TRANSLATE_NOOP("PropertyName", "rectTemperatureVariance"), QT_TRANSLATE_NOOP("Units", "K"),
74  QT_TR_NOOP("Variance of temperature of particles in the measureRect"), rectTemperatureVariance)
75  STEPCORE_PROPERTY_R_D(double, rectMeanKineticEnergyVariance, QT_TRANSLATE_NOOP("PropertyName", "rectMeanKineticEnergyVariance"), QT_TRANSLATE_NOOP("Units", "J"),
76  QT_TR_NOOP("Variance of mean kinetic energy of particles in the measureRect"), rectMeanKineticEnergyVariance)
77  STEPCORE_PROPERTY_R_D(StepCore::Vector2d, rectMeanVelocityVariance, QT_TRANSLATE_NOOP("PropertyName", "rectMeanVelocityVariance"), QT_TRANSLATE_NOOP("Units", "m/s"),
78  QT_TR_NOOP("Variance of mean velocity of particles in the measureRect"), rectMeanVelocityVariance)
79  STEPCORE_PROPERTY_R_D(double, rectMeanParticleMassVariance, QT_TRANSLATE_NOOP("PropertyName", "rectMeanParticleMassVariance"), QT_TRANSLATE_NOOP("Units", "kg"),
80  QT_TR_NOOP("Variance of mean mass of particles in the measureRect"), rectMeanParticleMassVariance)
81  STEPCORE_PROPERTY_R_D(double, rectMassVariance, QT_TRANSLATE_NOOP("PropertyName", "rectMassVariance"), QT_TRANSLATE_NOOP("Units", "kg"),
82  QT_TR_NOOP("Variance of total mass of particles in the measureRect"), rectMassVariance)
83  )
84 
85 GasLJForce* GasLJForceErrors::gasLJForce() const
86 {
87  return static_cast<GasLJForce*>(owner());
88 }
89 
90 GasLJForce::GasLJForce(double depth, double rmin, double cutoff)
91  : _depth(depth), _rmin(rmin), _cutoff(cutoff)
92 {
93  calcABC();
94 }
95 
96 void GasLJForce::calcABC()
97 {
98  double m = 12*_depth;
99  _rmin6 = pow(_rmin, 6);
100  _rmin12 = _rmin6*_rmin6;
101  _a = m*_rmin12; _b = m*_rmin6;
102  _c = _cutoff*_cutoff;
103 }
104 
105 void GasLJForce::calcForce(bool calcVariances)
106 {
107  if(!group()) return;
108 
109  // NOTE: Currently we are handling only children of the same group
110  const ItemList::const_iterator end = group()->items().end();
111  for(ItemList::const_iterator i1 = group()->items().begin(); i1 != end; ++i1) {
112  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
113  for(ItemList::const_iterator i2 = i1+1; i2 != end; ++i2) {
114  if(!(*i2)->metaObject()->inherits<GasParticle>()) continue;
115 
116  GasParticle* p1 = static_cast<GasParticle*>(*i1);
117  GasParticle* p2 = static_cast<GasParticle*>(*i2);
118  Vector2d r = p2->position() - p1->position();
119  double rsquaredNorm = r.squaredNorm();
120  if(rsquaredNorm < _c) {
121  double rnorm6 = rsquaredNorm*rsquaredNorm*rsquaredNorm;
122  double rnorm8 = rnorm6*rsquaredNorm;
123  Vector2d force = r * ((_a/rnorm6 - _b)/rnorm8);
124  p2->applyForce(force);
125  force = -force;
126  p1->applyForce(force);
127 
128  if(calcVariances) {
129  ParticleErrors* pe1 = p1->particleErrors();
130  ParticleErrors* pe2 = p2->particleErrors();
131  Vector2d rV = pe2->positionVariance() + pe1->positionVariance();
132 
133  GasLJForceErrors* ge = gasLJForceErrors();
134  Vector2d forceV = r.cwise().square() * (
135  ge->_rminVariance * square( (12*_a/_rmin/rnorm6 - 6*_b/_rmin)/rnorm8 ) +
136  ge->_depthVariance * square( 12*(_rmin12/rnorm6 - _rmin6)/rnorm8 ) );
137 
138  forceV[0] += rV[0] * square( (_a/rnorm6*( 1 - 14*r[0]*r[0]/rsquaredNorm ) -
139  _b*( 1 - 8*r[0]*r[0]/rsquaredNorm ))/rnorm8 ) +
140  rV[1] * square( (_a/rnorm6*14 - _b*8)*r[0]*r[1]/(rnorm8*rsquaredNorm) );
141  forceV[1] += rV[1] * square( (_a/rnorm6*( 1 - 14*r[1]*r[1]/rsquaredNorm ) -
142  _b*( 1 - 8*r[1]*r[1]/rsquaredNorm ))/rnorm8 ) +
143  rV[0] * square( (_a/rnorm6*14 - _b*8)*r[0]*r[1]/(rnorm8*rsquaredNorm) );
144 
145  pe1->applyForceVariance(forceV);
146  pe2->applyForceVariance(forceV);
147  }
148  }
149  }
150  }
151 }
152 
153 Gas* GasErrors::gas() const
154 {
155  return static_cast<Gas*>(owner());
156 }
157 
158 double Gas::rectVolume() const
159 {
160  return _measureRectSize[0]*_measureRectSize[1];
161 }
162 
163 double Gas::randomUniform(double min, double max)
164 {
165  return double(std::rand())/RAND_MAX*(max-min) + min;
166 }
167 
168 double Gas::randomGauss(double mean, double deviation)
169 {
170  // Polar Box-Muller algorithm
171  double x1, x2, w;
172 
173  do {
174  x1 = 2.0 * double(std::rand())/RAND_MAX - 1.0;
175  x2 = 2.0 * double(std::rand())/RAND_MAX - 1.0;
176  w = x1 * x1 + x2 * x2;
177  } while( w >= 1.0 || w == 0 );
178 
179  w = sqrt( (-2.0 * log( w ) ) / w );
180  return x1 * w * deviation + mean;
181 }
182 
183 GasParticleList Gas::rectCreateParticles(int count,
184  double mass, double temperature,
185  const Vector2d& meanVelocity)
186 {
187  GasParticleList particles;
188 
189  Vector2d r0 = _measureRectCenter-_measureRectSize/2.0;
190  Vector2d r1 = _measureRectCenter+_measureRectSize/2.0;
191  double deviation = std::sqrt( Constants::Boltzmann * temperature / mass );
192 
193  for(int i=0; i<count; ++i) {
194  Vector2d pos(randomUniform(r0[0], r1[0]), randomUniform(r0[1], r1[1]));
195  Vector2d vel(randomGauss(meanVelocity[0], deviation), randomGauss(meanVelocity[1], deviation));
196  GasParticle* particle = new GasParticle(pos, vel, mass);
197  particles.push_back(particle);
198  }
199 
200  return particles;
201 }
202 
203 void Gas::addParticles(const GasParticleList& particles)
204 {
205  const GasParticleList::const_iterator end = particles.end();
206  for(GasParticleList::const_iterator it = particles.begin(); it != end; ++it) {
207  addItem(*it);
208  }
209 }
210 
211 double Gas::rectParticleCount() const
212 {
213  Vector2d r0 = _measureRectCenter-_measureRectSize/2.0;
214  Vector2d r1 = _measureRectCenter+_measureRectSize/2.0;
215 
216  double count = 0;
217  const ItemList::const_iterator end = items().end();
218  for(ItemList::const_iterator i1 = items().begin(); i1 != end; ++i1) {
219  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
220  GasParticle* p1 = static_cast<GasParticle*>(*i1);
221  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
222  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
223  ++count;
224  }
225 
226  return count;
227 }
228 
229 double Gas::rectMeanParticleMass() const
230 {
231  Vector2d r0 = _measureRectCenter-_measureRectSize/2.0;
232  Vector2d r1 = _measureRectCenter+_measureRectSize/2.0;
233 
234  double count = 0;
235  double mass = 0;
236 
237  const ItemList::const_iterator end = items().end();
238  for(ItemList::const_iterator i1 = items().begin(); i1 != end; ++i1) {
239  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
240  GasParticle* p1 = static_cast<GasParticle*>(*i1);
241  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
242  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
243  mass += p1->mass();
244  ++count;
245  }
246 
247  mass /= count;
248  return mass;
249 }
250 
251 double GasErrors::rectMeanParticleMassVariance() const
252 {
253  Vector2d r0 = gas()->_measureRectCenter-gas()->_measureRectSize/2.0;
254  Vector2d r1 = gas()->_measureRectCenter+gas()->_measureRectSize/2.0;
255 
256  double count = 0;
257 
258  double mass = gas()->rectMeanParticleMass();
259  double massVariance = 0;
260 
261  const ItemList::const_iterator end = gas()->items().end();
262  for(ItemList::const_iterator i1 = gas()->items().begin(); i1 != end; ++i1) {
263  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
264  GasParticle* p1 = static_cast<GasParticle*>(*i1);
265  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
266  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
267 
268  massVariance += square(p1->mass() - mass);
269 
270  ParticleErrors* pe1 = static_cast<ParticleErrors*>(p1->tryGetObjectErrors());
271  if(pe1) massVariance += pe1->massVariance();
272 
273  ++count;
274  }
275 
276  massVariance /= square(count);
277  return massVariance;
278 }
279 
280 double Gas::rectMass() const
281 {
282  Vector2d r0 = _measureRectCenter-_measureRectSize/2.0;
283  Vector2d r1 = _measureRectCenter+_measureRectSize/2.0;
284 
285  double mass = 0;
286 
287  const ItemList::const_iterator end = items().end();
288  for(ItemList::const_iterator i1 = items().begin(); i1 != end; ++i1) {
289  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
290  GasParticle* p1 = static_cast<GasParticle*>(*i1);
291  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
292  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
293  mass += p1->mass();
294  }
295 
296  return mass;
297 }
298 
299 double GasErrors::rectMassVariance() const
300 {
301  Vector2d r0 = gas()->_measureRectCenter-gas()->_measureRectSize/2.0;
302  Vector2d r1 = gas()->_measureRectCenter+gas()->_measureRectSize/2.0;
303 
304  double massVariance = 0;
305 
306  const ItemList::const_iterator end = gas()->items().end();
307  for(ItemList::const_iterator i1 = gas()->items().begin(); i1 != end; ++i1) {
308  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
309  GasParticle* p1 = static_cast<GasParticle*>(*i1);
310  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
311  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
312 
313  ParticleErrors* pe1 = static_cast<ParticleErrors*>(p1->tryGetObjectErrors());
314  if(pe1) massVariance += pe1->massVariance();
315  }
316 
317  return massVariance;
318 }
319 
320 double Gas::rectConcentration() const
321 {
322  return rectParticleCount() / rectVolume();
323 }
324 
325 Vector2d Gas::rectMeanVelocity() const
326 {
327  Vector2d r0 = _measureRectCenter-_measureRectSize/2.0;
328  Vector2d r1 = _measureRectCenter+_measureRectSize/2.0;
329 
330  double count = 0;
331  Vector2d velocity(0.,0.);
332 
333  const ItemList::const_iterator end = items().end();
334  for(ItemList::const_iterator i1 = items().begin(); i1 != end; ++i1) {
335  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
336  GasParticle* p1 = static_cast<GasParticle*>(*i1);
337  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
338  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
339  velocity += p1->velocity();
340  ++count;
341  }
342 
343  velocity /= count;
344  return velocity;
345 }
346 
347 Vector2d GasErrors::rectMeanVelocityVariance() const
348 {
349  Vector2d r0 = gas()->_measureRectCenter-gas()->_measureRectSize/2.0;
350  Vector2d r1 = gas()->_measureRectCenter+gas()->_measureRectSize/2.0;
351 
352  double count = 0;
353 
354  Vector2d velocity = gas()->rectMeanVelocity();
355  Vector2d velocityVariance(0.,0.);
356 
357  const ItemList::const_iterator end = gas()->items().end();
358  for(ItemList::const_iterator i1 = gas()->items().begin(); i1 != end; ++i1) {
359  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
360  GasParticle* p1 = static_cast<GasParticle*>(*i1);
361  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
362  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
363 
364  velocityVariance += (p1->velocity() - velocity).cwise().square();
365 
366  ParticleErrors* pe1 = static_cast<ParticleErrors*>(p1->tryGetObjectErrors());
367  if(pe1) velocityVariance += pe1->velocityVariance();
368 
369  ++count;
370  }
371 
372  velocityVariance /= square(count);
373  return velocityVariance;
374 }
375 
376 double Gas::rectMeanKineticEnergy() const
377 {
378  Vector2d r0 = _measureRectCenter-_measureRectSize/2.0;
379  Vector2d r1 = _measureRectCenter+_measureRectSize/2.0;
380 
381  double count = 0;
382  double energy = 0;
383 
384  const ItemList::const_iterator end = items().end();
385  for(ItemList::const_iterator i1 = items().begin(); i1 != end; ++i1) {
386  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
387  GasParticle* p1 = static_cast<GasParticle*>(*i1);
388  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
389  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
390  energy += p1->mass() * p1->velocity().squaredNorm();
391  ++count;
392  }
393 
394  energy /= (2.0*count);
395  return energy;
396 }
397 
398 double GasErrors::rectMeanKineticEnergyVariance() const
399 {
400  Vector2d r0 = gas()->_measureRectCenter-gas()->_measureRectSize/2.0;
401  Vector2d r1 = gas()->_measureRectCenter+gas()->_measureRectSize/2.0;
402 
403  double count = 0;
404 
405  double energy = 2*gas()->rectMeanKineticEnergy();
406  double energyVariance = 0;
407 
408  const ItemList::const_iterator end = gas()->items().end();
409  for(ItemList::const_iterator i1 = gas()->items().begin(); i1 != end; ++i1) {
410  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
411  GasParticle* p1 = static_cast<GasParticle*>(*i1);
412  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
413  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
414 
415  double pEnergy = p1->mass() * p1->velocity().squaredNorm();
416  energyVariance += square(pEnergy - energy);
417 
418  ParticleErrors* pe1 = static_cast<ParticleErrors*>(p1->tryGetObjectErrors());
419  if(pe1) {
420  energyVariance +=
421  pe1->massVariance() * square(p1->velocity().squaredNorm()) +
422  pe1->velocityVariance().dot(
423  (2*p1->mass()*p1->velocity()).cwise().square() );
424  }
425 
426  ++count;
427  }
428 
429  energyVariance /= square(2.0*count);
430  return energyVariance;
431 }
432 
433 double Gas::rectTemperature() const
434 {
435  Vector2d r0 = _measureRectCenter-_measureRectSize/2.0;
436  Vector2d r1 = _measureRectCenter+_measureRectSize/2.0;
437 
438  double count = 0;
439  double temperature = 0;
440 
441  StepCore::Vector2d meanVelocity = rectMeanVelocity();
442  const ItemList::const_iterator end = items().end();
443  for(ItemList::const_iterator i1 = items().begin(); i1 != end; ++i1) {
444  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
445  GasParticle* p1 = static_cast<GasParticle*>(*i1);
446  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
447  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
448  temperature += p1->mass() * (p1->velocity() - meanVelocity).squaredNorm();
449  ++count;
450  }
451 
452  // no 3/2 factor since we live in 2d
453  temperature /= (2.0*count*Constants::Boltzmann);
454  return temperature;
455 }
456 
457 double GasErrors::rectTemperatureVariance() const
458 {
459  Vector2d r0 = gas()->_measureRectCenter-gas()->_measureRectSize/2.0;
460  Vector2d r1 = gas()->_measureRectCenter+gas()->_measureRectSize/2.0;
461 
462  double count = 0;
463 
464  StepCore::Vector2d meanVelocity = gas()->rectMeanVelocity();
465  double temperature = 2.0*Constants::Boltzmann*gas()->rectTemperature();
466  double temperatureVariance = 0;
467 
468  const ItemList::const_iterator end = gas()->items().end();
469  for(ItemList::const_iterator i1 = gas()->items().begin(); i1 != end; ++i1) {
470  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
471  GasParticle* p1 = static_cast<GasParticle*>(*i1);
472  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
473  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
474 
475  double pTemperature = p1->mass() * (p1->velocity() - meanVelocity).squaredNorm();
476  temperatureVariance += square(pTemperature - temperature);
477 
478  ParticleErrors* pe1 = static_cast<ParticleErrors*>(p1->tryGetObjectErrors());
479  if(pe1) {
480  temperatureVariance +=
481  pe1->massVariance() * square((p1->velocity() - meanVelocity).squaredNorm()) +
482  pe1->velocityVariance().dot(
483  (p1->mass()*(p1->velocity() - meanVelocity)).cwise().square() );
484  }
485 
486  ++count;
487  }
488 
489  temperatureVariance /= square(2.0*Constants::Boltzmann*count);
490  // XXX: We could easily take into account BoltzmannError here
491  // but this can confuse users so for now we don't do it
492 
493  return temperatureVariance;
494 }
495 
496 // XXX: this formula is incorrect when forces are big
497 // XXX: use better formula (for example from lammps)
498 double Gas::rectPressure() const
499 {
500  Vector2d r0 = _measureRectCenter-_measureRectSize/2.0;
501  Vector2d r1 = _measureRectCenter+_measureRectSize/2.0;
502 
503  double pressure = 0;
504 
505  StepCore::Vector2d meanVelocity = rectMeanVelocity();
506  const ItemList::const_iterator end = items().end();
507  for(ItemList::const_iterator i1 = items().begin(); i1 != end; ++i1) {
508  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
509  GasParticle* p1 = static_cast<GasParticle*>(*i1);
510  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
511  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
512  pressure += p1->mass() * (p1->velocity() - meanVelocity).squaredNorm();
513  }
514 
515  pressure /= (2.0 * rectVolume());
516  return pressure;
517 }
518 
519 double GasErrors::rectPressureVariance() const
520 {
521  Vector2d r0 = gas()->_measureRectCenter-gas()->_measureRectSize/2.0;
522  Vector2d r1 = gas()->_measureRectCenter+gas()->_measureRectSize/2.0;
523 
524  StepCore::Vector2d meanVelocity = gas()->rectMeanVelocity();
525  double pressure = gas()->rectPressure();
526  double pressureVariance = 0;
527 
528  const ItemList::const_iterator end = gas()->items().end();
529  for(ItemList::const_iterator i1 = gas()->items().begin(); i1 != end; ++i1) {
530  if(!(*i1)->metaObject()->inherits<GasParticle>()) continue;
531  GasParticle* p1 = static_cast<GasParticle*>(*i1);
532  if(p1->position()[0] < r0[0] || p1->position()[0] > r1[0] ||
533  p1->position()[1] < r0[1] || p1->position()[1] > r1[1]) continue;
534 
535  double pPressure = p1->mass() * (p1->velocity() - meanVelocity).squaredNorm();
536  pressureVariance += square(pPressure - pressure);
537 
538  ParticleErrors* pe1 = static_cast<ParticleErrors*>(p1->tryGetObjectErrors());
539  if(pe1) {
540  pressureVariance +=
541  pe1->massVariance() * square((p1->velocity() - meanVelocity).squaredNorm()) +
542  pe1->velocityVariance().dot(
543  (p1->mass()*(p1->velocity() - meanVelocity)).cwise().square() );
544  }
545  }
546 
547  pressureVariance /= square(2.0*gas()->rectVolume());
548  return pressureVariance;
549 }
550 
551 }
552 
StepCore::Particle::mass
double mass() const
Get mass of the particle.
Definition: particle.h:138
StepCore::Gas
Gas - a group of several GasParticle and a force.
Definition: gas.h:170
StepCore::ParticleErrors::massVariance
double massVariance() const
Get mass variance.
Definition: particle.h:77
types.h
Type to and from string convertion helpers.
StepCore::GasParticle
Gas particle.
Definition: gas.h:39
StepCore::GasErrors
Errors object for Gas.
Definition: gas.h:144
StepCore::Vector2d
Eigen::Vector2d Vector2d
Two-dimensional vector with double components.
Definition: vector.h:29
StepCore::GasLJForceErrors::_rminVariance
double _rminVariance
Definition: gas.h:76
constants.h
Constants class.
StepCore::ParticleErrors::applyForceVariance
void applyForceVariance(const Vector2d &forceVariance)
Increment force variance.
Definition: particle.h:73
StepCore::STEPCORE_SUPER_CLASS
STEPCORE_SUPER_CLASS(CollisionSolver)
StepCore::rmin
rmin
Definition: gas.cc:33
StepCore::Particle::particleErrors
ParticleErrors * particleErrors()
Get (and possibly create) ParticleErrors object.
Definition: particle.h:164
StepCore::STEPCORE_FROM_UTF8
setAngleVariance setAngularVelocityVariance STEPCORE_FROM_UTF8(QT_TRANSLATE_NOOP("Units","rad/s²"))
StepCore::Item::tryGetObjectErrors
ObjectErrors * tryGetObjectErrors() const
Get ObjectErrors only if it already exists.
Definition: world.h:100
StepCore::GasLJForce::GasLJForce
GasLJForce(double depth=1, double rmin=1, double cutoff=HUGE_VAL)
Constructs GasLJForce.
StepCore::QT_TRANSLATE_NOOP
QT_TRANSLATE_NOOP("ObjectClass","GJKCollisionSolver")
StepCore::GasLJForceErrors::_depthVariance
double _depthVariance
Definition: gas.h:75
gas.h
Gas-related classes.
StepCore::GasParticleList
std::vector< GasParticle * > GasParticleList
Definition: gas.h:139
StepCore::ObjectErrors
Base class for all errors objects.
Definition: world.h:49
StepCore::ParticleErrors::positionVariance
const Vector2d & positionVariance() const
Get position variance.
Definition: particle.h:52
StepCore::QT_TR_NOOP
QT_TR_NOOP("Errors class for CoulombForce")
StepCore::STEPCORE_UNITS_1
STEPCORE_UNITS_1
Definition: world.cc:43
StepCore::GasLJForceErrors
Errors object for GasLJForce.
Definition: gas.h:52
StepCore::STEPCORE_PROPERTY_RW
STEPCORE_PROPERTY_RW(double, depth, QT_TRANSLATE_NOOP("PropertyName","depth"), QT_TRANSLATE_NOOP("Units","J"), QT_TR_NOOP("Potential depth"), depth, setDepth) STEPCORE_PROPERTY_RW(double
StepCore::ParticleErrors::velocityVariance
const Vector2d & velocityVariance() const
Get velocity variance.
Definition: particle.h:58
StepCore::ItemGroup
Groups several items together.
Definition: world.h:309
StepCore::Particle::applyForce
void applyForce(const Vector2d &force)
Apply force to the body.
Definition: particle.h:135
StepCore::Particle
Particle with mass.
Definition: particle.h:103
StepCore::rminVariance
setRmin rminVariance
Definition: gas.cc:40
StepCore::STEPCORE_PROPERTY_R_D
setRmin setRminVariance STEPCORE_PROPERTY_R_D(double, rectPressureVariance, QT_TRANSLATE_NOOP("PropertyName","rectPressureVariance"), QT_TRANSLATE_NOOP("Units","Pa"), QT_TR_NOOP("Variance of pressure of particles in the measureRect"), rectPressureVariance) STEPCORE_PROPERTY_R_D(double
StepCore::Particle::position
const Vector2d & position() const
Get position of the particle.
Definition: particle.h:117
StepCore::STEPCORE_META_OBJECT
STEPCORE_META_OBJECT(CollisionSolver, QT_TRANSLATE_NOOP("ObjectClass","CollisionSolver"),"CollisionSolver", MetaObject::ABSTRACT, STEPCORE_SUPER_CLASS(Object), STEPCORE_PROPERTY_RW(double, toleranceAbs, QT_TRANSLATE_NOOP("PropertyName","toleranceAbs"), STEPCORE_UNITS_1, QT_TR_NOOP("Allowed absolute tolerance"), toleranceAbs, setToleranceAbs) STEPCORE_PROPERTY_R_D(double, localError, QT_TRANSLATE_NOOP("PropertyName","localError"), STEPCORE_UNITS_1, QT_TR_NOOP("Maximal local error during last step"), localError)) STEPCORE_META_OBJECT(GJKCollisionSolver
StepCore::Particle::velocity
const Vector2d & velocity() const
Get velocity of the particle.
Definition: particle.h:122
StepCore::rectTemperatureVariance
setRmin setRminVariance rectTemperatureVariance
Definition: gas.cc:73
StepCore::square
T square(T v)
Definition: util.h:30
StepCore::ParticleErrors
Errors object for Particle.
Definition: particle.h:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:43:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

step/stepcore

Skip menu "step/stepcore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal