Skip to content

update polyfem #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
command line tool
  • Loading branch information
Huangzizhou committed Jul 1, 2024
commit a5de325da0f9cb689a911c40cac8f441947e8ac5
82 changes: 16 additions & 66 deletions polyfempy/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,30 @@ def polyfem():
parser = argparse.ArgumentParser()

parser.add_argument("-j", "--json", type=str,
default="", help="Simulation json file")
parser.add_argument("-m", "--mesh", type=str, default="", help="Mesh path")
parser.add_argument("-b", "--febio", type=str,
default="", help="FEBio file path")
default="", help="Simulation JSON file")

parser.add_argument("--n_refs", type=int, default=0,
help="Number of refinements")
parser.add_argument("--not_norm", type=bool, default=True,
help="Skips mesh normalization")
parser.add_argument("-y", "--yaml", type=str,
default="", help="Simulation YAML file")

parser.add_argument("--problem", type=str,
default="", help="Problem name")
parser.add_argument("--sform", type=str,
default="", help="Scalar formulation")
parser.add_argument("--tform", type=str,
default="", help="Tensor formulation")
parser.add_argument("--max_threads", type=int, default=1,
help="Maximum number of threads")

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

parser.add_argument("-q", "-p", type=int, default=1,
help="Discretization order")
parser.add_argument("--p_ref", type=bool,
default=False, help="Use p refimenet")
# parser.add_argument("--spline", use_splines, "Use spline for quad/hex meshes");
parser.add_argument("--count_flipped_els", type=bool,
default=False, help="Count flippsed elements")
parser.add_argument("--lin_geom", type=bool, default=False,
help="Force use linear geometric mapping")
# parser.add_argument("--isoparametric", isoparametric, "Force use isoparametric basis");
# parser.add_argument("--serendipity", serendipity, "Use of serendipity elements, only for Q2");
# parser.add_argument("--stop_after_build_basis", stop_after_build_basis, "Stop after build bases");
parser.add_argument("--vis_mesh_res", type=float,
default=-1.0, help="Vis mesh resolution")
parser.add_argument("--project_to_psd", type=bool,
default=False, help="Project local matrices to psd")
parser.add_argument("--n_incr_load", type=int, default=-
1, help="Number of incremeltal load")

parser.add_argument("--output", type=str, default="",
help="Output json file")
parser.add_argument("--vtu", type=str, default="", help="Vtu output file")

parser.add_argument("--quiet", type=bool, default=False,
help="Disable cout for logging")
parser.add_argument("--log_file", type=str,
default="", help="Log to a file")
parser.add_argument("--log_level", type=int, default=1,
help="Log level 1 debug 2 info")

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

args = parser.parse_args()

polyfem_command(
args.json,
args.febio,
args.mesh,
args.problem,
args.sform,
args.tform,
args.n_refs,
args.not_norm,
args.solver,
args.q,
args.p_ref,
args.count_flipped_els,
args.lin_geom,
args.vis_mesh_res,
args.project_to_psd,
args.n_incr_load,
args.output,
args.vtu,
args.log_level,
args.log_file,
args.quiet,
args.export_material_params)
json=args.json,
yaml=args.yaml,
log_level=args.log_level,
strict_validation=args.strict_validation,
max_threads=args.max_threads,
output_dir=args.output_dir
)
52 changes: 33 additions & 19 deletions src/state/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,24 +571,41 @@ void define_solver(py::module_ &m)
s.assemble_rhs();
s.assemble_mass_mat();

s.solve_export_to_file = false;
s.solution_frames.clear();
Eigen::MatrixXd sol, pressure;
s.solve_problem(sol, pressure);
s.solve_export_to_file = true;

s.compute_errors(sol);

s.save_json(sol);
s.export_data(sol, pressure);

return py::make_tuple(sol, pressure);
},
"solve the pde")
.def(
"init_timestepping",
[](State &s, const double t0, const double dt) {
init_globals(s);
s.stats.compute_mesh_stats(*s.mesh);
"build_basis",
[](State &s) {
if (!s.mesh)
throw std::runtime_error("Load mesh first!");

s.build_basis();
},
"build finite element basis")
.def(
"assemble",
[](State &s) {
if (s.bases.size() == 0)
throw std::runtime_error("Call build_basis() first!");

s.assemble_rhs();
s.assemble_mass_mat();
},
"assemble RHS and mass matrix if needed")
.def(
"init_timestepping",
[](State &s, const double t0, const double dt) {
if (!s.solve_data.rhs_assembler || s.mass.size() == 0)
throw std::runtime_error("Call assemble() first!");

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

.def(
"compute_errors",
[](State &s, Eigen::MatrixXd &sol) {
init_globals(s);
// py::scoped_ostream_redirect output;

s.compute_errors(sol);
},
[](State &s, Eigen::MatrixXd &sol) { s.compute_errors(sol); },
"compute the error", py::arg("solution"))

// .def("export_data", [](State &s) {
// py::scoped_ostream_redirect output; s.export_data(); }, "exports all
// data specified in the settings")
.def(
"export_data",
[](State &s, const Eigen::MatrixXd &sol,
const Eigen::MatrixXd &pressure) { s.export_data(sol, pressure); },
"exports all data specified in the settings")
.def(
"export_vtu",
[](State &s, const Eigen::MatrixXd &sol,
const Eigen::MatrixXd &pressure, const double time,
const double dt, std::string &path, bool boundary_only) {
// py::scoped_ostream_redirect output;
s.args["output"]["advanced"]["vis_boundary_only"] = boundary_only;
s.out_geom.save_vtu(
s.resolve_output_path(path), s, sol, pressure, time, dt,
Expand Down Expand Up @@ -1142,7 +1155,8 @@ void define_solve(py::module_ &m)
: load_yaml(yaml_file, in_args);

if (!ok)
log_and_throw_error(fmt::format("unable to open {} file", json_file));
throw std::runtime_error(
fmt::format("unable to open {} file", json_file));

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

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

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

Expand Down
Loading