Skip to content

Commit a5de325

Browse files
committed
command line tool
1 parent 5d713ba commit a5de325

File tree

2 files changed

+49
-85
lines changed

2 files changed

+49
-85
lines changed

polyfempy/command.py

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,30 @@ def polyfem():
66
parser = argparse.ArgumentParser()
77

88
parser.add_argument("-j", "--json", type=str,
9-
default="", help="Simulation json file")
10-
parser.add_argument("-m", "--mesh", type=str, default="", help="Mesh path")
11-
parser.add_argument("-b", "--febio", type=str,
12-
default="", help="FEBio file path")
9+
default="", help="Simulation JSON file")
1310

14-
parser.add_argument("--n_refs", type=int, default=0,
15-
help="Number of refinements")
16-
parser.add_argument("--not_norm", type=bool, default=True,
17-
help="Skips mesh normalization")
11+
parser.add_argument("-y", "--yaml", type=str,
12+
default="", help="Simulation YAML file")
1813

19-
parser.add_argument("--problem", type=str,
20-
default="", help="Problem name")
21-
parser.add_argument("--sform", type=str,
22-
default="", help="Scalar formulation")
23-
parser.add_argument("--tform", type=str,
24-
default="", help="Tensor formulation")
14+
parser.add_argument("--max_threads", type=int, default=1,
15+
help="Maximum number of threads")
2516

26-
parser.add_argument("--solver", type=str, default="", help="Solver to use")
17+
parser.add_argument("-s", "--strict_validation", action='store_true',
18+
help="Enables strict validation of input JSON")
2719

28-
parser.add_argument("-q", "-p", type=int, default=1,
29-
help="Discretization order")
30-
parser.add_argument("--p_ref", type=bool,
31-
default=False, help="Use p refimenet")
32-
# parser.add_argument("--spline", use_splines, "Use spline for quad/hex meshes");
33-
parser.add_argument("--count_flipped_els", type=bool,
34-
default=False, help="Count flippsed elements")
35-
parser.add_argument("--lin_geom", type=bool, default=False,
36-
help="Force use linear geometric mapping")
37-
# parser.add_argument("--isoparametric", isoparametric, "Force use isoparametric basis");
38-
# parser.add_argument("--serendipity", serendipity, "Use of serendipity elements, only for Q2");
39-
# parser.add_argument("--stop_after_build_basis", stop_after_build_basis, "Stop after build bases");
40-
parser.add_argument("--vis_mesh_res", type=float,
41-
default=-1.0, help="Vis mesh resolution")
42-
parser.add_argument("--project_to_psd", type=bool,
43-
default=False, help="Project local matrices to psd")
44-
parser.add_argument("--n_incr_load", type=int, default=-
45-
1, help="Number of incremeltal load")
46-
47-
parser.add_argument("--output", type=str, default="",
48-
help="Output json file")
49-
parser.add_argument("--vtu", type=str, default="", help="Vtu output file")
50-
51-
parser.add_argument("--quiet", type=bool, default=False,
52-
help="Disable cout for logging")
53-
parser.add_argument("--log_file", type=str,
54-
default="", help="Log to a file")
5520
parser.add_argument("--log_level", type=int, default=1,
5621
help="Log level 1 debug 2 info")
5722

58-
parser.add_argument("--export_material_params", type=bool,
59-
default=False, help="Export material parameters")
23+
parser.add_argument("-o", "--output_dir", type=str,
24+
default="", help="Directory for output files")
6025

6126
args = parser.parse_args()
6227

6328
polyfem_command(
64-
args.json,
65-
args.febio,
66-
args.mesh,
67-
args.problem,
68-
args.sform,
69-
args.tform,
70-
args.n_refs,
71-
args.not_norm,
72-
args.solver,
73-
args.q,
74-
args.p_ref,
75-
args.count_flipped_els,
76-
args.lin_geom,
77-
args.vis_mesh_res,
78-
args.project_to_psd,
79-
args.n_incr_load,
80-
args.output,
81-
args.vtu,
82-
args.log_level,
83-
args.log_file,
84-
args.quiet,
85-
args.export_material_params)
29+
json=args.json,
30+
yaml=args.yaml,
31+
log_level=args.log_level,
32+
strict_validation=args.strict_validation,
33+
max_threads=args.max_threads,
34+
output_dir=args.output_dir
35+
)

src/state/state.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -571,24 +571,41 @@ void define_solver(py::module_ &m)
571571
s.assemble_rhs();
572572
s.assemble_mass_mat();
573573

574-
s.solve_export_to_file = false;
575-
s.solution_frames.clear();
576574
Eigen::MatrixXd sol, pressure;
577575
s.solve_problem(sol, pressure);
578-
s.solve_export_to_file = true;
576+
577+
s.compute_errors(sol);
578+
579+
s.save_json(sol);
580+
s.export_data(sol, pressure);
581+
579582
return py::make_tuple(sol, pressure);
580583
},
581584
"solve the pde")
582585
.def(
583-
"init_timestepping",
584-
[](State &s, const double t0, const double dt) {
585-
init_globals(s);
586-
s.stats.compute_mesh_stats(*s.mesh);
586+
"build_basis",
587+
[](State &s) {
588+
if (!s.mesh)
589+
throw std::runtime_error("Load mesh first!");
587590

588591
s.build_basis();
592+
},
593+
"build finite element basis")
594+
.def(
595+
"assemble",
596+
[](State &s) {
597+
if (s.bases.size() == 0)
598+
throw std::runtime_error("Call build_basis() first!");
589599

590600
s.assemble_rhs();
591601
s.assemble_mass_mat();
602+
},
603+
"assemble RHS and mass matrix if needed")
604+
.def(
605+
"init_timestepping",
606+
[](State &s, const double t0, const double dt) {
607+
if (!s.solve_data.rhs_assembler || s.mass.size() == 0)
608+
throw std::runtime_error("Call assemble() first!");
592609

593610
s.solution_frames.clear();
594611
Eigen::MatrixXd sol, pressure;
@@ -639,23 +656,19 @@ void define_solver(py::module_ &m)
639656

640657
.def(
641658
"compute_errors",
642-
[](State &s, Eigen::MatrixXd &sol) {
643-
init_globals(s);
644-
// py::scoped_ostream_redirect output;
645-
646-
s.compute_errors(sol);
647-
},
659+
[](State &s, Eigen::MatrixXd &sol) { s.compute_errors(sol); },
648660
"compute the error", py::arg("solution"))
649661

650-
// .def("export_data", [](State &s) {
651-
// py::scoped_ostream_redirect output; s.export_data(); }, "exports all
652-
// data specified in the settings")
662+
.def(
663+
"export_data",
664+
[](State &s, const Eigen::MatrixXd &sol,
665+
const Eigen::MatrixXd &pressure) { s.export_data(sol, pressure); },
666+
"exports all data specified in the settings")
653667
.def(
654668
"export_vtu",
655669
[](State &s, const Eigen::MatrixXd &sol,
656670
const Eigen::MatrixXd &pressure, const double time,
657671
const double dt, std::string &path, bool boundary_only) {
658-
// py::scoped_ostream_redirect output;
659672
s.args["output"]["advanced"]["vis_boundary_only"] = boundary_only;
660673
s.out_geom.save_vtu(
661674
s.resolve_output_path(path), s, sol, pressure, time, dt,
@@ -1142,7 +1155,8 @@ void define_solve(py::module_ &m)
11421155
: load_yaml(yaml_file, in_args);
11431156

11441157
if (!ok)
1145-
log_and_throw_error(fmt::format("unable to open {} file", json_file));
1158+
throw std::runtime_error(
1159+
fmt::format("unable to open {} file", json_file));
11461160

11471161
json tmp = json::object();
11481162
tmp["/output/log/level"_json_pointer] = int(log_level);
@@ -1163,7 +1177,7 @@ void define_solve(py::module_ &m)
11631177

11641178
// Mesh was not loaded successfully; load_mesh() logged the error.
11651179
if (state.mesh == nullptr)
1166-
log_and_throw_error("Failed to load the mesh!");
1180+
throw std::runtime_error("Failed to load the mesh!");
11671181

11681182
state.stats.compute_mesh_stats(*state.mesh);
11691183

0 commit comments

Comments
 (0)