source: src/Dynamics/AtomicForceManipulator.hpp@ 4a187d

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 4a187d was 51cdfd, checked in by Frederik Heber <heber@…>, 10 years ago

Extracted common functions from VerletForceIntegration into AtomicForceManipulator.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * AtomicForceManipulator.hpp
3 *
4 * Created on: Feb 23, 2011
5 * Author: heber
6 */
7
8#ifndef ATOMICFORCEMANIPULATOR_HPP_
9#define ATOMICFORCEMANIPULATOR_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include "Atom/atom.hpp"
17#include "Atom/AtomSet.hpp"
18#include "CodePatterns/Assert.hpp"
19#include "CodePatterns/Info.hpp"
20#include "CodePatterns/Log.hpp"
21#include "CodePatterns/Verbose.hpp"
22#include "Fragmentation/ForceMatrix.hpp"
23#include "Helpers/helpers.hpp"
24#include "Helpers/defs.hpp"
25#include "LinearAlgebra/Vector.hpp"
26#include "Thermostats/ThermoStatContainer.hpp"
27#include "Thermostats/Thermostat.hpp"
28#include "World.hpp"
29
30template <class T>
31class AtomicForceManipulator
32{
33public:
34 /** Constructor of class AtomicForceManipulator.
35 *
36 * \param _atoms set of atoms to integrate
37 * \param _Deltat time step width in atomic units
38 * \param _IsAngstroem whether length units are in angstroem or bohr radii
39 */
40 AtomicForceManipulator(AtomSetMixin<T> &_atoms, double _Deltat, bool _IsAngstroem) :
41 Deltat(_Deltat),
42 IsAngstroem(_IsAngstroem),
43 atoms(_atoms)
44 {}
45 /** Destructor of class AtomicForceManipulator.
46 *
47 */
48 ~AtomicForceManipulator()
49 {}
50
51 /** Parses nuclear forces from file.
52 * Forces are stored in the time step \a TimeStep in the atomicForces in \a atoms.
53 * \param *file filename
54 * \param TimeStep time step to parse forces file into
55 * \return true - file parsed, false - file not found or imparsable
56 */
57 bool parseForcesFile(const char *file, const int TimeStep)
58 {
59 Info FunctionInfo(__func__);
60 ForceMatrix Force;
61
62 // parse file into ForceMatrix
63 std::ifstream input(file);
64 if ((input.good()) && (!Force.ParseMatrix(input, 0,0,0))) {
65 ELOG(0, "Could not parse Force Matrix file " << file << ".");
66 return false;
67 }
68 input.close();
69 if (Force.RowCounter[0] != (int)atoms.size()) {
70 ELOG(0, "Mismatch between number of atoms in file " << Force.RowCounter[0] << " and in molecule " << atoms.size() << ".");
71 return false;
72 }
73
74 addForceMatrixToAtomicForce(Force, TimeStep, 1);
75 return true;
76 }
77
78protected:
79 void addForceMatrixToAtomicForce(const ForceMatrix &Force, const int &TimeStep, const int offset)
80 {
81 // place forces from matrix into atoms
82 Vector tempVector;
83 size_t i=0;
84 for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter,++i) {
85 for(size_t d=0;d<NDIM;d++) {
86 tempVector[d] = Force.Matrix[0][i][d+offset]*(IsAngstroem ? AtomicLengthToAngstroem : 1.);
87 }
88 tempVector += (*iter)->getAtomicForceAtStep(TimeStep);
89 (*iter)->setAtomicForceAtStep(TimeStep, tempVector);
90 }
91 }
92
93 void correctForceMatrixForFixedCenterOfMass(const size_t offset, const int &TimeStep) {
94 Vector ForceVector;
95 // correct Forces
96 //std::cout << "Force before correction, " << Force << std::endl;
97 ForceVector.Zero();
98 for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
99 ForceVector += (*iter)->getAtomicForceAtStep(TimeStep);
100 }
101 ForceVector.Scale(1./(double)atoms.size());
102 //std::cout << "Force before second correction, " << Force << std::endl;
103 for(typename AtomSetMixin<T>::iterator iter = atoms.begin(); iter != atoms.end(); ++iter) {
104 const Vector tempVector = (*iter)->getAtomicForceAtStep(TimeStep) - ForceVector;
105 (*iter)->setAtomicForceAtStep(TimeStep, tempVector);
106 }
107 LOG(3, "INFO: forces corrected by " << ForceVector << " each.");
108 }
109
110 void correctVelocitiesForFixedCenterOfMass(const int &TimeStep) {
111 Vector Velocity;
112 double IonMass;
113 // correct velocities (rather momenta) so that center of mass remains motionless
114 Velocity = atoms.totalMomentumAtStep(TimeStep);
115 IonMass = atoms.totalMass();
116
117 // correct velocities (rather momenta) so that center of mass remains motionless
118 Velocity *= 1./IonMass;
119 atoms.addVelocityAtStep(-1.*Velocity,TimeStep);
120
121 LOG(3, "INFO: Velocities corrected by " << Velocity << " each.");
122 }
123
124 void performThermostatControl(const int &TimeStep) {
125 double ActualTemp;
126
127 // calculate current temperature
128 ActualTemp = atoms.totalTemperatureAtStep(TimeStep);
129 LOG(3, "INFO: Current temperature is " << ActualTemp);
130
131 // rescale to desired value
132 double ekin = World::getInstance().getThermostats()->getActive()->scaleAtoms(TimeStep,ActualTemp,atoms);
133 ActualTemp = atoms.totalTemperatureAtStep(TimeStep);
134 LOG(3, "INFO: New temperature after thermostat is " << ActualTemp);
135 LOG(1, "Kinetic energy is " << ekin << ".");
136 }
137
138protected:
139 double Deltat;
140 bool IsAngstroem;
141 AtomSetMixin<T> atoms;
142};
143
144#endif /* ATOMICFORCEMANIPULATOR_HPP_ */
Note: See TracBrowser for help on using the repository browser.