Alamo
PSRead.H
Go to the documentation of this file.
1 //
2 // Fill a domain (region where field=0) with packed spheres (regions where field=1).
3 // Sphere locations and radii are determined from an xyzr file.
4 //
5 
6 #ifndef IC_PSREAD_H_
7 #define IC_PSREAD_H_
8 
9 #include "Set/Set.H"
10 #include "IC/IC.H"
11 using namespace std;
12 #include <iostream>
13 #include <vector>
14 #include <algorithm>
15 #include <iterator>
16 #include "mpi.h"
17 #include "IO/ParmParse.H"
18 
19 namespace IC
20 {
21 class PSRead : public IC
22 {
23 public:
24  enum Type
25  {
27  Values
28  };
29 
30  PSRead(amrex::Vector<amrex::Geometry>& _geom) : IC(_geom) {}
31 
32  PSRead(amrex::Vector<amrex::Geometry>& _geom, IO::ParmParse& pp, std::string name) : IC(_geom)
33  {
34  pp_queryclass(name, *this);
35  }
36 
37  void Define() {
38 
39  };
40 
41  void Add(const int& lev, Set::Field<Set::Scalar>& a_phi, Set::Scalar)
42  {
43  Set::Vector size = Set::Size(geom[lev]);
44  int ncomp = a_phi[lev]->nComp();
45 
46  for (amrex::MFIter mfi(*a_phi[lev], amrex::TilingIfNotGPU()); mfi.isValid(); ++mfi)
47  {
48  amrex::Box bx;
49  amrex::IndexType type = a_phi[lev]->ixType();
50  if (type == amrex::IndexType::TheCellType()) bx = mfi.growntilebox();
51  else if (type == amrex::IndexType::TheNodeType()) bx = mfi.grownnodaltilebox();
52  else Util::Abort(INFO, "Unkonwn index type");
53 
54  amrex::Array4<Set::Scalar> const& phi = a_phi[lev]->array(mfi);
55  amrex::ParallelFor(bx, [=] AMREX_GPU_DEVICE(int i, int j, int k)
56  {
57  Set::Vector x = Set::Position(i, j, k, geom[lev], type);
58  Set::Scalar min_grain_id = 0;
59 
60  for (unsigned int n = 0; n < X.size(); n++)
61  {
62  Set::Scalar d;
63  Set::Scalar d1 = std::numeric_limits<Set::Scalar>::infinity();
64 
65  d = (x - X[n]).lpNorm<2>();
66 
67  if (geom[0].isPeriodic(0))
68  {
69  d1 = std::min((x - X[n] + size(0) * Set::Vector::Unit(0)).lpNorm<2>(),
70  (x - X[n] - size(0) * Set::Vector::Unit(0)).lpNorm<2>());
71  }
72 #if AMREX_SPACEDIM > 1
73  if (geom[0].isPeriodic(1))
74  {
75  d1 = std::min((x - X[n] + size(1) * Set::Vector::Unit(1)).lpNorm<2>(),
76  (x - X[n] - size(1) * Set::Vector::Unit(1)).lpNorm<2>());
77  }
78 #endif
79 #if AMREX_SPACEDIM > 2
80  if (geom[0].isPeriodic(2))
81  {
82  d1 = std::min((x - X[n] + size(2) * Set::Vector::Unit(2)).lpNorm<2>(),
83  (x - X[n] - size(2) * Set::Vector::Unit(2)).lpNorm<2>());
84  }
85 #endif
86 
87  d = std::min(d, d1);
88 
89  if (d <= (R[n] + eps))
90 
91  {
92  Set::Scalar m = 0.5 * (1 + erf((-d + R[n]) / (eps)));
93  min_grain_id = min_grain_id + m * (1. - min_grain_id);
94  }
95  }
96 
97  phi(i, j, k, 0) = min_grain_id;
98  if (ncomp > 1) phi(i, j, k, 1) = 1.0 - min_grain_id;
99  });
100  }
101  }
102 
103  static void Parse(PSRead& value, IO::ParmParse& pp)
104  {
105  std::string filename;
106  int verbose = 0;
107  pp_query("eps", value.eps); // Diffuseness of the sphere boundary
108  pp_query_file("filename", filename); // Location of .xyzr file
109  pp_query("verbose", verbose); // Verbosity (used in parser only)
110  pp_query("mult",value.mult); // Coordinate multiplier
111  if (pp.contains("x0"))
112  pp_queryarr("x0",value.x0); // Coordinate offset
113  std::ifstream datafile(filename);
114  std::string line;
115  if (datafile.is_open())
116  {
117  value.X.clear();
118  value.R.clear();
119 
120  while (getline(datafile, line))
121  {
122  std::istringstream in(line);
123 
124  std::string strx, stry, strz, strR;
125  in >> strx >> stry >> strz >> strR;
126  value.X.push_back(value.x0 + value.mult*Set::Vector(AMREX_D_DECL(std::stod(strx), std::stod(stry), std::stod(strz))));
127  value.R.push_back(value.mult*std::stod(strR));
128  if (verbose > 0)
129  Util::Message(INFO, "x=", value.X.back().transpose(), " r=", value.R.back());
130  }
131  datafile.close();
132  }
133  else
134  {
135  Util::Abort(INFO, "Unable to open file ", filename);
136  }
137  }
138 
139 private:
140  std::vector<Set::Vector> X;
141  std::vector<Set::Scalar> R;
143  Set::Scalar mult = 1.0;
144  Set::Vector x0 = Set::Vector::Zero();
145 };
146 }
147 #endif
Util::filename
std::string filename
Definition: Util.cpp:19
IC::PSRead::Partition
@ Partition
Definition: PSRead.H:26
Set::Position
AMREX_FORCE_INLINE Vector Position(const int &i, const int &j, const int &k, const amrex::Geometry &geom, const amrex::IndexType &ixType)
Definition: Base.H:121
IC::PSRead::R
std::vector< Set::Scalar > R
Definition: PSRead.H:141
BC::AMREX_D_DECL
@ AMREX_D_DECL
Definition: BC.H:34
IC::PSRead
Definition: PSRead.H:21
Set::Field< Set::Scalar >
Definition: Set.H:236
X
#define X(name)
IC::PSRead::Type
Type
Definition: PSRead.H:24
IC::PSRead::X
std::vector< Set::Vector > X
Definition: PSRead.H:140
Set::Vector
Eigen::Matrix< amrex::Real, AMREX_SPACEDIM, 1 > Vector
Definition: Base.H:20
ParmParse.H
pp_query
#define pp_query(...)
Definition: ParmParse.H:104
Set::Size
AMREX_FORCE_INLINE Vector Size(const amrex::Geometry &geom)
Definition: Base.H:161
Set::Scalar
amrex::Real Scalar
Definition: Base.H:19
pp_queryclass
#define pp_queryclass(...)
Definition: ParmParse.H:105
IC::PSRead::x0
Set::Vector x0
Definition: PSRead.H:144
IC::PSRead::Parse
static void Parse(PSRead &value, IO::ParmParse &pp)
Definition: PSRead.H:103
IC::PSRead::eps
Set::Scalar eps
Definition: PSRead.H:142
IC::PSRead::PSRead
PSRead(amrex::Vector< amrex::Geometry > &_geom)
Definition: PSRead.H:30
pp_query_file
#define pp_query_file(...)
Definition: ParmParse.H:102
IO::ParmParse::contains
bool contains(std::string name)
Definition: ParmParse.H:151
IC::PSRead::mult
Set::Scalar mult
Definition: PSRead.H:143
Util::Abort
void Abort(const char *msg)
Definition: Util.cpp:165
Set.H
IC
Definition: Affine.H:18
IO::ParmParse
Definition: ParmParse.H:110
INFO
#define INFO
Definition: Util.H:20
IC.H
IC::PSRead::PSRead
PSRead(amrex::Vector< amrex::Geometry > &_geom, IO::ParmParse &pp, std::string name)
Definition: PSRead.H:32
IC::PSRead::Add
void Add(const int &lev, Set::Field< Set::Scalar > &a_phi, Set::Scalar)
Definition: PSRead.H:41
Util::Message
void Message(std::string file, std::string func, int line, Args const &... args)
Definition: Util.H:133
IC::PSRead::Define
void Define()
Definition: PSRead.H:37
pp_queryarr
#define pp_queryarr(...)
Definition: ParmParse.H:103