15using namespace solver;
17bool has_arg(
const CLI::App &command_line,
const std::string &value)
19 const auto *opt = command_line.get_option_no_throw(value.size() == 1 ? (
"-" + value) : (
"--" + value));
23 return opt->count() > 0;
28 std::ifstream file(json_file);
35 if (!out.contains(
"root_path"))
36 out[
"root_path"] = json_file;
46 if (!out.contains(
"root_path"))
47 out[
"root_path"] = yaml_file;
57 const std::string &hdf5_file,
58 const std::string output_dir,
59 const unsigned max_threads,
61 const bool fallback_solver,
62 const spdlog::level::level_enum &log_level,
66 const unsigned max_threads,
68 const spdlog::level::level_enum &log_level,
71int main(
int argc,
char **argv)
75 CLI::App command_line{
"polyfem"};
77 command_line.ignore_case();
78 command_line.ignore_underscore();
81 unsigned max_threads = std::numeric_limits<unsigned>::max();
82 command_line.add_option(
"--max_threads", max_threads,
"Maximum number of threads");
84 auto input = command_line.add_option_group(
"input");
86 std::string json_file =
"";
87 input->add_option(
"-j,--json", json_file,
"Simulation JSON file")->check(CLI::ExistingFile);
89 std::string yaml_file =
"";
90 input->add_option(
"-y,--yaml", yaml_file,
"Simulation YAML file")->check(CLI::ExistingFile);
92 std::string hdf5_file =
"";
93 input->add_option(
"--hdf5", hdf5_file,
"Simulation HDF5 file")->check(CLI::ExistingFile);
95 input->require_option(1);
97 std::string output_dir =
"";
98 command_line.add_option(
"-o,--output_dir", output_dir,
"Directory for output files")->check(CLI::ExistingDirectory | CLI::NonexistentPath);
100 bool is_strict =
true;
101 command_line.add_flag(
"-s,--strict_validation,!--ns,!--no_strict_validation", is_strict,
"Disables strict validation of input JSON");
103 bool fallback_solver =
false;
104 command_line.add_flag(
"--enable_overwrite_solver", fallback_solver,
"If solver in input is not present, falls back to default.");
106 const std::vector<std::pair<std::string, spdlog::level::level_enum>>
107 SPDLOG_LEVEL_NAMES_TO_LEVELS = {
108 {
"trace", spdlog::level::trace},
109 {
"debug", spdlog::level::debug},
110 {
"info", spdlog::level::info},
111 {
"warning", spdlog::level::warn},
112 {
"error", spdlog::level::err},
113 {
"critical", spdlog::level::critical},
114 {
"off", spdlog::level::off}};
115 spdlog::level::level_enum log_level = spdlog::level::debug;
116 command_line.add_option(
"--log_level", log_level,
"Log level")
117 ->transform(CLI::CheckedTransformer(SPDLOG_LEVEL_NAMES_TO_LEVELS, CLI::ignore_case));
119 CLI11_PARSE(command_line, argc, argv);
123 if (!json_file.empty() || !yaml_file.empty())
125 const bool ok = !json_file.empty() ?
load_json(json_file, in_args) :
load_yaml(yaml_file, in_args);
130 if (in_args.contains(
"states"))
134 is_strict, fallback_solver, log_level, in_args);
138 is_strict, fallback_solver, log_level, in_args);
142 const std::string &hdf5_file,
143 const std::string output_dir,
144 const unsigned max_threads,
145 const bool is_strict,
146 const bool fallback_solver,
147 const spdlog::level::level_enum &log_level,
150 std::vector<std::string> names;
151 std::vector<Eigen::MatrixXi> cells;
152 std::vector<Eigen::MatrixXd> vertices;
154 if (in_args.empty() && hdf5_file.empty())
156 logger().error(
"No input file specified!");
157 return command_line.exit(CLI::RequiredError(
"--json or --hdf5"));
160 if (in_args.empty() && !hdf5_file.empty())
162 using MatrixXl = Eigen::Matrix<int64_t, Eigen::Dynamic, Eigen::Dynamic>;
164 h5pp::File file(hdf5_file, h5pp::FileAccess::READONLY);
165 std::string json_string = file.readDataset<std::string>(
"json");
167 in_args = json::parse(json_string);
168 in_args[
"root_path"] = hdf5_file;
170 names = file.findGroups(
"",
"/meshes");
171 cells.resize(names.size());
172 vertices.resize(names.size());
174 for (
int i = 0; i < names.size(); ++i)
176 const std::string &name = names[i];
177 cells[i] = file.readDataset<MatrixXl>(
"/meshes/" + name +
"/c").cast<int>();
178 vertices[i] = file.readDataset<Eigen::MatrixXd>(
"/meshes/" + name +
"/v");
182 json tmp = json::object();
183 if (
has_arg(command_line,
"log_level"))
184 tmp[
"/output/log/level"_json_pointer] = int(log_level);
185 if (
has_arg(command_line,
"max_threads"))
186 tmp[
"/solver/max_threads"_json_pointer] = max_threads;
187 if (
has_arg(command_line,
"output_dir"))
188 tmp[
"/output/directory"_json_pointer] = std::filesystem::absolute(output_dir);
189 if (
has_arg(command_line,
"enable_overwrite_solver"))
190 tmp[
"/solver/linear/enable_overwrite_solver"_json_pointer] = fallback_solver;
191 assert(tmp.is_object());
192 in_args.merge_patch(tmp);
195 state.
init(in_args, is_strict);
196 state.
load_mesh(
false, names, cells, vertices);
199 if (state.
mesh ==
nullptr)
213 Eigen::MatrixXd pressure;
228 const unsigned max_threads,
229 const bool is_strict,
230 const spdlog::level::level_enum &log_level,
233 json tmp = json::object();
234 if (
has_arg(command_line,
"log_level"))
235 tmp[
"/output/log/level"_json_pointer] = int(log_level);
236 if (
has_arg(command_line,
"max_threads"))
237 tmp[
"/solver/max_threads"_json_pointer] = max_threads;
238 opt_args.merge_patch(tmp);
241 opt_state.
init(opt_args, is_strict);
250 if (opt_state.
args[
"compute_objective"].get<
bool>())
252 logger().info(
"Objective is {}", opt_state.
eval(
x));
main class that contains the polyfem adjoint solver and all its state
void solve(Eigen::VectorXd &x)
void initial_guess(Eigen::VectorXd &x)
json args
main input arguments containing all defaults
void init(const json &args, const bool strict_validation)
initialize the polyfem solver with a json settings
double eval(Eigen::VectorXd &x) const
void create_states(const polyfem::solver::CacheLevel level, const int max_threads=-1)
create the opt states
void init_variables()
init variables
main class that contains the polyfem solver and all its state
io::OutRuntimeData timings
runtime statistics
void init(const json &args, const bool strict_validation)
initialize the polyfem solver with a json settings
void assemble_mass_mat()
assemble mass, step 4 of solve build mass matrix based on defined basis modifies mass (and maybe more...
void compute_errors(const Eigen::MatrixXd &sol)
computes all errors
void load_mesh(bool non_conforming=false, const std::vector< std::string > &names=std::vector< std::string >(), const std::vector< Eigen::MatrixXi > &cells=std::vector< Eigen::MatrixXi >(), const std::vector< Eigen::MatrixXd > &vertices=std::vector< Eigen::MatrixXd >())
loads the mesh from the json arguments
void save_json(const Eigen::MatrixXd &sol, std::ostream &out)
saves the output statistic to a stream
std::unique_ptr< mesh::Mesh > mesh
current mesh, it can be a Mesh2D or Mesh3D
void solve_problem(Eigen::MatrixXd &sol, Eigen::MatrixXd &pressure)
solves the problems
void build_basis()
builds the bases step 2 of solve modifies bases, pressure_bases, geom_bases_, boundary_nodes,...
io::OutStatsData stats
Other statistics.
void assemble_rhs()
compute rhs, step 3 of solve build rhs vector based on defined basis and given rhs of the problem mod...
void export_data(const Eigen::MatrixXd &sol, const Eigen::MatrixXd &pressure)
saves all data on the disk according to the input params
double total_time()
computes total time
void compute_mesh_stats(const polyfem::mesh::Mesh &mesh)
compute stats (counts els type, mesh lenght, etc), step 1 of solve
bool load_json(const std::string &json_file, json &out)
int main(int argc, char **argv)
int optimization_simulation(const CLI::App &command_line, const unsigned max_threads, const bool is_strict, const spdlog::level::level_enum &log_level, json &opt_args)
bool load_yaml(const std::string &yaml_file, json &out)
bool has_arg(const CLI::App &command_line, const std::string &value)
int forward_simulation(const CLI::App &command_line, const std::string &hdf5_file, const std::string output_dir, const unsigned max_threads, const bool is_strict, const bool fallback_solver, const spdlog::level::level_enum &log_level, json &in_args)
json yaml_file_to_json(const std::string &yaml_filepath)
Load a YAML file to JSON.
spdlog::logger & logger()
Retrieves the current logger.
void log_and_throw_error(const std::string &msg)