BaseCoupling.h
Go to the documentation of this file.
1 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://MercuryDPm.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10  // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #ifndef BASE_COUPLING_H
27 #define BASE_COUPLING_H
28 
29 #include "Math/Vector.h"
30 #include "Walls/TriangleWall.h"
32 #include "CG/Functions/Gauss.h"
33 #include "CG/Functions/Lucy.h"
34 #include "CG/Functions/Heaviside.h"
36 #include "Logger.h"
37 
46 template<class M, class O>
47 class BaseCoupling : public M, public O
48 {
49 //protected:
50 // // A reference to the Mercury problem
51 // Mercury3D& m = this;
52 // // A reference to the Oomph problem
53 // SolidProblem<typename OomphProblem::ELEMENT_TYPE>& o = this;
54 
55 public:
56 
57  BaseCoupling() = default;
58 
59  void setName(std::string name) {
60  M::setName(name);
61  O::setName(name);
62  }
63 
64  std::string getName() const {
65  return M::getName();
66  }
67 
68  void removeOldFiles() const {
69  M::removeOldFiles();
70  O::removeOldFiles();
71  }
72 
76  void writeEneTimeStep(std::ostream& os) const override
77  {
78  if (M::eneFile.getCounter() == 1 || M::eneFile.getFileType() == FileType::MULTIPLE_FILES ||
79  M::eneFile.getFileType() == FileType::MULTIPLE_FILES_PADDED)
80  {
81  writeEneHeader(os);
82  }
83 
84  const Mdouble m = M::particleHandler.getMass();
85  const Vec3D com = M::particleHandler.getMassTimesPosition();
86  //Ensure the numbers fit into a constant width column: for this we need the precision given by the operating system,
87  //plus a few extra characters for characters like a minus and scientific notation.
88  const static long int width = os.precision() + 6;
89  os << std::setw(width) << M::getTime()
90 // << " " << std::setw(width) << getCoupledMass()
91  << " " << std::setw(width) << M::particleHandler.getMomentum().getX()
92  << " " << std::setw(width) << M::particleHandler.getMomentum().getY()
93  << " " << std::setw(width) << M::particleHandler.getMomentum().getZ()
94  << " " << std::setw(width) << M::particleHandler.getAngularMomentum().getX()
95  << " " << std::setw(width) << M::particleHandler.getAngularMomentum().getY()
96  << " " << std::setw(width) << M::particleHandler.getAngularMomentum().getZ()
97  << " " << std::setw(width) << -Vec3D::dot(M::getGravity(), com)
98  << " " << std::setw(width) << M::particleHandler.getKineticEnergy()
99  << " " << std::setw(width) << M::particleHandler.getRotationalEnergy()
100  //<< " " << std::setw(width) << M::getElasticEnergyCoupled()
101  // we need to write x, y and z coordinates separately, otherwise the width of the columns is incorrect
102  << " " << std::setw(width)
103  << ( m == 0 ? constants::NaN : com.X / m ) //set to nan because 0/0 implementation in gcc and clang differs
104  << " " << std::setw(width) << ( m == 0 ? constants::NaN : com.Y / m )
105  << " " << std::setw(width) << ( m == 0 ? constants::NaN : com.Z / m )
106  << std::endl;
107  }
108 
112  void writeEneHeader(std::ostream& os) const override
113  {
114  //only write if we don't restart
115  if (M::getAppend()) {
116  return;
117  }
118 
119  long width = os.precision() + 6;
120  os << std::setw(width)
121  << "time " << std::setw(width)
122  << "mass " << std::setw(width)
123  << "momentum_X " << std::setw(width)
124  << "momentum_Y " << std::setw(width)
125  << "momentum_Z " << std::setw(width)
126  << "angMomentum_X " << std::setw(width)
127  << "angMomentum_Y " << std::setw(width)
128  << "angMomentum_Z " << std::setw(width)
129  << "gravitEnergy " << std::setw(width) //gravitational potential energy
130  << "traKineticEnergy " << std::setw(width) //translational kinetic energy
131  << "rotKineticEnergy " << std::setw(width) //rotational kE
132  << "elasticEnergy " << std::setw(width)
133  << "centerOfMassX " << std::setw(width)
134  << "centerOfMassY " << std::setw(width)
135  << "centerOfMassZ\n";
136  }
137 
141  void solveOomph()
142  {
143  O::actionsBeforeOomphTimeStep();
144  this->unsteady_newton_solve(this->time_pt()->dt());
145  }
146 
150  void solveMercury(unsigned long nt)
151  {
152  for (int n = 0; n < nt; ++n)
153  {
154  M::computeOneTimeStep();
155  }
156  }
157 
158  inline void setCGWidth(const double& width)
159  {
160  if (width == 0)
161  {
162  CGMapping_ = false;
163  }
164  else
165  {
166  CGMapping_ = true;
167  // note CG function needs to be nondimensionalized
168  // fixme CG width is not set with respect to particle radius. Should we do that?
169  cgFunction_.setWidth(width);
170  }
171  }
172 
176  inline double getCGWidth()
177  {
178  return cgFunction_.getWidth();
179  }
180 
181  inline bool useCGMapping()
182  { return CGMapping_; }
183 
185  { return cgFunction_; }
186 
187 private:
188  // flag to construct mapping with FEM basis functions (default) or DPM coarse graining
189  bool CGMapping_ = false;
190  // coarse-graining functions and coordinates
192 };
193 
194 #endif //BASE_COUPLING_H
const unsigned n
Definition: CG3DPackingUnitTest.cpp:32
std::string getName(int argc, char *argv[])
Definition: CombineParallelDataFiles.cpp:12
@ MULTIPLE_FILES
each time-step will be written into/read from separate files numbered consecutively: name_....
@ MULTIPLE_FILES_PADDED
each time-step will be written into/read from separate files numbered consecutively,...
Definition: BaseCoupling.h:48
bool CGMapping_
Definition: BaseCoupling.h:189
CGFunctions::LucyXYZ cgFunction_
Definition: BaseCoupling.h:191
std::string getName() const
Definition: BaseCoupling.h:64
bool useCGMapping()
Definition: BaseCoupling.h:181
void solveOomph()
Definition: BaseCoupling.h:141
void removeOldFiles() const
Definition: BaseCoupling.h:68
double getCGWidth()
Definition: BaseCoupling.h:176
BaseCoupling()=default
void writeEneHeader(std::ostream &os) const override
Definition: BaseCoupling.h:112
void setName(std::string name)
Definition: BaseCoupling.h:59
CGFunctions::LucyXYZ getCGFunction()
Definition: BaseCoupling.h:184
void setCGWidth(const double &width)
Definition: BaseCoupling.h:158
void writeEneTimeStep(std::ostream &os) const override
Definition: BaseCoupling.h:76
void solveMercury(unsigned long nt)
Definition: BaseCoupling.h:150
Mdouble getWidth() const
void setWidth(Mdouble width)
Set the cutoff radius.
Definition: Vector.h:51
Mdouble Y
Definition: Vector.h:66
Mdouble Z
Definition: Vector.h:66
Mdouble X
the vector components
Definition: Vector.h:66
static Mdouble dot(const Vec3D &a, const Vec3D &b)
Calculates the dot product of two Vec3D: .
Definition: Vector.cc:76
Mdouble getX() const
Definition: Vector.h:402
const Mdouble NaN
Definition: GeneralDefine.h:43
std::string name
Definition: MercuryProb.h:48