MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseSpecies.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, 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 
26 #include "BaseSpecies.h"
27 #include "SpeciesHandler.h"
28 #include "DPMBase.h"
30 #include<cmath>
33 
34 class BaseParticle;
35 
36 class BaseInteractable;
37 
39  : BaseObject(), handler_(nullptr), interactionDistance_(0)
40 {
41  logger(DEBUG, "BaseSpecies::BaseSpecies() finished");
42 }
43 
44 BaseSpecies::BaseSpecies(BaseNormalForce* normalForce, BaseFrictionForce* frictionForce, BaseAdhesiveForce* adhesiveForce)
45  : BaseObject(), normalForce_(normalForce), frictionForce_(frictionForce), adhesiveForce_(adhesiveForce),
46  handler_(nullptr), interactionDistance_(0)
47 {
51  logger(DEBUG, "BaseSpecies::BaseSpecies() finished");
52 }
53 
54 
59  : BaseObject(p), handler_(p.handler_), interactionDistance_(p.interactionDistance_)
60 {
61  logger(DEBUG, "BaseSpecies::BaseSpecies(const BaseSpecies &p) finished");
62 }
63 
65 {
69  //for now, I added a particleHandler.clear(); wallHandler.clear() before speciesHandler.clear() in DPMBase::read()
70 
71 // //if species gets removed, all particles/ walls of this type need to be removed as well
72 // if (getHandler()) {
73 // DPMBase* dpm = getHandler()->getDPMBase();
74 // for (const auto o : dpm->wallHandler) {
75 // if (o->getSpecies()==this) {
76 // dpm->wallHandler.removeObject(o->getIndex());
77 // }
78 // }
79 // for (const auto o : dpm->particleHandler) {
80 // if (o->getSpecies()==this) {
81 // dpm->particleHandler.removeObject(o->getIndex());
82 // }
83 // }
84 // }
85  logger(DEBUG, "BaseSpecies::~BaseSpecies() finished");
86 }
87 
92 {
93  handler_ = handler;
94 }
95 
100 {
101  return handler_;
102 }
103 
111 {
112  //the second algorithm seems to have a better accuracy, at least for the case average(2e5,2e5)
113  //return (a + b) != 0.0 ? (2. * (a * b) / (a + b)) : 0;
114  return (a==0||b==0) ? 0.0 : (2. / (1.0 / a + 1.0 / b));
115 }
116 
121 {
122  if (a == constants::inf || b == constants::inf)
123  return constants::inf;
124  else
125  return average(a, b);
126 }
127 
131 void BaseSpecies::write(std::ostream& os) const
132 {
133  //BaseObject::write(os);
134  if (normalForce_->getConstantRestitution()) os << " constantRestitution " << normalForce_->getConstantRestitution();
135 }
136 
140 void BaseSpecies::read(std::istream& is)
141 {
142  //BaseObject::read(is);
143  bool constantRestitution;
144  if (helpers::readOptionalVariable(is, "constantRestitution", constantRestitution)) {
145  normalForce_->setConstantRestitution(constantRestitution);
146  }
147 }
148 
156 void BaseSpecies::setInteractionDistance(Mdouble interactionDistance) {
157  interactionDistance_ = interactionDistance;
158 
159  SpeciesHandler* handler = getHandler();
160  if (handler == nullptr) return;
161 
162  for (auto mixedSpecies : handler->getMixedObjects()) {
163  if (mixedSpecies == this) {
164  // get the two particlespecies id's
165  unsigned mixedId = mixedSpecies->getIndex();
166  unsigned maxId= 1;
167  unsigned maxMixedId = (maxId * (maxId + 1)) / 2;
168  while (maxMixedId<mixedId) {
169  ++maxId;
170  maxMixedId = (maxId * (maxId + 1)) / 2;
171  }
172  unsigned minId = (mixedId + maxId) - maxMixedId;
173  handler->getObject(minId)->setMaxInteractionDistance(interactionDistance);
174  handler->getObject(maxId)->setMaxInteractionDistance(interactionDistance);
175  //logger(INFO,"setInteractionDistance(%) mixed % handler %",interactionDistance, getIndex(), getHandler());
176  return;
177  }
178  }
179  handler->getObject(getId())->setMaxInteractionDistance(interactionDistance);
180 
181  //logger(INFO,"setInteractionDistance(%) species % handler %",interactionDistance, getIndex(), getHandler());
182 }
Container to store all ParticleSpecies.
bool getConstantRestitution() const
void setBaseSpecies(BaseSpecies *baseSpecies)
Definition: BaseForce.h:40
unsigned int getId() const
Returns the unique identifier of any particular object.
Definition: BaseObject.h:125
Mdouble interactionDistance_
Returns the distance between particles of this species below which adhesive forces can occur (needed ...
Definition: BaseSpecies.h:201
void setMaxInteractionDistance(Mdouble interactionDistance=0)
Sets maxInteractionDistance_.
BaseSpecies is the class from which all other species are derived.
Definition: BaseSpecies.h:49
BaseNormalForce * normalForce_
A pointer to the normal force parameters This pointer is used by the Interaction's to get a pointer ...
Definition: BaseSpecies.h:173
~BaseSpecies()
The default destructor.
Definition: BaseSpecies.cc:64
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
double Mdouble
Definition: GeneralDefine.h:34
It is an abstract base class due to the purely virtual functions declared below. Even if the function...
Definition: BaseObject.h:50
const std::vector< BaseSpecies * > & getMixedObjects() const
Returns a pointer to the vector of all mixed objects.
void setInteractionDistance(Mdouble interactionDistance)
Sets BaseSpecies::interactionDistance_.
Definition: BaseSpecies.cc:156
BaseAdhesiveForce * adhesiveForce_
A pointer to the adhesive force parameters This pointer is used by the Interaction's to get a pointe...
Definition: BaseSpecies.h:187
void setConstantRestitution(bool constantRestitution)
const Mdouble inf
Definition: GeneralDefine.h:44
SpeciesHandler * getHandler() const
Returns the pointer to the handler to which this species belongs.
Definition: BaseSpecies.cc:99
void write(std::ostream &os) const override
Sets the boolean constantRestitution_.
Definition: BaseSpecies.cc:131
SpeciesHandler * handler_
A pointer to the handler to which this species belongs. It is initialized to nullptr and gets set whe...
Definition: BaseSpecies.h:195
static Mdouble averageInf(Mdouble a, Mdouble b)
Returns the harmonic mean of two variables, returning inf if either is inf.
Definition: BaseSpecies.cc:120
static Mdouble average(Mdouble a, Mdouble b)
Returns the harmonic mean of two variables.
Definition: BaseSpecies.cc:110
T * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:613
bool readOptionalVariable(std::istream &is, const std::string &name, T &variable)
Reads optional variables in the restart file.
Definition: Helpers.h:247
void read(std::istream &is) override
Definition: BaseSpecies.cc:140
void setHandler(SpeciesHandler *handler)
Sets the pointer to the handler to which this species belongs.
Definition: BaseSpecies.cc:91
BaseSpecies()
The default constructor.
Definition: BaseSpecies.cc:38
BaseFrictionForce * frictionForce_
A pointer to the friction force parameters This pointer is used by the Interaction's to get a pointe...
Definition: BaseSpecies.h:180
Defines the basic properties that a interactable object can have.