Line data Source code
1 : //
2 : //
3 :
4 : #ifndef BC_EXPRESSION_H_
5 : #define BC_EXPRESSION_H_
6 :
7 : #include <AMReX_ParallelDescriptor.H>
8 : #include <AMReX_ParmParse.H>
9 : #include <AMReX_BCRec.H>
10 : #include <AMReX_PhysBCFunct.H>
11 : #include <AMReX_Array.H>
12 : #include <AMReX_Parser.H>
13 :
14 : #include "Set/Set.H"
15 : #include "BC/BC.H"
16 : #include "Numeric/Interpolator/Linear.H"
17 :
18 : namespace BC
19 : {
20 : class Expression
21 : : public BC<Set::Scalar>
22 : {
23 : public:
24 : static constexpr const char* name = "expression";
25 :
26 : //static constexpr const char* const Elastic::strings[];
27 : #if AMREX_SPACEDIM==2
28 : static const constexpr char * const facestr[] = {
29 : "xlo","ylo","xhi","yhi"
30 : };
31 : #elif AMREX_SPACEDIM==3
32 : static const constexpr char * const facestr[] = {
33 : "xlo","ylo","zlo","xhi","yhi","zhi",
34 : };
35 : #endif
36 :
37 : #if AMREX_SPACEDIM==2
38 : enum Face {
39 : XLO, YLO, XHI, YHI,
40 : INT
41 : };
42 : #elif AMREX_SPACEDIM==3
43 : enum Face {
44 : XLO, YLO, ZLO, XHI, YHI, ZHI, // 6
45 : INT
46 : };
47 : #endif
48 :
49 : public:
50 : //Constant (amrex::Vector<amrex::Geometry> &_geom) : geom(_geom) {};
51 : Expression(int a_ncomp) : m_ncomp(a_ncomp) {};
52 0 : Expression(int a_ncomp, IO::ParmParse& pp, std::string name) : m_ncomp(a_ncomp)
53 : {
54 0 : pp_queryclass(name, *this);
55 0 : };
56 :
57 0 : virtual ~Expression() {};
58 :
59 : virtual void FillBoundary(amrex::BaseFab<Set::Scalar>& in, const amrex::Box& box,
60 : int ngrow, int dcomp, int ncomp, amrex::Real time,
61 : Orientation face = Orientation::All,
62 : const amrex::Mask* mask = nullptr) override;
63 :
64 : using BC::FillBoundary;
65 :
66 : amrex::BCRec GetBCRec() override;
67 : virtual amrex::Array<int, AMREX_SPACEDIM> IsPeriodic() override;
68 : virtual amrex::Periodicity Periodicity() const override;
69 : virtual amrex::Periodicity Periodicity(const amrex::Box& b) override;
70 :
71 :
72 :
73 : template<class T>
74 : const amrex::Array<amrex::Array<T, AMREX_SPACEDIM>, 2> GetBCTypes()
75 : {
76 : return { {{AMREX_D_DECL((T)m_bc_type[Face::XLO][0],(T)m_bc_type[Face::YLO][0],(T)m_bc_type[Face::ZLO][0])},
77 : {AMREX_D_DECL((T)m_bc_type[Face::XLO][0],(T)m_bc_type[Face::YLO][0],(T)m_bc_type[Face::ZLO][0])}} };
78 : }
79 :
80 :
81 : private:
82 : #if AMREX_SPACEDIM==2
83 : static const int m_nfaces = 4;
84 : #elif AMREX_SPACEDIM==3
85 : static const int m_nfaces = 6;
86 : #endif
87 :
88 : unsigned int m_ncomp = 0;
89 :
90 : std::array<std::vector<int>, m_nfaces> m_bc_type;
91 : std::array<std::vector<amrex::Parser>, m_nfaces> m_bc_func_parser;
92 : std::array<std::vector<amrex::ParserExecutor<4>>, m_nfaces> m_bc_func;
93 :
94 : //std::array<std::vector<Numeric::Interpolator::Linear<Set::Scalar>>, m_nfaces> m_bc_val;
95 :
96 : public:
97 0 : static void Parse(Expression& value, IO::ParmParse& pp)
98 : {
99 0 : std::map<std::string, int> bcmap;
100 0 : bcmap["BOGUS_BC"] = amrex::BCType::mathematicalBndryTypes::bogus;
101 0 : bcmap["INT_DIR"] = amrex::BCType::mathematicalBndryTypes::int_dir;
102 0 : bcmap["REFLECT_ODD"] = amrex::BCType::mathematicalBndryTypes::reflect_odd;
103 0 : bcmap["INT_DIR"] = amrex::BCType::mathematicalBndryTypes::int_dir;
104 0 : bcmap["REFLECT_EVEN"] = amrex::BCType::mathematicalBndryTypes::reflect_even;
105 0 : bcmap["FOEXTRAP"] = amrex::BCType::mathematicalBndryTypes::foextrap;
106 0 : bcmap["EXT_DIR"] = amrex::BCType::mathematicalBndryTypes::ext_dir;
107 0 : bcmap["HOEXTRAP"] = amrex::BCType::mathematicalBndryTypes::hoextrap;
108 0 : bcmap["Interior"] = amrex::BCType::mathematicalBndryTypes::int_dir;
109 0 : bcmap["Inflow"] = amrex::BCType::mathematicalBndryTypes::ext_dir;
110 0 : bcmap["Outflow"] = amrex::BCType::mathematicalBndryTypes::foextrap;
111 0 : bcmap["Symmetry"] = amrex::BCType::mathematicalBndryTypes::reflect_even;
112 0 : bcmap["SlipWall"] = amrex::BCType::mathematicalBndryTypes::ext_dir;
113 0 : bcmap["NoSlipWall"] = amrex::BCType::mathematicalBndryTypes::ext_dir;
114 : // From <AMReX_LO_BCTYPES.H>
115 0 : bcmap["interior"] = (int)amrex::LinOpBCType::interior;
116 0 : bcmap["Dirichlet"] = (int)amrex::LinOpBCType::Dirichlet;
117 0 : bcmap["dirichlet"] = (int)amrex::LinOpBCType::Dirichlet;
118 0 : bcmap["Neumann"] = (int)amrex::LinOpBCType::Neumann;
119 0 : bcmap["NEUMANN"] = (int)amrex::LinOpBCType::Neumann;
120 0 : bcmap["neumann"] = (int)amrex::LinOpBCType::Neumann;
121 0 : bcmap["reflect_odd"] = (int)amrex::LinOpBCType::reflect_odd;
122 0 : bcmap["Marshak"] = (int)amrex::LinOpBCType::Marshak;
123 0 : bcmap["SanchezPomraning"] = (int)amrex::LinOpBCType::SanchezPomraning;
124 0 : bcmap["inflow"] = (int)amrex::LinOpBCType::inflow;
125 0 : bcmap["Periodic"] = (int)amrex::LinOpBCType::Periodic;
126 0 : bcmap["periodic"] = (int)amrex::LinOpBCType::Periodic;
127 :
128 :
129 :
130 : // TYPES
131 :
132 0 : std::vector<std::string> str;
133 0 : pp_queryarr("type.xlo", str); // BC type on the lower x edge (2d) face (3d)
134 0 : for (unsigned int i = 0; i < str.size(); i++) if (!bcmap.count(str[i])) Util::Abort(INFO, "Invalid BC: ", str[i]);
135 0 : if (str.size() == value.m_ncomp) for (unsigned int i = 0; i < value.m_ncomp; i++) value.m_bc_type[Face::XLO].push_back(bcmap[str[i]]);
136 0 : else if (str.size() == 1) value.m_bc_type[Face::XLO].resize(value.m_ncomp, bcmap[str[0]]);
137 0 : else Util::Abort(INFO, "Incorrect number of ", pp.prefix(), " BC type args: expected ", value.m_ncomp, " or 1 but got ", str.size());
138 0 : pp_queryarr("type.xhi", str); // BC type on the upper x edge (2d) face (3d)
139 0 : for (unsigned int i = 0; i < str.size(); i++) if (!bcmap.count(str[i])) Util::Abort(INFO, "Invalid BC: ", str[i]);
140 0 : if (str.size() == value.m_ncomp) for (unsigned int i = 0; i < value.m_ncomp; i++) value.m_bc_type[Face::XHI].push_back(bcmap[str[i]]);
141 0 : else if (str.size() == 1) value.m_bc_type[Face::XHI].resize(value.m_ncomp, bcmap[str[0]]);
142 0 : else Util::Abort(INFO, "Incorrect number of ", pp.prefix(), " BC type args: expected ", value.m_ncomp, " or 1 but got ", str.size());
143 0 : pp_queryarr("type.ylo", str); // BC type on the lower y edge (2d) face (3d)
144 0 : for (unsigned int i = 0; i < str.size(); i++) if (!bcmap.count(str[i])) Util::Abort(INFO, "Invalid BC: ", str[i]);
145 0 : if (str.size() == value.m_ncomp) for (unsigned int i = 0; i < value.m_ncomp; i++) value.m_bc_type[Face::YLO].push_back(bcmap[str[i]]);
146 0 : else if (str.size() == 1) value.m_bc_type[Face::YLO].resize(value.m_ncomp, bcmap[str[0]]);
147 0 : else Util::Abort(INFO, "Incorrect number of ", pp.prefix(), " BC type args: expected ", value.m_ncomp, " or 1 but got ", str.size());
148 0 : pp_queryarr("type.yhi", str); // BC type on the upper y edge (2d) face (3d)
149 0 : for (unsigned int i = 0; i < str.size(); i++) if (!bcmap.count(str[i])) Util::Abort(INFO, "Invalid BC: ", str[i]);
150 0 : if (str.size() == value.m_ncomp) for (unsigned int i = 0; i < value.m_ncomp; i++) value.m_bc_type[Face::YHI].push_back(bcmap[str[i]]);
151 0 : else if (str.size() == 1) value.m_bc_type[Face::YHI].resize(value.m_ncomp, bcmap[str[0]]);
152 0 : else Util::Abort(INFO, "Incorrect number of ", pp.prefix(), " BC type args: expected ", value.m_ncomp, " or 1 but got ", str.size());
153 0 : pp_queryarr("type.zlo", str); // BC type on the lower z face (processed but ignored in 2d to prevent unused input errors)
154 : #if AMREX_SPACEDIM==3
155 0 : for (unsigned int i = 0; i < str.size(); i++) if (!bcmap.count(str[i])) Util::Abort(INFO, "Invalid BC: ", str[i]);
156 0 : if (str.size() == value.m_ncomp) for (unsigned int i = 0; i < value.m_ncomp; i++) value.m_bc_type[Face::ZLO].push_back(bcmap[str[i]]);
157 0 : else if (str.size() == 1) value.m_bc_type[Face::ZLO].resize(value.m_ncomp, bcmap[str[0]]);
158 0 : else Util::Abort(INFO, "Incorrect number of ", pp.prefix(), " BC type args: expected ", value.m_ncomp, " or 1 but got ", str.size());
159 : #endif
160 0 : pp_queryarr("type.zhi", str); // BC type on the upper z face (processed but ignored in 2d to prevent unused input errors)
161 : #if AMREX_SPACEDIM==3
162 0 : for (unsigned int i = 0; i < str.size(); i++) if (!bcmap.count(str[i])) Util::Abort(INFO, "Invalid BC: ", str[i]);
163 0 : if (str.size() == value.m_ncomp) for (unsigned int i = 0; i < value.m_ncomp; i++) value.m_bc_type[Face::ZHI].push_back(bcmap[str[i]]);
164 0 : else if (str.size() == 1) value.m_bc_type[Face::ZHI].resize(value.m_ncomp, bcmap[str[0]]);
165 0 : else Util::Abort(INFO, "Incorrect number of ", pp.prefix(), " BC type args: expected ", value.m_ncomp, " or 1 but got ", str.size());
166 : #endif
167 :
168 : // VALS
169 0 : std::vector<std::string> val;
170 0 : for (int face = 0; face != Face::INT; face++)
171 : {
172 0 : std::string querystr = std::string("val.") + std::string(facestr[face]);
173 0 : if (pp.contains(querystr.c_str())) pp_queryarr(querystr.c_str(),val);
174 0 : else val.resize(value.m_ncomp,"0.0");
175 0 : if (val.size() != value.m_ncomp) Util::Abort(INFO,"Incorrect number of expressions specified for ",querystr,": expected 1 or ",value.m_ncomp," but received ",val.size());
176 :
177 0 : value.m_bc_func_parser[face].clear();
178 0 : value.m_bc_func[face].clear();
179 0 : for (unsigned int i = 0 ; i < value.m_ncomp; i++)
180 : {
181 0 : value.m_bc_func_parser[face].push_back( amrex::Parser(val[i].c_str()) );
182 0 : value.m_bc_func_parser[face][i].registerVariables({"x","y","z","t"});
183 0 : value.m_bc_func[face].push_back( value.m_bc_func_parser[face][i].compile<4>() );
184 : }
185 0 : }
186 :
187 : #if AMREX_SPACEDIM==2
188 : // We may wish to use an input file that has 3D BC inputs
189 : // This will prevent the parser from complaining that there are unused inputs.
190 : std::vector<std::string> ignore_in_2d = {
191 : "zlo","zhi",
192 : "zhixlo","zloxlo","zhixhi","zloxhi","ylozlo","ylozhi","yhizlo","yhizhi",
193 0 : "xloylozlo","xloylozhi","xloyhizlo","xloyhizhi","xhiylozlo","xhiylozhi","xhiyhizlo","xhiyhizhi"};
194 0 : for (unsigned int n = 0; n < ignore_in_2d.size(); n++)
195 : {
196 0 : std::string querystr = std::string("val.") + ignore_in_2d[n];
197 0 : pp.ignore(querystr);
198 0 : querystr = std::string("type.") + ignore_in_2d[n];
199 0 : pp.ignore(querystr);
200 0 : }
201 : #endif
202 :
203 0 : }
204 : };
205 : }
206 : #endif
|