[194649] | 1 | /*
|
---|
| 2 | * Langevin.cpp
|
---|
| 3 | *
|
---|
| 4 | * Created on: Aug 20, 2010
|
---|
| 5 | * Author: crueger
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[d2b28f] | 8 | // include config.h
|
---|
| 9 | #ifdef HAVE_CONFIG_H
|
---|
| 10 | #include <config.h>
|
---|
| 11 | #endif
|
---|
| 12 |
|
---|
[ad011c] | 13 | #include "CodePatterns/MemDebug.hpp"
|
---|
[d2b28f] | 14 |
|
---|
[194649] | 15 | #include "Langevin.hpp"
|
---|
| 16 | #include "element.hpp"
|
---|
| 17 | #include "config.hpp"
|
---|
[ad011c] | 18 | #include "CodePatterns/Verbose.hpp"
|
---|
| 19 | #include "CodePatterns/Log.hpp"
|
---|
[255829] | 20 | #include "Helpers/defs.hpp"
|
---|
[194649] | 21 | #include "ThermoStatContainer.hpp"
|
---|
| 22 |
|
---|
[6625c3] | 23 | #include <boost/random/mersenne_twister.hpp>
|
---|
| 24 | #include <boost/random/normal_distribution.hpp>
|
---|
| 25 | #include <boost/random/variate_generator.hpp>
|
---|
| 26 |
|
---|
[a5028f] | 27 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
|
---|
| 28 | #include "RandomNumbers/RandomNumberGenerator.hpp"
|
---|
| 29 |
|
---|
[c0c650] | 30 | Langevin::Langevin(double _TempFrequency,double _alpha) :
|
---|
| 31 | TempFrequency(_TempFrequency),
|
---|
[6625c3] | 32 | alpha(_alpha),
|
---|
| 33 | rng_engine(new boost::mt19937),
|
---|
| 34 | rng_distribution(NULL)
|
---|
| 35 | {}
|
---|
[194649] | 36 |
|
---|
[3e4162] | 37 | Langevin::Langevin() :
|
---|
| 38 | TempFrequency(2.5),
|
---|
[6625c3] | 39 | alpha(0.),
|
---|
| 40 | rng_engine(new boost::mt19937),
|
---|
| 41 | rng_distribution(NULL)
|
---|
[3e4162] | 42 | {}
|
---|
| 43 |
|
---|
[194649] | 44 | Langevin::~Langevin()
|
---|
| 45 | {
|
---|
[6625c3] | 46 | delete rng_engine;
|
---|
| 47 | delete rng_distribution;
|
---|
[194649] | 48 | }
|
---|
| 49 |
|
---|
[14c57a] | 50 | const char *ThermostatTraits<Langevin>::name = "Langevin";
|
---|
| 51 |
|
---|
| 52 | std::string ThermostatTraits<Langevin>::getName(){
|
---|
| 53 | return ThermostatTraits<Langevin>::name;
|
---|
| 54 | }
|
---|
[579a81] | 55 |
|
---|
[14c57a] | 56 | Thermostat *ThermostatTraits<Langevin>::make(class ConfigFileBuffer * const fb){
|
---|
[c0c650] | 57 | double TempFrequency;
|
---|
| 58 | double alpha;
|
---|
| 59 | const int verbose = 0;
|
---|
| 60 | ParseForParameter(verbose,fb,"Thermostat", 0, 2, 1, double_type, &TempFrequency, 1, critical); // read gamma
|
---|
| 61 | if (ParseForParameter(verbose,fb,"Thermostat", 0, 3, 1, double_type, &alpha, 1, optional)) {
|
---|
| 62 | DoLog(2) && (Log() << Verbose(2) << "Extended Stochastic Thermostat detected with interpolation coefficient " << alpha << "." << endl);
|
---|
| 63 | } else {
|
---|
| 64 | alpha = 1.;
|
---|
| 65 | }
|
---|
[14c57a] | 66 | return new Langevin(TempFrequency,alpha);
|
---|
[c0c650] | 67 | }
|
---|
| 68 |
|
---|
[579a81] | 69 | double Langevin::scaleAtoms(unsigned int step,double ActualTemp,ATOMSET(std::list) atoms){
|
---|
| 70 | return doScaleAtoms(step,ActualTemp,atoms.begin(),atoms.end());
|
---|
[194649] | 71 | }
|
---|
| 72 |
|
---|
[579a81] | 73 | double Langevin::scaleAtoms(unsigned int step,double ActualTemp,ATOMSET(std::vector) atoms){
|
---|
| 74 | return doScaleAtoms(step,ActualTemp,atoms.begin(),atoms.end());
|
---|
[194649] | 75 | }
|
---|
| 76 |
|
---|
[579a81] | 77 | double Langevin::scaleAtoms(unsigned int step,double ActualTemp,ATOMSET(std::set) atoms){
|
---|
| 78 | return doScaleAtoms(step,ActualTemp,atoms.begin(),atoms.end());
|
---|
[194649] | 79 | }
|
---|
| 80 |
|
---|
| 81 | template <class ForwardIterator>
|
---|
[579a81] | 82 | double Langevin::doScaleAtoms(unsigned int step,double ActualTemp,ForwardIterator begin, ForwardIterator end){
|
---|
[194649] | 83 | DoLog(2) && (Log() << Verbose(2) << "Applying Langevin thermostat..." << endl);
|
---|
[6625c3] | 84 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator("mt19937", "normal_distribution");
|
---|
[a5028f] | 85 | const double rng_min = random.min();
|
---|
| 86 | const double rng_max = random.max();
|
---|
[194649] | 87 | double ekin=0;
|
---|
| 88 | for(ForwardIterator iter=begin;iter!=end;++iter){
|
---|
[51c3e4] | 89 | double sigma = sqrt(getContainer().TargetTemp/(*iter)->getType()->getMass()); // sigma = (k_b T)/m (Hartree/atomicmass = atomiclength/atomictime)
|
---|
[6625c3] | 90 | rng_distribution = new boost::normal_distribution<>(0,sigma);
|
---|
| 91 | boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > rng(*rng_engine, *rng_distribution);
|
---|
[056e70] | 92 | Vector U = (*iter)->getAtomicVelocityAtStep(step);
|
---|
[6625c3] | 93 | if ((*iter)->getFixedIon() == 0) { // even FixedIon moves, only not by other's forces
|
---|
[194649] | 94 | // throw a dice to determine whether it gets hit by a heat bath particle
|
---|
[a5028f] | 95 | if (((((random()/(rng_max-rng_min)))*TempFrequency) < 1.)) {
|
---|
[c0c650] | 96 | DoLog(3) && (Log() << Verbose(3) << "Particle " << (**iter) << " was hit (sigma " << sigma << "): " << U.Norm() << " -> ");
|
---|
[194649] | 97 | // pick three random numbers from a Boltzmann distribution around the desired temperature T for each momenta axis
|
---|
| 98 | for (int d=0; d<NDIM; d++) {
|
---|
[6625c3] | 99 | U[d] = rng();
|
---|
[194649] | 100 | }
|
---|
[c0c650] | 101 | DoLog(2) && (Log() << Verbose(2) << U.Norm() << endl);
|
---|
[194649] | 102 | }
|
---|
[51c3e4] | 103 | ekin += 0.5*(*iter)->getType()->getMass() * U.NormSquared();
|
---|
[194649] | 104 | }
|
---|
[056e70] | 105 | (*iter)->setAtomicVelocityAtStep(step, U);
|
---|
[6625c3] | 106 | delete rng_distribution;
|
---|
| 107 | rng_distribution = NULL;
|
---|
[194649] | 108 | }
|
---|
| 109 | return ekin;
|
---|
| 110 | }
|
---|
[579a81] | 111 |
|
---|
| 112 | std::string Langevin::name(){
|
---|
[14c57a] | 113 | return ThermostatTraits<Langevin>::name;
|
---|
[579a81] | 114 | }
|
---|
| 115 |
|
---|
| 116 | std::string Langevin::writeParams(){
|
---|
| 117 | stringstream sstr;
|
---|
| 118 | sstr << TempFrequency << "\t" << alpha;
|
---|
| 119 | return sstr.str();
|
---|
| 120 | }
|
---|