Line data Source code
1 : // Copyright (C) 2015 Technische Universitaet Muenchen
2 : // This file is part of the Mamico project. For conditions of distribution
3 : // and use, please see the copyright notice in Mamico's main folder, or at
4 : // www5.in.tum.de/mamico
5 : #ifndef _MOLECULARDYNAMICS_COUPLING_ADDITIVEMOMENTUMINSERTION_H_
6 : #define _MOLECULARDYNAMICS_COUPLING_ADDITIVEMOMENTUMINSERTION_H_
7 :
8 : #include "coupling/MomentumInsertion.h"
9 : #include "coupling/cell-mappings/ComputeMassMapping.h"
10 : #include "coupling/cell-mappings/SetMomentumMapping.h"
11 : #include "coupling/datastructures/CouplingCell.h"
12 : #include "tarch/la/Vector.h"
13 :
14 : /** @brief everything necessary for coupling operations, is defined in here */
15 : namespace coupling {
16 : template <class LinkedCell, unsigned int dim> class AdditiveMomentumInsertion;
17 : }
18 :
19 : /** This class allows to add momentum to molecules. In each MD timestep, it
20 : * takes a respective fraction from the momentum buffer of a coupling cell
21 : * and adds this momentum to the molecules of the coupling cell.
22 : * @brief used to manipulate the momentum/velocity of the molecules contained
23 : * in a coupling cell.
24 : * @tparam LinkedCell the LinkedCell class is given by the implementation of
25 : * linked cells in the molecular dynamics simulation
26 : * @tparam dim the integer dim refers to the spacial dimension of the
27 : * simulation, can be 1, 2, or 3
28 : * @author Philipp Neumann
29 : */
30 : template <class LinkedCell, unsigned int dim> class coupling::AdditiveMomentumInsertion : public coupling::MomentumInsertion<LinkedCell, dim> {
31 : public:
32 : /** @brief A simple constructor
33 : * @param mdSolverInterface The interface of the molecular dynamics solver
34 : * in application
35 : * @param numberMDTimestepsPerCouplingCycle The number of molecular dynamics
36 : * timesteps within one coupling cycle */
37 0 : AdditiveMomentumInsertion(coupling::interface::MDSolverInterface<LinkedCell, dim>* const mdSolverInterface, unsigned int numberMDTimestepsPerCouplingCycle)
38 0 : : coupling::MomentumInsertion<LinkedCell, dim>(mdSolverInterface), _numberMDTimestepsPerCouplingCycle(numberMDTimestepsPerCouplingCycle) {}
39 :
40 : /** @brief A simple destructor*/
41 0 : ~AdditiveMomentumInsertion() {}
42 :
43 : /** returns 1 since momentum shall be inserted in every time step
44 : * @brief returns the interval of time steps for momentum insertion
45 : * @returns 1 */
46 0 : unsigned int getTimeIntervalPerMomentumInsertion() const override { return 1; }
47 :
48 : /** inserts the momentum of the coupling cell and distributes it over all
49 : * molecules. This method does not conserve the kinetic energy of the
50 : * respective coupling cell. To conserve the energy as well, see the
51 : * description of MomentumController on details how to do that.
52 : * @brief inserts momentum to the cell
53 : * @param cell coupling cell to insert momentum to
54 : * @param idx coupling cell index of the cell */
55 0 : void insertMomentum(coupling::datastructures::CouplingCellWithLinkedCells<LinkedCell, dim>& cell, I02 idx) const override {
56 0 : const unsigned int timeIntervalMomentumInsertion = getTimeIntervalPerMomentumInsertion();
57 : // determine fraction of momentum that is to be inserted in this frame
58 0 : double fraction = 1.0 / ((_numberMDTimestepsPerCouplingCycle / timeIntervalMomentumInsertion) +
59 0 : (_numberMDTimestepsPerCouplingCycle % timeIntervalMomentumInsertion != 0));
60 0 : tarch::la::Vector<dim, double> momentum(fraction * cell.getMicroscopicMomentum());
61 0 : tarch::la::Vector<dim, double> zeroMomentum(0.0);
62 :
63 : // if there is some momentum to be transferred, do so
64 0 : if (tarch::la::dot(momentum, momentum) != 0.0) {
65 0 : coupling::cellmappings::ComputeMassMapping<LinkedCell, dim> computeMassMapping(coupling::MomentumInsertion<LinkedCell, dim>::_mdSolverInterface);
66 0 : cell.iterateConstCells(computeMassMapping);
67 : unsigned int numberParticles = computeMassMapping.getNumberOfParticles();
68 0 : coupling::cellmappings::SetMomentumMapping<LinkedCell, dim> setMomentumMapping(zeroMomentum, momentum, numberParticles,
69 0 : coupling::MomentumInsertion<LinkedCell, dim>::_mdSolverInterface);
70 0 : cell.iterateCells(setMomentumMapping);
71 0 : }
72 0 : }
73 :
74 0 : void setInnerImposition(bool enable) override {
75 0 : throw std::runtime_error(std::string("coupling::AdditiveMomentumInsertion::setInnerImposition not implemented"));
76 : }
77 :
78 : private:
79 : const unsigned int _numberMDTimestepsPerCouplingCycle; ///< The number of molecular dynamics
80 : ///< timesteps within one coupling
81 : ///< cycle */
82 : };
83 : #endif // _MOLECULARDYNAMICS_COUPLING_ADDITIVEMOMENTUMINSERTION_H_
|