SPHinXsys  alpha version
diffusion_reaction.h
1 /* -------------------------------------------------------------------------*
2  * SPHinXsys *
3  * --------------------------------------------------------------------------*
4  * SPHinXsys (pronunciation: s'finksis) is an acronym from Smoothed Particle *
5  * Hydrodynamics for industrial compleX systems. It provides C++ APIs for *
6  * physical accurate simulation and aims to model coupled industrial dynamic *
7  * systems including fluid, solid, multi-body dynamics and beyond with SPH *
8  * (smoothed particle hydrodynamics), a meshless computational method using *
9  * particle discretization. *
10  * *
11  * SPHinXsys is partially funded by German Research Foundation *
12  * (Deutsche Forschungsgemeinschaft) DFG HU1527/6-1, HU1527/10-1 *
13  * and HU1527/12-1. *
14  * *
15  * Portions copyright (c) 2017-2020 Technical University of Munich and *
16  * the authors' affiliations. *
17  * *
18  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
19  * not use this file except in compliance with the License. You may obtain a *
20  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
21  * *
22  * --------------------------------------------------------------------------*/
32 #ifndef DIFFUSION_REACTION_H
33 #define DIFFUSION_REACTION_H
34 
35 #include "base_material.h"
36 
37 #include <map>
38 #include <functional>
39 using namespace std::placeholders;
40 
41 namespace SPH
42 {
47  class BaseDiffusion : public BaseMaterial
48  {
49  public:
50  BaseDiffusion(size_t diffusion_species_index, size_t gradient_species_index)
51  : BaseMaterial(), diffusion_species_index_(diffusion_species_index),
52  gradient_species_index_(gradient_species_index)
53  {
54  material_type_name_ = "BaseDiffusion";
55  };
56  virtual ~BaseDiffusion(){};
57 
58  size_t diffusion_species_index_;
59  size_t gradient_species_index_;
60 
61  virtual Real getReferenceDiffusivity() = 0;
62  virtual Real getInterParticleDiffusionCoff(size_t particle_i, size_t particle_j, Vecd &direction_from_j_to_i) = 0;
63  };
64 
70  {
71  protected:
72  Real diff_cf_;
74  public:
75  IsotropicDiffusion(size_t diffusion_species_index, size_t gradient_species_index,
76  Real diff_cf = 1.0)
77  : BaseDiffusion(diffusion_species_index, gradient_species_index),
78  diff_cf_(diff_cf)
79  {
80  material_type_name_ = "IsotropicDiffusion";
81  };
82  virtual ~IsotropicDiffusion(){};
83 
84  virtual Real getReferenceDiffusivity() override { return diff_cf_; };
85  virtual Real getInterParticleDiffusionCoff(size_t particle_i, size_t particle_j, Vecd &direction_from_j_to_i) override
86  {
87  return diff_cf_;
88  };
89  };
90 
96  {
97  protected:
102  void initializeDirectionalDiffusivity(Real diff_cf, Real bias_diff_cf, Vecd bias_direction);
103 
104  public:
105  DirectionalDiffusion(size_t diffusion_species_index, size_t gradient_species_index,
106  Real diff_cf, Real bias_diff_cf, Vecd bias_direction)
107  : IsotropicDiffusion(diffusion_species_index, gradient_species_index, diff_cf),
108  bias_direction_(bias_direction), bias_diff_cf_(bias_diff_cf),
109  transformed_diffusivity_(1.0)
110  {
111  material_type_name_ = "DirectionalDiffusion";
112  initializeDirectionalDiffusivity(diff_cf, bias_diff_cf, bias_direction);
113  };
114  virtual ~DirectionalDiffusion(){};
115 
116  virtual Real getReferenceDiffusivity() override
117  {
118  return SMAX(diff_cf_, diff_cf_ + bias_diff_cf_);
119  };
120 
121  virtual Real getInterParticleDiffusionCoff(size_t particle_index_i,
122  size_t particle_index_j, Vecd &inter_particle_direction) override
123  {
124  Vecd grad_ij = transformed_diffusivity_ * inter_particle_direction;
125  return 1.0 / grad_ij.scalarNormSqr();
126  };
127  };
128 
134  {
135  protected:
136  StdLargeVec<Vecd> local_bias_direction_;
137  StdLargeVec<Matd> local_transformed_diffusivity_;
138 
139  void initializeFiberDirection();
140 
141  public:
142  LocalDirectionalDiffusion(size_t diffusion_species_index, size_t gradient_species_index,
143  Real diff_cf, Real bias_diff_cf, Vecd bias_direction)
144  : DirectionalDiffusion(diffusion_species_index, gradient_species_index, diff_cf, bias_diff_cf, bias_direction)
145  {
146  material_type_name_ = "LocalDirectionalDiffusion";
147  };
148  virtual ~LocalDirectionalDiffusion(){};
149  virtual Real getInterParticleDiffusionCoff(size_t particle_index_i, size_t particle_index_j, Vecd &inter_particle_direction) override
150  {
151  Matd trans_diffusivity = getAverageValue(local_transformed_diffusivity_[particle_index_i], local_transformed_diffusivity_[particle_index_j]);
152  Vecd grad_ij = trans_diffusivity * inter_particle_direction;
153  return 1.0 / grad_ij.scalarNormSqr();
154  };
155  virtual void assignBaseParticles(BaseParticles *base_particles) override;
156  virtual void readFromXmlForLocalParameters(const std::string &filefullpath) override;
157  };
158 
164  {
165  protected:
167  typedef std::function<Real(StdVec<StdLargeVec<Real>> &, size_t particle_i)> ReactionFunctor;
168  StdVec<std::string> species_name_list_;
169  std::map<std::string, size_t> species_indexes_map_;
170  std::string reaction_model_;
171 
172  public:
173  explicit BaseReactionModel(StdVec<std::string> species_name_list)
174  : reaction_model_("BaseReactionModel"), species_name_list_(species_name_list)
175  {
176  for (size_t i = 0; i != species_name_list.size(); ++i)
177  {
178  species_indexes_map_.insert(make_pair(species_name_list[i], i));
179  }
180  };
181  virtual ~BaseReactionModel(){};
182 
183  IndexVector reactive_species_;
184  StdVec<ReactionFunctor> get_production_rates_;
185  StdVec<ReactionFunctor> get_loss_rates_;
186 
187  StdVec<std::string> getSpeciesNameList() { return species_name_list_; };
188  };
189 
194  template <class BaseMaterialType = BaseMaterial>
196  {
197  private:
198  UniquePtrKeepers<BaseDiffusion> diffusion_ptr_keeper_;
199 
200  protected:
201  StdVec<std::string> species_name_list_;
202  size_t number_of_species_;
203  std::map<std::string, size_t> species_indexes_map_;
204  StdVec<BaseDiffusion *> species_diffusion_;
205  BaseReactionModel *species_reaction_;
206 
207  public:
209  template <typename... ConstructorArgs>
210  DiffusionReaction(StdVec<std::string> species_name_list, ConstructorArgs &&...args)
211  : BaseMaterialType(std::forward<ConstructorArgs>(args)...),
212  species_name_list_(species_name_list),
213  number_of_species_(species_name_list.size()),
214  species_reaction_(nullptr)
215  {
216  BaseMaterialType::material_type_name_ = "Diffusion";
217  for (size_t i = 0; i != number_of_species_; ++i)
218  {
219  species_indexes_map_.insert(make_pair(species_name_list[i], i));
220  }
221  };
223  template <typename... ConstructorArgs>
225  StdVec<std::string> species_name_list, ConstructorArgs &&...args)
226  : DiffusionReaction(species_name_list, std::forward<ConstructorArgs>(args)...)
227  {
228  species_reaction_ = &species_reaction;
229  BaseMaterialType::material_type_name_ = "DiffusionReaction";
230  };
231  virtual ~DiffusionReaction(){};
232 
233  size_t NumberOfSpecies() { return number_of_species_; };
234  size_t NumberOfSpeciesDiffusion() { return species_diffusion_.size(); };
235  StdVec<BaseDiffusion *> SpeciesDiffusion() { return species_diffusion_; };
236  BaseReactionModel *SpeciesReaction() { return species_reaction_; };
237  std::map<std::string, size_t> SpeciesIndexMap() { return species_indexes_map_; };
238  StdVec<std::string> getSpeciesNameList() { return species_name_list_; };
239  void assignBaseParticles(BaseParticles *base_particles) override
240  {
241  BaseMaterialType::assignBaseParticles(base_particles);
242  for (size_t k = 0; k < species_diffusion_.size(); ++k)
243  species_diffusion_[k]->assignBaseParticles(base_particles);
244  };
249  Real getDiffusionTimeStepSize(Real smoothing_length)
250  {
251  Real diff_coff_max = 0.0;
252  for (size_t k = 0; k < species_diffusion_.size(); ++k)
253  diff_coff_max = SMAX(diff_coff_max, species_diffusion_[k]->getReferenceDiffusivity());
254  Real dimension = Real(Vecd(0).size());
255  return 0.5 * smoothing_length * smoothing_length / diff_coff_max / dimension;
256  };
257 
259  template <class DiffusionType, typename... ConstructorArgs>
260  void initializeAnDiffusion(const std::string &diffusion_species_name,
261  const std::string &gradient_species_name, ConstructorArgs &&...args)
262  {
263  species_diffusion_.push_back(
264  diffusion_ptr_keeper_.createPtr<DiffusionType>(
265  species_indexes_map_[diffusion_species_name],
266  species_indexes_map_[diffusion_species_name], std::forward<ConstructorArgs>(args)...));
267  };
268 
269  virtual DiffusionReaction<BaseMaterialType> *ThisObjectPtr() override { return this; };
270  };
271 }
272 #endif // DIFFUSION_REACTION_H
diffusion property abstract base class.
Definition: diffusion_reaction.h:47
isotropic diffusion property.
Definition: diffusion_reaction.h:69
Particles with essential (geometric and kinematic) data. There are three types of particles, all par...
Definition: base_particles.h:81
Diffusion is biased along a specific direction.
Definition: diffusion_reaction.h:95
Real getDiffusionTimeStepSize(Real smoothing_length)
Get diffusion time step size. Here, I follow the reference: https://www.uni-muenster.de/imperia/md/content/physik_tp/lectures/ws2016-2017/num_methods_i/heat.pdf.
Definition: diffusion_reaction.h:249
Vecd bias_direction_
Definition: diffusion_reaction.h:98
Complex material for diffusion or/and reactions.
Definition: diffusion_reaction.h:195
Base of all materials.
Definition: base_material.h:52
DiffusionReaction(StdVec< std::string > species_name_list, ConstructorArgs &&...args)
Definition: diffusion_reaction.h:210
This is the base classes of all materials. A function in a derived material class returns a value wit...
Real diff_cf_
Definition: diffusion_reaction.h:72
Base class for all reaction models.
Definition: diffusion_reaction.h:163
Diffusion is biased along a specific direction.
Definition: diffusion_reaction.h:133
std::function< Real(StdVec< StdLargeVec< Real >> &, size_t particle_i)> ReactionFunctor
Definition: diffusion_reaction.h:167
Matd transformed_diffusivity_
Definition: diffusion_reaction.h:100
void initializeAnDiffusion(const std::string &diffusion_species_name, const std::string &gradient_species_name, ConstructorArgs &&...args)
Definition: diffusion_reaction.h:260
A wrapper to provide an ownership for a vector of base class pointers which point to derived objects...
Definition: ownership.h:127
Real bias_diff_cf_
Definition: diffusion_reaction.h:99
DiffusionReaction(BaseReactionModel &species_reaction, StdVec< std::string > species_name_list, ConstructorArgs &&...args)
Definition: diffusion_reaction.h:224
DerivedType * createPtr(ConstructorArgs &&...args)
Definition: ownership.h:133
Definition: solid_body_supplementary.cpp:9