DropTestAnalytic.cpp 6.46 KB
Newer Older
Sebastian Eibl's avatar
Sebastian Eibl committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//======================================================================================================================
//
//  This file is part of waLBerla. waLBerla is free software: you can
//  redistribute it and/or modify it under the terms of the GNU General Public
//  License as published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
//  for more details.
//
//  You should have received a copy of the GNU General Public License along
//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
Sebastian Eibl's avatar
Sebastian Eibl committed
16
//! \file   DropTestAnalytic.cpp
Sebastian Eibl's avatar
Sebastian Eibl committed
17
18
19
20
//! \author Sebastian Eibl <sebastian.eibl@fau.de>
//
//======================================================================================================================

Sebastian Eibl's avatar
Sebastian Eibl committed
21
#include <mesa_pd/collision_detection/AnalyticContactDetection.h>
Sebastian Eibl's avatar
Sebastian Eibl committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85

#include <mesa_pd/data/ParticleAccessor.h>
#include <mesa_pd/data/ParticleStorage.h>
#include <mesa_pd/data/ShapeStorage.h>

#include <mesa_pd/kernel/DoubleCast.h>
#include <mesa_pd/kernel/ExplicitEulerWithShape.h>
#include <mesa_pd/kernel/ParticleSelector.h>
#include <mesa_pd/kernel/SpringDashpot.h>

#include <core/Abort.h>
#include <core/Environment.h>
#include <core/logging/Logging.h>
#include <core/waLBerlaBuildInfo.h>

#include <functional>
#include <memory>
#include <string>
#include <type_traits>

namespace walberla {
namespace mesa_pd {

class ParticleAccessorWithShape : public data::ParticleAccessor
{
public:
   ParticleAccessorWithShape(std::shared_ptr<data::ParticleStorage>& ps, std::shared_ptr<data::ShapeStorage>& ss)
         : ParticleAccessor(ps)
         , ss_(ss)
   {}

   const auto& getInvMass(const size_t p_idx) const {return ss_->shapes[ps_->getShapeID(p_idx)]->getInvMass();}

   const auto& getInvInertiaBF(const size_t p_idx) const {return ss_->shapes[ps_->getShapeID(p_idx)]->getInvInertiaBF();}

   data::BaseShape* getShape(const size_t p_idx) const {return ss_->shapes[ps_->getShapeID(p_idx)].get();}
private:
   std::shared_ptr<data::ShapeStorage> ss_;
};

int main( int argc, char ** argv )
{
   using namespace walberla::timing;

   Environment env(argc, argv);
   walberla::mpi::MPIManager::instance()->useWorldComm();

   if (std::is_same<walberla::real_t, float>::value)
   {
      WALBERLA_LOG_WARNING("waLBerla build in sp mode: skipping test due to low precision");
      return EXIT_SUCCESS;
   }

   //init data structures
   auto ps = std::make_shared<data::ParticleStorage>(100);
   auto ss = std::make_shared<data::ShapeStorage>();
   ParticleAccessorWithShape accessor(ps, ss);

   auto p                       = ps->create();
   p->getPositionRef()          = Vec3(real_t(0), real_t(0), real_t(0));
   p->getInteractionRadiusRef() = std::numeric_limits<real_t>::infinity();
   p->getShapeIDRef()           = ss->create<data::HalfSpace>( Vec3(real_t(0), real_t(0), real_t(1)) );
   p->getOwnerRef()             = walberla::mpi::MPIManager::instance()->rank();
   p->getTypeRef()              = 0;
Sebastian Eibl's avatar
Sebastian Eibl committed
86
87
88
89
   using namespace walberla::mesa_pd::data::particle_flags;
   set(p->getFlagsRef(), INFINITE);
   set(p->getFlagsRef(), FIXED);
   set(p->getFlagsRef(), GLOBAL);
Sebastian Eibl's avatar
Sebastian Eibl committed
90
91
92
93
94

   auto sp                       = ps->create();
   sp->getPositionRef()          = Vec3(real_t(0), real_t(0), real_t(0.01));
   sp->getInteractionRadiusRef() = real_t(1);
   sp->getShapeIDRef()           = ss->create<data::Sphere>( real_t(0.004) );
Sebastian Eibl's avatar
Sebastian Eibl committed
95
96
97
   ss->shapes[sp->getShapeID()]->updateMassAndInertia(real_t(2707));
   WALBERLA_LOG_DEVEL_VAR(ss->shapes[sp->getShapeID()]->getInvMass());
   WALBERLA_LOG_DEVEL_VAR(ss->shapes[sp->getShapeID()]->getInvInertiaBF());
Sebastian Eibl's avatar
Sebastian Eibl committed
98
99
100
   sp->getOwnerRef()             = walberla::mpi::MPIManager::instance()->rank();
   sp->getTypeRef()              = 0;

Sebastian Eibl's avatar
Sebastian Eibl committed
101
102
   int64_t simulationSteps = 200000;
   real_t dt = real_t(0.00001);
Sebastian Eibl's avatar
Sebastian Eibl committed
103
104
105
106

   // Init kernels
   kernel::ExplicitEulerWithShape        explicitEulerWithShape( dt );
   kernel::SpringDashpot                 dem(1);
Sebastian Eibl's avatar
Sebastian Eibl committed
107
108
   auto meff = real_t(1.0) / ss->shapes[sp->getShapeID()]->getInvMass();
   dem.setParametersFromCOR(0,0,real_t(0.9), dt * real_t(20), meff);
Sebastian Eibl's avatar
Sebastian Eibl committed
109
110
   dem.setDampingT (0, 0, real_t(6.86e1));
   dem.setFriction (0, 0, real_t(1.2));
Sebastian Eibl's avatar
Sebastian Eibl committed
111
112
113
114
   WALBERLA_LOG_DEVEL_VAR(dem.getStiffness(0,0));
   WALBERLA_LOG_DEVEL_VAR(dem.getDampingN(0,0));
   WALBERLA_LOG_DEVEL_VAR(dem.calcCollisionTime(0,0,meff));
   WALBERLA_LOG_DEVEL_VAR(dem.calcCoefficientOfRestitution(0,0,meff));
Sebastian Eibl's avatar
Sebastian Eibl committed
115

Sebastian Eibl's avatar
Sebastian Eibl committed
116
   collision_detection::AnalyticContactDetection gcd;
Sebastian Eibl's avatar
Sebastian Eibl committed
117
118
119
120
121
122
123
   kernel::DoubleCast                 double_cast;

   for (int64_t i=0; i < simulationSteps; ++i)
   {
      ps->forEachParticle(false,
                          kernel::SelectLocal(),
                          accessor,
Sebastian Eibl's avatar
Sebastian Eibl committed
124
125
126
127
128
129
                          [&](const size_t idx, auto& ac)
      {
         ac.setForce(idx,
                     Vec3(real_t(0), real_t(0), real_t(-9.81)) *
                     real_t(1.0) / ss->shapes[ac.getShapeID(idx)]->getInvMass() );
      }, accessor);
Sebastian Eibl's avatar
Sebastian Eibl committed
130

Sebastian Eibl's avatar
Sebastian Eibl committed
131
      ps->forEachParticlePairHalf(false,
Sebastian Eibl's avatar
Sebastian Eibl committed
132
133
134
135
136
137
                                  kernel::SelectAll(),
                                  accessor,
                                  [&](const size_t idx1, const size_t idx2, auto& ac)
      {
         if (double_cast(idx1, idx2, ac, gcd, ac ))
         {
Sebastian Eibl's avatar
Sebastian Eibl committed
138
139
140
141
142
143
144
145
146
            if (ss->shapes[ac.getShapeID(idx1)]->getShapeType() == data::HalfSpace::SHAPE_TYPE)
            {
               meff = real_t(1.0) / ss->shapes[ac.getShapeID(idx2)]->getInvMass();
            } else
            {
               meff = real_t(1.0) / ss->shapes[ac.getShapeID(idx1)]->getInvMass();
            }

            dem.setParametersFromCOR(0,0,real_t(0.9), dt * real_t(20), meff);
Sebastian Eibl's avatar
Sebastian Eibl committed
147
148
149
150
151
            dem(gcd.getIdx1(), gcd.getIdx2(), ac, gcd.getContactPoint(), gcd.getContactNormal(), gcd.getPenetrationDepth());
         }
      },
      accessor );

Sebastian Eibl's avatar
Sebastian Eibl committed
152
      ps->forEachParticle(false,
Sebastian Eibl's avatar
Sebastian Eibl committed
153
154
155
156
157
158
                          kernel::SelectLocal(),
                          accessor,
                          explicitEulerWithShape,
                          accessor);

//      if(i%1 == 0)
Sebastian Eibl's avatar
Sebastian Eibl committed
159
//         WALBERLA_LOG_DEVEL_VAR(sp->getPosition()[2]);
Sebastian Eibl's avatar
Sebastian Eibl committed
160
161
   }

Sebastian Eibl's avatar
Sebastian Eibl committed
162
   WALBERLA_CHECK_FLOAT_EQUAL_EPSILON(sp->getPosition()[2], real_t(0.004),     real_t(1e-7));
Sebastian Eibl's avatar
Sebastian Eibl committed
163
164
165
166
167
168
169
170
171
172
173

   return EXIT_SUCCESS;
}

} // namespace mesa_pd
} // namespace walberla

int main( int argc, char* argv[] )
{
   return walberla::mesa_pd::main( argc, argv );
}