14#include <symengine/parser.h>
15#include <symengine/expression.h>
16#include <symengine/lambda_double.h>
25PetscErrorCode createDirectory(
const std::string &dir)
27 PetscFunctionBeginUser;
29 if (mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == -1)
33 SETERRQ2(PETSC_COMM_WORLD, 1,
34 "Could not create the folder \"%s\" (%s).\n", dir.c_str(),
39 PetscFunctionReturn(0);
59 if (!node.IsDefined())
return false;
79 if (!node.IsDefined())
return false;
99 if (!node.IsDefined())
return false;
118 if (!node.IsDefined())
return false;
127struct convert<PetscBool>
129 static Node encode(
const PetscBool &b)
136 static bool decode(
const Node &node, PetscBool &b)
138 if (!node.IsDefined())
return false;
140 b = PetscBool(node.as<
bool>());
152PetscErrorCode
readYAMLFile(
const std::string &filePath, YAML::Node &node)
156 PetscFunctionBeginUser;
161 tmp = YAML::LoadFile(filePath);
163 catch (YAML::BadFile &err)
165 SETERRQ1(PETSC_COMM_WORLD, PETSC_ERR_FILE_OPEN,
"Unable to open %s",
169 for (
auto item : tmp) node[item.first.as<std::string>()] = item.second;
171 PetscFunctionReturn(0);
178 char s[PETSC_MAX_PATH_LEN];
180 std::string filePath;
182 PetscFunctionBeginUser;
185 node[
"directory"] =
"./";
186 ierr = PetscOptionsGetString(
nullptr,
nullptr,
"-directory", s,
sizeof(s),
187 &flag); CHKERRQ(ierr);
188 if (flag) node[
"directory"] = s;
193 node[
"output"] = node[
"directory"].as<std::string>() +
"/output";
195 PetscOptionsGetString(
nullptr,
nullptr,
"-output", s,
sizeof(s), &flag);
197 if (flag) node[
"output"] = s;
198 ierr = createDirectory(node[
"output"].as<std::string>()); CHKERRQ(ierr);
203 node[
"logs"] = node[
"output"].as<std::string>() +
"/logs";
205 PetscOptionsGetString(
nullptr,
nullptr,
"-logs", s,
sizeof(s), &flag);
207 if (flag) node[
"logs"] = s;
208 ierr = createDirectory(node[
"logs"].as<std::string>()); CHKERRQ(ierr);
213 filePath = node[
"directory"].as<std::string>() +
"/config.yaml";
214 ierr = PetscOptionsGetString(
215 nullptr,
nullptr,
"-config", s,
sizeof(s), &flag); CHKERRQ(ierr);
216 if (flag) filePath = s;
221 std::vector<const char *> cliOpts = {
"-mesh",
"-flow",
"-parameters",
222 "-bodies",
"-probes"};
223 for (
auto cliOpt : cliOpts)
227 ierr = PetscOptionsGetString(
nullptr,
nullptr, cliOpt, s,
sizeof(s),
228 &flag); CHKERRQ(ierr);
232 ierr =
readYAMLFile(std::string(s), node); CHKERRQ(ierr);
236 PetscFunctionReturn(0);
239PetscErrorCode
parseMesh(
const YAML::Node &meshNode, PetscInt &dim,
243 using namespace type;
245 PetscFunctionBeginUser;
250 dim = meshNode.size();
253 for (
auto ax : meshNode)
257 PetscInt dir, nTotalAx;
258 PetscReal bgAx, edAx;
262 ierr =
parseOneAxis(ax, dir, bgAx, edAx, nTotalAx, dLAx);
268 nTotal[dir] = nTotalAx;
272 PetscFunctionReturn(0);
276 PetscReal &bg, PetscReal &ed, PetscInt &nTotal,
279 PetscFunctionBeginUser;
286 dir = PetscInt(axis[
"direction"].as<type::Dir>());
289 bg = axis[
"start"].as<PetscReal>();
295 PetscFunctionReturn(0);
299 PetscInt &nTotal, PetscReal &ed,
302 PetscFunctionBeginUser;
316 for (
auto sub : subs)
328 dL.insert(dL.end(), dLSub.begin(), dLSub.end());
331 PetscFunctionReturn(0);
335 PetscInt &n, PetscReal &ed,
338 PetscFunctionBeginUser;
341 n = sub[
"cells"].as<PetscInt>();
344 ed = sub[
"end"].as<PetscReal>();
347 PetscReal r = sub[
"stretchRatio"].as<PetscReal>();
350 if (std::abs(r - 1.0) <= 1e-12)
355 PetscFunctionReturn(0);
362 PetscFunctionBeginUser;
364 if (!node[
"flow"].IsDefined())
365 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
366 "Could not find the key \"flow\" in the YAML "
367 "node passed to parseBCs.\n");
369 if (!node[
"flow"][
"boundaryConditions"].IsDefined())
370 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
371 "Could not find the key \"boundaryConditions\" under the key "
372 "\"flow\" in the YAML node passed to parseBCs.\n");
377 for (
auto sub : node[
"flow"][
"boundaryConditions"])
379 int loc = int(sub[
"location"].as<type::BCLoc>());
381 for (
auto ssub : sub)
383 if (ssub.first.as<std::string>() !=
"location")
386 bcTypes[f][loc] = int(ssub.second[0].as<
type::BCType>());
387 bcValues[f][loc] = ssub.second[1].as<PetscReal>();
392 PetscFunctionReturn(0);
397 const YAML::Node &node,
398 std::vector<SymEngine::LambdaRealDoubleVisitor> &lambdas)
400 PetscFunctionBeginUser;
402 if (!node[
"flow"].IsDefined())
403 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
404 "Could not find the key \"flow\" in the YAML "
405 "node passed to parseICs.\n");
407 if (!node[
"flow"][
"initialVelocity"].IsDefined())
408 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_ARG_OUTOFRANGE,
409 "Could not find the key \"initialVelocity\" under the key "
410 "\"flow\" in the YAML node passed to parseICs.\n");
412 const YAML::Node &temp = node[
"flow"][
"initialVelocity"];
415 lambdas = std::vector<SymEngine::LambdaRealDoubleVisitor>(4);
418 auto x = SymEngine::symbol(
"x");
419 auto y = SymEngine::symbol(
"y");
420 auto z = SymEngine::symbol(
"z");
421 auto t = SymEngine::symbol(
"t");
422 auto nu = SymEngine::symbol(
"nu");
424 for (
unsigned int i = 0; i < temp.size(); i++)
427 auto expr = SymEngine::parse(temp[i].as<std::string>());
430 lambdas[i].init({
x,
y,
z, t, nu}, *expr);
434 if (node[
"flow"][
"initialPressure"].IsDefined()){
435 auto expr = SymEngine::parse(node[
"flow"][
"initialPressure"].as<std::string>());
436 lambdas[3].init({
x,
y,
z, t, nu}, *expr);
438 auto expr = SymEngine::parse(
"0");
439 lambdas[3].init({
x,
y,
z, t, nu}, *expr);
442 PetscFunctionReturn(0);
PetscErrorCode parseOneAxis(const YAML::Node &axis, PetscInt &dir, PetscReal &bg, PetscReal &ed, PetscInt &nTotal, type::RealVec1D &dL)
Parse the info of only one direction from YAML node.
PetscErrorCode getSettings(YAML::Node &node)
Get configuration settings.
PetscErrorCode parseSubDomains(const YAML::Node &subs, const PetscReal bg, PetscInt &nTotal, PetscReal &ed, type::RealVec1D &dL)
Parse all sub-domains in a direction.
PetscErrorCode parseMesh(const YAML::Node &meshNode, PetscInt &dim, type::RealVec1D &bg, type::RealVec1D &ed, type::IntVec1D &nTotal, type::RealVec2D &dL)
Parse a YAML node of cartesianMesh.
PetscErrorCode parseBCs(const YAML::Node &node, type::IntVec2D &bcTypes, type::RealVec2D &bcValues)
Parse boundary conditions from a YAML node.
PetscErrorCode readYAMLFile(const std::string &filePath, YAML::Node &node)
Load the content of a YAML file into a YAML node.
PetscErrorCode stretchGrid(const PetscReal &bg, const PetscReal &ed, const PetscInt &n, const PetscReal &r, type::RealVec1D &dL)
Calculate and return cell sizes of stretched grid in one direction.
PetscErrorCode parseOneSubDomain(const YAML::Node &sub, const PetscReal bg, PetscInt &n, PetscReal &ed, type::RealVec1D &dL)
Parse only one sub-domain.
PetscErrorCode parseICs(const YAML::Node &node, std::vector< SymEngine::LambdaRealDoubleVisitor > &ics)
Parse initial conditions from a YAML node.
std::vector< IntVec1D > IntVec2D
2D std::vector holding PetscInt.
std::map< BCLoc, std::string > bl2str
Mapping between BCLoc and std::string.
BCType
Type of boundary conditions.
Dir
Legal physical directions.
std::map< Field, std::string > fd2str
Mapping between Field and std::string.
std::map< std::string, BCLoc > str2bl
Mapping between std::string and BCLoc.
std::map< std::string, BCType > str2bt
Mapping between std::string and BCType.
std::vector< RealVec1D > RealVec2D
2D std::vector holding PetscReal.
std::vector< PetscInt > IntVec1D
1D std::vector holding PetscInt.
BCLoc
Location of a boundary.
std::map< Dir, std::string > dir2str
Mapping between Dir and std::string.
std::map< BCType, std::string > bt2str
mapping between BCType and std::string.
std::map< std::string, Dir > str2dir
Mapping between std::string and Dir.
std::vector< PetscReal > RealVec1D
1D std::vector holding PetscReal.
std::map< std::string, Field > str2fd
Mapping between std::string and Field.
Prototypes of some miscellaneous functions.
A toolbox for building flow solvers.
Prototypes of parser functions.