ContactLawTestHelpers.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://www.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 
30 #ifndef CONTACT_LAW_TEST_HELPERS
31 #define CONTACT_LAW_TEST_HELPERS
32 
33 #include "DPMBase.h"
34 #include "Helpers/FileIOHelpers.h"
35 #include "Logger.h"
36 #include "Particles/BaseParticle.h"
37 #include "Species/BaseSpecies.h"
38 #include "Walls/InfiniteWall.h"
39 
46 void loadingTest(const ParticleSpecies* species, Mdouble displacement, Mdouble velocity, Mdouble radius,
47  std::string name)
48 {
49  class LoadingTest : public DPMBase
50  {
51  const ParticleSpecies* species;
52  Mdouble displacement;
53  Mdouble velocity;
54  Mdouble radius;
55  public:
56  //public variables
57  LoadingTest(const ParticleSpecies* species, Mdouble displacement, Mdouble velocity, Mdouble radius)
58  : species(species), displacement(displacement), velocity(velocity), radius(radius)
59  {}
60 
61  void setupInitialConditions() override
62  {
63  //setName("LoadingTest"+species->getName());
64  setTimeMax(2.0 * displacement / velocity);
65  setTimeStep(2e-3 * getTimeMax());
66  setSaveCount(1);
68  fStatFile.setFileType(FileType::ONE_FILE);
69 
70  setMax({radius, radius, radius + radius});
71  setMin({-radius, -radius, 0});
74 
75  speciesHandler.copyAndAddObject(*species);
76 
78  p.setSpecies(speciesHandler.getObject(0));
79  p.setRadius(radius);
80  p.setPosition({0, 0, radius});
81  particleHandler.copyAndAddObject(p);
82 
83  InfiniteWall w;
84  w.setSpecies(speciesHandler.getObject(0));
85  w.set(Vec3D(0, 0, -1), Vec3D(0.0, 0.0, 0.0));
86  wallHandler.copyAndAddObject(w);
87  }
88 
89  void actionsBeforeTimeStep() override
90  {
91  BaseParticle* p = particleHandler.getLastObject();
92  logger.assert_debug(p,"Empty particle handler");
93  p->setAngularVelocity({0, 0, 0});
94 
95  //Moving particle normally into surface
96  if (getTime() <= displacement / velocity)
97  {
98  p->setVelocity({0, 0, velocity});
99  p->setPosition({0, 0, radius - velocity * getTime()});
100  }
101  else
102  {
103  p->setVelocity({0, 0, -velocity});
104  p->setPosition({0, 0, radius - displacement - displacement + velocity * getTime()});
105  }
106  }
107  } test(species, displacement, velocity, radius);
108  test.setName(name);
109  test.solve();
110  helpers::writeToFile(test.getName() + ".gnu", "plot '" + test.getName() + ".fstat' u 7:9 w lp");
111  logger(INFO, "finished loading test: run 'gnuplot %.gnu' to view output", test.getName());
112 }
113 
120 void normalAndTangentialLoadingTest(const ParticleSpecies* species, Mdouble displacement,
121  Mdouble tangentialDisplacement, Mdouble velocity, Mdouble radius,
122  std::string name)
123 {
124  class LoadingTest : public DPMBase
125  {
126  const ParticleSpecies* species;
127  Mdouble displacement;
128  Mdouble tangentialDisplacement;
129  Mdouble velocity;
130  Mdouble radius;
131  public:
132  //public variables
133  LoadingTest(const ParticleSpecies* species, Mdouble displacement, Mdouble tangentialDisplacement,
134  Mdouble velocity, Mdouble radius)
135  : species(species), displacement(displacement), tangentialDisplacement(tangentialDisplacement),
136  velocity(velocity), radius(radius)
137  {}
138 
139  void setupInitialConditions() override
140  {
141  //setName("TangentialLoadingTest"+species->getName());
142  setTimeMax(4.0 * tangentialDisplacement / velocity);
143  setTimeStep(4e-4 * getTimeMax());
144  setSaveCount(1);
146  fStatFile.setFileType(FileType::ONE_FILE);
147 
148  setMax({radius, radius, radius + radius});
149  setMin({-radius, -radius, 0});
152 
153  speciesHandler.copyAndAddObject(*species);
154 
156  p.setSpecies(speciesHandler.getObject(0));
157  p.setRadius(radius);
158  p.setPosition({0, 0, radius - displacement});
159  particleHandler.copyAndAddObject(p);
160 
161  InfiniteWall w;
162  w.setSpecies(speciesHandler.getObject(0));
163  w.set(Vec3D(0, 0, -1), Vec3D(0.0, 0.0, 0.0));
164  wallHandler.copyAndAddObject(w);
165  }
166 
167  void actionsBeforeTimeStep() override
168  {
169  BaseParticle* p = particleHandler.getLastObject();
170  logger.assert_debug(p,"Empty particle handler");
171  p->setAngularVelocity({0, 0, 0});
172 
173  //Moving particle cyclically right and left between +-tangentialDisplacement
174  bool moveRight = static_cast<int>(getTime() / (2.0*tangentialDisplacement / velocity) +0.5)%2==0;
175  if (moveRight)
176  {
177  p->setVelocity({-velocity, 0, 0});
178  p->setPosition({tangentialDisplacement - velocity * getTime(), 0, radius - displacement});
179  }
180  else
181  {
182  p->setVelocity({velocity, 0, 0});
183  p->setPosition({-2*tangentialDisplacement + velocity * getTime(), 0, radius - displacement});
184  }
185  }
186 
187  } test(species, displacement, tangentialDisplacement, velocity, radius);
188  test.setName(name);
189  test.solve();
190  helpers::writeToFile(test.getName() + ".gnu", "plot '" + test.getName() + ".fstat' u 8:($10*$14) w lp");
191  logger(INFO, "finished tangential loading test: run 'gnuplot %.gnu' to view output", test.getName());
192 }
193 
200 void objectivenessTest(const ParticleSpecies* species, Mdouble displacement, Mdouble tangentialDisplacement,
201  Mdouble velocity, Mdouble radius, std::string name)
202 {
203  class ObjectivenessTest : public DPMBase
204  {
205  const ParticleSpecies* species;
206  Mdouble displacement;
207  Mdouble tangentialDisplacement;
208  Mdouble velocity;
209  Mdouble radius;
210  public:
211  //public variables
212  ObjectivenessTest(const ParticleSpecies* species, Mdouble displacement, Mdouble tangentialDisplacement,
213  Mdouble velocity, Mdouble radius)
214  : species(species), displacement(displacement), tangentialDisplacement(tangentialDisplacement),
215  velocity(velocity), radius(radius)
216  {}
217 
218  void setupInitialConditions() override
219  {
220  //setName("ObjectivenessTest"+species->getName());
221  setTimeMax((tangentialDisplacement + 0.5 * constants::pi * radius) / velocity);
222  setTimeStep(1e-4 * getTimeMax());
223  setSaveCount(20);
225  dataFile.setFileType(FileType::ONE_FILE);
226  fStatFile.setFileType(FileType::ONE_FILE);
227 
228  setMax(radius * Vec3D(2, 2, 2));
229  setMin(radius * Vec3D(-2, -2, -2));
232 
233  speciesHandler.copyAndAddObject(*species);
234 
236  p.setSpecies(speciesHandler.getObject(0));
237  p.setRadius(radius);
238  p.setPosition({0, radius - displacement, 0});
239  particleHandler.copyAndAddObject(p);
240  p.setPosition({0, -radius + displacement, 0});
241  particleHandler.copyAndAddObject(p);
242  }
243 
244  void actionsBeforeTimeStep() override
245  {
246  BaseParticle* p = particleHandler.getObject(0);
247  BaseParticle* q = particleHandler.getLastObject();
248  logger.assert_debug(p,"Empty particle handler");
249  logger.assert_debug(q,"Empty particle handler");
250 
251  //Moving particle normally into surface
252  if (getTime() <= tangentialDisplacement / velocity)
253  {
254  p->setAngularVelocity({0, 0, 0});
255  p->setVelocity({velocity, 0, 0});
256  p->setPosition({-tangentialDisplacement + velocity * getTime(), radius - displacement, 0});
257  q->setAngularVelocity({0, 0, 0});
258  q->setVelocity({-velocity, 0, 0});
259  q->setPosition({tangentialDisplacement - velocity * getTime(), -radius + displacement, 0});
260  }
261  else
262  {
263  Mdouble angle = velocity / (radius - displacement) * (getTime() - tangentialDisplacement / velocity);
264  Mdouble s = sin(angle);
265  Mdouble c = cos(angle);
266  p->setAngularVelocity(velocity / (radius - displacement) * Vec3D(0, 0, -1));
267  //p->setAngularVelocity(Vec3D(0,0,0));
268  p->setOrientation({1, 0, 0, 0});
269  p->setVelocity(velocity * Vec3D(c, -s, 0));
270  //p->setVelocity(Vec3D(0,0,0));
271  p->setPosition((radius - displacement) * Vec3D(s, c, 0));
273  q->setOrientation(-p->getOrientation());
274  q->setVelocity(-p->getVelocity());
275  q->setPosition(-p->getPosition());
276  }
277  }
278 
279  } test(species, displacement, tangentialDisplacement, velocity, radius);
280  test.setName(name);
281  test.solve();
282  helpers::writeToFile(test.getName() + ".gnu", "set size ratio -1; plot '" + test.getName() + ".fstat' u 14:15 every 2 w lp");
283  logger(INFO, "finished objectiveness test: run 'gnuplot %.gnu' to view output", test.getName());
284 }
285 
286 #endif // CONTACT_LAW_TEST_HELPERS
void loadingTest(const ParticleSpecies *species, Mdouble displacement, Mdouble velocity, Mdouble radius, std::string name)
Definition: ContactLawTestHelpers.h:46
void normalAndTangentialLoadingTest(const ParticleSpecies *species, Mdouble displacement, Mdouble tangentialDisplacement, Mdouble velocity, Mdouble radius, std::string name)
Definition: ContactLawTestHelpers.h:120
void objectivenessTest(const ParticleSpecies *species, Mdouble displacement, Mdouble tangentialDisplacement, Mdouble velocity, Mdouble radius, std::string name)
Definition: ContactLawTestHelpers.h:200
@ NO_FILE
file will not be created/read
@ ONE_FILE
all data will be written into/ read from a single file called name_
double Mdouble
Definition: GeneralDefine.h:34
LL< Log::INFO > INFO
Info log level.
Definition: Logger.cc:55
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
const Quaternion & getOrientation() const
Returns the orientation of this BaseInteractable.
Definition: BaseInteractable.h:230
virtual const Vec3D & getAngularVelocity() const
Returns the angular velocity of this interactable.
Definition: BaseInteractable.cc:341
virtual void setOrientation(const Quaternion &orientation)
Sets the orientation of this BaseInteractable.
Definition: BaseInteractable.h:260
void setAngularVelocity(const Vec3D &angularVelocity)
set the angular velocity of the BaseInteractble.
Definition: BaseInteractable.cc:360
virtual const Vec3D & getVelocity() const
Returns the velocity of this interactable.
Definition: BaseInteractable.cc:329
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
Definition: BaseInteractable.cc:350
virtual void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
Definition: BaseInteractable.h:239
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
Definition: BaseInteractable.h:218
Definition: BaseParticle.h:54
virtual void setRadius(Mdouble radius)
Sets the particle's radius_ (and adjusts the mass_ accordingly, based on the particle's species)
Definition: BaseParticle.cc:553
void setSpecies(const ParticleSpecies *species)
Definition: BaseParticle.cc:818
void setSpecies(const ParticleSpecies *species)
Defines the species of the current wall.
Definition: BaseWall.cc:169
The DPMBase header includes quite a few header files, defining all the handlers, which are essential....
Definition: DPMBase.h:77
void setSaveCount(unsigned int saveCount)
Sets File::saveCount_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:408
virtual void setupInitialConditions()
This function allows to set the initial conditions for our problem to be solved, by default particle ...
Definition: DPMBase.cc:1998
void setParticleDimensions(unsigned int particleDimensions)
Sets the particle dimensionality.
Definition: DPMBase.cc:1448
virtual void actionsBeforeTimeStep()
A virtual function which allows to define operations to be executed before the new time step.
Definition: DPMBase.cc:1864
Mdouble getTime() const
Returns the current simulation time.
Definition: DPMBase.cc:808
void setFileType(FileType fileType)
Sets File::fileType_ for all files (ene, data, fstat, restart, stat)
Definition: DPMBase.cc:459
void setMin(const Vec3D &min)
Sets the minimum coordinates of the problem domain.
Definition: DPMBase.cc:1118
void setTimeStep(Mdouble newDt)
Sets a new value for the simulation time step.
Definition: DPMBase.cc:1234
void setTimeMax(Mdouble newTMax)
Sets a new value for the maximum simulation duration.
Definition: DPMBase.cc:873
void setSystemDimensions(unsigned int newDim)
Sets the system dimensionality.
Definition: DPMBase.cc:1417
void setMax(const Vec3D &max)
Sets the maximum coordinates of the problem domain.
Definition: DPMBase.cc:1082
Mdouble getTimeMax() const
Returns the maximum simulation duration.
Definition: DPMBase.cc:888
A infinite wall fills the half-space {point: (position_-point)*normal_<=0}.
Definition: InfiniteWall.h:48
void set(Vec3D normal, Vec3D point)
Defines a standard wall, given an outward normal vector s.t. normal*x=normal*point for all x of the w...
Definition: InfiniteWall.cc:118
Definition: ParticleSpecies.h:37
A spherical particle is the most simple particle used in MercuryDPM.
Definition: SphericalParticle.h:37
Definition: Vector.h:51
const Mdouble pi
Definition: ExtendedMath.h:45
bool writeToFile(std::string filename, std::string filecontent)
Writes a string to a file.
Definition: FileIOHelpers.cc:58
Mdouble cos(Mdouble x)
Definition: ExtendedMath.cc:64
Mdouble sin(Mdouble x)
Definition: ExtendedMath.cc:44
std::string name
Definition: MercuryProb.h:48