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_TRANSFERSTRATEGIES_DIFFERENCETRANSFERSTRATEGY_H_
6 : #define _MOLECULARDYNAMICS_COUPLING_TRANSFERSTRATEGIES_DIFFERENCETRANSFERSTRATEGY_H_
7 :
8 : #include "coupling/cell-mappings/ComputeMassMapping.h"
9 : #include "coupling/cell-mappings/ComputeMomentumMapping.h"
10 : #include "coupling/transferstrategies/TransferStrategy.h"
11 : #include "tarch/la/Vector.h"
12 :
13 : namespace coupling {
14 : namespace transferstrategies {
15 : template <class LinkedCell, unsigned int dim> class DifferenceTransferStrategy;
16 : }
17 : } // namespace coupling
18 :
19 : /** from MD to macroscopic solver: mass and momentum are computed at the current
20 : * timestep and mapped to macroscopic solver. The values are averaged over a
21 : * certain time. from macroscopic solver to MD: The difference in mass and
22 : * momentum between both solvers is computed and introduced on MD side.
23 : * @author Philipp Neumann
24 : * @tparam LinkedCell the LinkedCell class is given by the implementation of
25 : * linked cells in the molecular dynamics simulation
26 : * @tparam dim refers to the spacial dimension of the simulation, can be 1, 2,
27 : * or 3*/
28 : template <class LinkedCell, unsigned int dim>
29 : class coupling::transferstrategies::DifferenceTransferStrategy : public coupling::transferstrategies::TransferStrategy<LinkedCell, dim> {
30 : public:
31 : /** @brief a simple constructor
32 : * @param mdSolverInterface interface for the md solver
33 : * @param numberMDSteps number of md steps within one coupling time step */
34 0 : DifferenceTransferStrategy(coupling::interface::MDSolverInterface<LinkedCell, dim>* const mdSolverInterface, unsigned int numberMDsteps)
35 0 : : coupling::transferstrategies::TransferStrategy<LinkedCell, dim>(mdSolverInterface), _numberMDsteps(numberMDsteps), _zero(0.0),
36 0 : _massMapping(mdSolverInterface), _momentumMapping(mdSolverInterface) {}
37 :
38 : /** @brief a dummy destructor */
39 0 : virtual ~DifferenceTransferStrategy() {}
40 :
41 : /** @brief
42 : * @param cell coupling cell to process
43 : * @param index index of the coupling cell */
44 0 : void processInnerCouplingCellBeforeReceivingMacroscopicSolverData(coupling::datastructures::CouplingCellWithLinkedCells<LinkedCell, dim>& cell,
45 : I02 index) override {
46 : // reset quantities
47 0 : cell.setMicroscopicMass(0.0);
48 0 : cell.setMicroscopicMomentum(_zero);
49 0 : }
50 :
51 : /** @brief the microscopicMass and -Momentum are reseted to zero
52 : * @param cell coupling cell to process
53 : * @param index index of the coupling cell */
54 0 : void processOuterCouplingCellBeforeReceivingMacroscopicSolverData(coupling::datastructures::CouplingCellWithLinkedCells<LinkedCell, dim>& cell,
55 : I02 index) override {
56 : // reset quantities
57 0 : cell.setMicroscopicMass(0.0);
58 0 : cell.setMicroscopicMomentum(_zero);
59 0 : }
60 :
61 : /** @brief difference between microscopic and macroscopic values is evaluated
62 : * and stored in the macroscopic quantity
63 : * @param cell coupling cell to process
64 : * @param index index of the coupling cell */
65 0 : void processInnerCouplingCellAfterReceivingMacroscopicSolverData(coupling::datastructures::CouplingCellWithLinkedCells<LinkedCell, dim>& cell,
66 : I02 index) override {
67 : // compute difference between macroscopic and microscopic mass and momentum
68 : // values; this value is set in the microscopic data buffer of the
69 : // coupling cell.
70 0 : const double diffMass = cell.getMicroscopicMass() - cell.getMacroscopicMass();
71 0 : const tarch::la::Vector<dim, double> diffMomentum = cell.getMicroscopicMomentum() - cell.getMacroscopicMomentum();
72 0 : cell.setMicroscopicMass(diffMass);
73 0 : cell.setMicroscopicMomentum(diffMomentum);
74 : // reset coupling cell buffers
75 0 : cell.setMacroscopicMass(0.0);
76 0 : cell.setMacroscopicMomentum(_zero);
77 0 : }
78 :
79 : /** @brief the quantities (mass & momentum) are averaged (divided by the
80 : * amount of md time steps)
81 : * @param cell coupling cell to process
82 : * @param index index of the coupling cell */
83 0 : void processInnerCouplingCellBeforeSendingMDSolverData(coupling::datastructures::CouplingCellWithLinkedCells<LinkedCell, dim>& cell, I02 index) override {
84 : // average quantities
85 0 : const double mass = cell.getMacroscopicMass() / ((double)_numberMDsteps);
86 0 : const tarch::la::Vector<dim, double> momentum = cell.getMacroscopicMomentum() * (1.0 / ((double)_numberMDsteps));
87 0 : cell.setMacroscopicMass(mass);
88 0 : cell.setMacroscopicMomentum(momentum);
89 0 : }
90 :
91 : /** @brief compute current mass and momentum and add it to averaged buffer
92 : * value
93 : * @param cell coupling cell to process
94 : * @param index index of the coupling cell */
95 0 : void processInnerCouplingCellAfterMDTimestep(coupling::datastructures::CouplingCellWithLinkedCells<LinkedCell, dim>& cell, I02 index) override {
96 0 : cell.iterateConstCells(_massMapping);
97 0 : const double mass = _massMapping.getMass();
98 0 : cell.iterateConstCells(_momentumMapping);
99 0 : const tarch::la::Vector<dim, double> momentum = _momentumMapping.getMomentum();
100 0 : cell.addMacroscopicMass(mass);
101 0 : cell.addMacroscopicMomentum(momentum);
102 0 : }
103 :
104 : private:
105 : /** number of md time steps within one coupling time step */
106 : const unsigned int _numberMDsteps;
107 : /** vector containing zeros, the dimension is the spacial dimension of the
108 : * simulation */
109 : const tarch::la::Vector<dim, double> _zero;
110 : /** class to compute the mass within every single cell */
111 : coupling::cellmappings::ComputeMassMapping<LinkedCell, dim> _massMapping;
112 : /** class to compute the momentum within every single cell */
113 : coupling::cellmappings::ComputeMomentumMapping<LinkedCell, dim> _momentumMapping;
114 : };
115 : #endif // _MOLECULARDYNAMICS_COUPLING_TRANSFERSTRATEGIES_DIFFERENCETRANSFERSTRATEGY_H_
|