OpenFPM  5.2.0
Project that contain the implementation of distributed structures
HelpFunctions_diffusion.hpp
1 //
2 // Created by jstark on 08.10.21
3 //
4 
5 #ifndef DIFFUSION_HELPFUNCTIONSDIFFUSION_HPP
6 #define DIFFUSION_HELPFUNCTIONSDIFFUSION_HPP
7 #include "util/PathsAndFiles.hpp"
8 
10 
19 template <typename grid_type, typename T>
20 T diffusion_time_step(grid_type & grid, T k_max)
21 {
22  T sum = 0;
23  for(int d = 0; d < grid_type::dims; ++d)
24  {
25  sum += (T)1.0 / (T)(grid.spacing(d) * grid.spacing(d));
26  }
27 
28  return 1.0 / ((T)4.0 * k_max * sum);
29 }
30 
42 template <typename T>
43 T get_smooth_sigmoidal(T x, T x_shift, T x_stretch, T y_min, T y_max)
44 {
45  return y_min + y_max / (1.0 + exp(- (x_shift + x_stretch * x)));
46 }
47 
55 template <size_t Prop, typename grid_type>
56 auto sum_prop_over_grid(grid_type & grid)
57 {
58  auto sum = 0.0;
59  auto dom = grid.getDomainIterator();
60  while(dom.isNext())
61  {
62  auto key = dom.get();
63  sum += grid.template getProp<Prop>(key);
64  ++dom;
65  }
66  auto &v_cl = create_vcluster();
67  v_cl.sum(sum);
68  v_cl.execute();
69  return sum;
70 }
71 
84 template <size_t Conc, typename grid_type, typename T, typename T_mass>
85 void monitor_total_mass(grid_type & grid, const T_mass m_initial, const T p_volume, const T t, const size_t i,
86  const std::string & path_output, const std::string & filename="total_mass.csv")
87 {
88  auto &v_cl = create_vcluster();
89 
90 // if (m_initial == 0)
91 // {
92 // if (v_cl.rank() == 0) std::cout << "m_initial is zero! Normalizing the total mass with the initial mass will "
93 // "not work. Aborting..." << std::endl;
94 // abort();
95 // }
96 
97  T m_total = sum_prop_over_grid<Conc>(grid) * p_volume;
98  T m_diff = m_total - m_initial;
99  T m_max = get_max_val<Conc>(grid) * p_volume;
100 
101  if (v_cl.rank() == 0)
102  {
103  std::string outpath = path_output + "/" + filename;
104  create_file_if_not_exist(outpath);
105  std::ofstream outfile;
106  outfile.open(outpath, std::ios_base::app); // append instead of overwrite
107 
108  if (i == 0)
109  {
110  outfile << "Time, Total mass, Mass difference, Max mass" << std::endl;
111 // std::cout << "Time, Total mass, Total mass in-/decrease, Max mass" << std::endl;
112  }
113 
114  outfile
115  << to_string_with_precision(t, 6) << ","
116  << to_string_with_precision(m_total, 6) << ","
117  << to_string_with_precision(m_diff, 6) << ","
118  << to_string_with_precision(m_max, 6)
119  << std::endl;
120  outfile.close();
121 #if 0
122  std::cout
123  << to_string_with_precision(t, 6) << ","
124  << to_string_with_precision(m_total, 6) << ","
125  << to_string_with_precision(m_diff, 6) << ","
126  << to_string_with_precision(m_max, 6)
127  << std::endl;
128 #endif
129 // if (m_domain_normalized > 100 || m_domain_normalized < 0)
130 // {
131 // std::cout << "Mass increases or is negative, something went wrong. Aborting..." << std::endl;
132 // abort();
133 // }
134  }
135 }
136 
147 template <size_t Conc, typename grid_type, typename T>
148 void monitor_total_concentration(grid_type & grid, const T t, const size_t i,
149  const std::string & path_output, const std::string & filename="total_conc.csv")
150 {
151  auto &v_cl = create_vcluster();
152  T c_total = sum_prop_over_grid<Conc>(grid);
153 
154  if (v_cl.rank() == 0)
155  {
156  std::string outpath = path_output + "/" + filename;
157  create_file_if_not_exist(outpath);
158  std::ofstream outfile;
159  outfile.open(outpath, std::ios_base::app); // append instead of overwrite
160 
161  if (i == 0)
162  {
163  outfile << "Time, Total concentration" << std::endl;
164  }
165 
166  outfile
167  << to_string_with_precision(t, 6) << ","
168  << to_string_with_precision(c_total, 6)
169  << std::endl;
170  outfile.close();
171  }
172 }
173 
185 template <size_t Conc, size_t Emission, typename grid_type, typename key_vector_type, typename T>
186 void adapt_emission(grid_type & grid, key_vector_type & keys, T threshold)
187 {
188  for (int i = 0; i < keys.size(); ++i)
189  {
190  auto key = keys.get(i);
191  if (grid.template getProp<Conc>(key) > threshold) grid.template getProp<Emission>(key) = 0.0;
192  }
193 }
194 
195 #endif //DIFFUSION_HELPFUNCTIONSDIFFUSION_HPP
Header file containing help-functions that perform on OpenFPM-grids.
std::string to_string_with_precision(const T myValue, const size_t n=6)
Converts value into string maintaining a desired precision.
Header file containing functions for creating files and folders.
static void create_file_if_not_exist(std::string path)
Creates a file if not already existent.
This is a distributed grid.
static const unsigned int dims
Number of dimensions.
It model an expression expr1 + ... exprn.
Definition: sum.hpp:93