Utility.h 8.75 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//======================================================================================================================
//
//  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/>.
//
//! \file   Utility.h
//! \author Christoph Rettinger <christoph.rettinger@fau.de>
//
//======================================================================================================================

#pragma once

#include "mesa_pd/data/ParticleStorage.h"

#include <iterator>
#include <algorithm>
#include <functional>

namespace walberla {
namespace mesa_pd {

class ParticleAccessorWithShape : public data::ParticleAccessor
{
public:
   ParticleAccessorWithShape(std::shared_ptr<data::ParticleStorage>& particleStorage)
         : ParticleAccessor(particleStorage)
   {}

   walberla::real_t const & getInvMass(const size_t p_idx) const {return ps_->getBaseShapeRef(p_idx)->getInvMass();}

   Mat3 const & getInvInertiaBF(const size_t p_idx) const {return ps_->getBaseShapeRef(p_idx)->getInvInertiaBF();}
   Mat3 const & getInertiaBF(const size_t p_idx) const {return ps_->getBaseShapeRef(p_idx)->getInertiaBF();}
   data::BaseShape* getShape(const size_t p_idx) const {return ps_->getBaseShape(p_idx).get();}
};



template< typename T>
std::vector<T> parseStringToVector(std::string inputStr)
{
   std::istringstream iss(inputStr);
   return std::vector<T>{std::istream_iterator<T>(iss),std::istream_iterator<T>()};
}

real_t radiusFromSphereVolume(real_t volume)
{
   return std::cbrt(real_t(3) / ( real_t(4) * math::pi) * volume);
}

real_t diameterFromSphereVolume(real_t volume)
{
   return real_t(2) * radiusFromSphereVolume(volume);
}

uint_t getIndexOfSecondSemiAxis(Vector3<real_t> semiAxes)
{
   std::set<uint_t> indices = {0,1,2};
   indices.erase(semiAxes.indexOfMin());
   indices.erase(semiAxes.indexOfMax());
   return *indices.begin();
}

void sortVector(Vector3<real_t> & v)
{
   std::sort(v.data(),v.data()+3);
}

real_t sizeFromSemiAxes(Vec3 semiAxes)
{
   sortVector(semiAxes);
   return 2_r * std::sqrt((semiAxes[0] * semiAxes[0] + semiAxes[1] * semiAxes[1]) / 2_r); // factor of 2 because those are SEMI axis
}

// assuming a mass-equivalent ellipsoid
Vec3 semiAxesFromInertiaTensor(const Matrix3<real_t> & inertiaTensor, real_t mass)
{
   Vec3 semiAxes;
   semiAxes[0] = std::sqrt( (- inertiaTensor(0,0) + inertiaTensor(1,1) + inertiaTensor(2,2)) / (2_r * mass / 5_r) );
   semiAxes[1] = std::sqrt( (- inertiaTensor(1,1) + inertiaTensor(2,2) + inertiaTensor(0,0)) / (2_r * mass / 5_r) );
   semiAxes[2] = std::sqrt( (- inertiaTensor(2,2) + inertiaTensor(0,0) + inertiaTensor(1,1)) / (2_r * mass / 5_r) );
   return semiAxes;
}


std::vector<real_t> getMeanDiametersFromSieveSizes(std::vector<real_t> sieveSizes)
{
   //since grain sizes are logarithmically distributed, it is practice to use the geometric mean
   std::vector<real_t> meanDiameters(sieveSizes.size()-1,0_r);
   for(uint_t i = 0; i < meanDiameters.size(); ++i)
   {
      meanDiameters[i] = std::sqrt(sieveSizes[i] * sieveSizes[i+1]);
   }
   return meanDiameters;
}

// if totalMass and density are given actual numbers, the resulting particle numbers are a good estimate for the actual numbers
// else, the resulting numbers are directly proportional to the actual ones, which is sufficient to define the distributions
std::vector<real_t> transferMassFractionsToParticleNumbersFromAvgVolumes(std::vector<real_t> massFractions, std::vector<real_t> avgVolumePerSizeFraction, real_t totalMass = real_t(1), real_t density = real_t(1) )
{
   WALBERLA_CHECK_EQUAL(avgVolumePerSizeFraction.size(), massFractions.size(), "Number of entries in volume and mass-fraction array has to be the same!");
   std::vector<real_t> particleNumbers(massFractions.size(), real_t(0));
   for(uint_t n = 0; n < massFractions.size(); ++n )
   {
      if(avgVolumePerSizeFraction[n] > 0_r) particleNumbers[n] = totalMass * massFractions[n] / (density * avgVolumePerSizeFraction[n]);
   }
   return particleNumbers;
}

// note: normalVolume is the volume of a typical particle with diameter = 1. For sphere: PI / 6
std::vector<real_t> transferMassFractionsToParticleNumbers(std::vector<real_t> massFractions, std::vector<real_t> diameters, real_t normalVolume = math::pi / real_t(6), real_t totalMass = real_t(1), real_t density = real_t(1) )
{
   WALBERLA_CHECK_EQUAL(diameters.size(), massFractions.size(), "Number of entries in diameter and mass-fraction array has to be the same!");
   std::vector<real_t> avgVolumePerSizeFraction(massFractions.size(), real_t(0));
   for(uint_t n = 0; n < massFractions.size(); ++n )
   {
      avgVolumePerSizeFraction[n] = normalVolume * diameters[n] * diameters[n] * diameters[n];
   }
   return transferMassFractionsToParticleNumbersFromAvgVolumes(massFractions, avgVolumePerSizeFraction, totalMass, density);
}

real_t computePercentileFromSieveDistribution(std::vector<real_t> diameters, std::vector<real_t> massFractions, real_t percentile)
{
   WALBERLA_CHECK(percentile > 0_r && percentile < 100_r, "Percentile is a value between 0 and 100");
   if(diameters[0] > diameters[1])
   {
      // reverse order to have it ascending
      std::reverse(diameters.begin(), diameters.end());
      std::reverse(massFractions.begin(), massFractions.end());
   }

   std::vector<real_t> cdf(massFractions.size(),0_r);
   std::partial_sum(massFractions.begin(), massFractions.end(), cdf.begin());

   for(uint_t i = 0; i < cdf.size()-1; ++i)
   {
      if(cdf[i] <= percentile/100_r && percentile/100_r <= cdf[i+1] )
      {
         real_t f_small = cdf[i];
         real_t f_large = cdf[i+1];
         if(f_small <= 0.000001_r && f_large>= 0.999999_r)
         {
            // special case of uniform distribution -> percentile is this value
            return diameters[i+1];
         }
         real_t phi_small = - std::log2(diameters[i]);
         real_t phi_large = - std::log2(diameters[i+1]);
         // logarithmic interpolation of diameter value
         real_t phi_percentile = phi_small + (phi_large - phi_small) / (f_large - f_small) * (percentile / 100_r - f_small);
         return std::pow(2_r,-phi_percentile);
      }
   }
   return diameters[0];
}


auto createPlane( std::shared_ptr<data::ParticleStorage> particleStorage,
                  const Vec3& pos,
                  const Vec3& normal)
{
   auto p = particleStorage->create(true);
   p->setPosition( pos );
   p->setBaseShape( std::make_shared<data::HalfSpace>( normal ) );
   p->getBaseShapeRef()->updateMassAndInertia(real_t(1));
   p->setOwner( walberla::mpi::MPIManager::instance()->rank() );
   p->setType( 0 );
   p->setInteractionRadius(std::numeric_limits<real_t>::infinity());
   data::particle_flags::set(p->getFlagsRef(), data::particle_flags::GLOBAL);
   data::particle_flags::set(p->getFlagsRef(), data::particle_flags::INFINITE);
   data::particle_flags::set(p->getFlagsRef(), data::particle_flags::FIXED);
   data::particle_flags::set(p->getFlagsRef(), data::particle_flags::NON_COMMUNICATING);
   return p;
}

auto createCylindricalBoundary( std::shared_ptr<data::ParticleStorage> particleStorage,
                                const Vec3& pos,
                                const Vec3& axis, real_t radius)
{
   auto p = particleStorage->create(true);
   p->setPosition( pos );
   p->setBaseShape( std::make_shared<data::CylindricalBoundary>( radius, axis ) );
   p->getBaseShapeRef()->updateMassAndInertia(real_t(1));
   p->setOwner( walberla::mpi::MPIManager::instance()->rank() );
   p->setType( 0 );
   p->setInteractionRadius(std::numeric_limits<real_t>::infinity());
   data::particle_flags::set(p->getFlagsRef(), data::particle_flags::GLOBAL);
   data::particle_flags::set(p->getFlagsRef(), data::particle_flags::INFINITE);
   data::particle_flags::set(p->getFlagsRef(), data::particle_flags::FIXED);
   data::particle_flags::set(p->getFlagsRef(), data::particle_flags::NON_COMMUNICATING);
   return p;
}


void writeParticleInformationToFile(const std::string& filename, const std::string& particleInfoStr, bool logToProcessLocalFiles)
{

   if(logToProcessLocalFiles)
   {
      std::ofstream file;
      file.open( filename.c_str());
      file << particleInfoStr;
      file.close();
   }else{
      walberla::mpi::writeMPITextFile( filename, particleInfoStr );
   }

}



} // namespace mesa_pd
} // namespace walberla