Skip to content
Snippets Groups Projects
Commit 82b4ed19 authored by Mark Meredith's avatar Mark Meredith
Browse files

Better messages

parent f435375f
No related branches found
No related tags found
1 merge request!21Provide better error messages for parsing inputs files
Pipeline #21239 passed
#include <iostream>
#include "solver_impl.hpp" #include "solver_impl.hpp"
namespace solver { namespace solver {
std::optional<solver::GeometrySettings> make_geometry(solver::InputInfo ii) { solver::GeometrySettings make_geometry(solver::InputInfo ii,
std::vector<InputsMessage> messages) {
solver::GeometrySettings geo; solver::GeometrySettings geo;
if (ii.count(CSG_FILENAME)) { if (ii.count(CSG_FILENAME)) {
auto csg_filename = std::get<solver::StringArray>(ii[CSG_FILENAME]); auto csg_filename = std::get<solver::StringArray>(ii[CSG_FILENAME]);
if (csg_filename.size() != 1) { if (csg_filename.size() != 1) {
std::cout << CSG_FILENAME << " should only have 1 element" << std::endl; messages.push_back(
return std::nullopt; InputsMessage{"geometry.filename should only have 1 element"});
} }
} else { } else {
geo.csg_filename = ""; geo.csg_filename = "";
} }
if (!ii.count(PROB_LO)) { if (!ii.count(PROB_LO)) {
require(PROB_LO); require(PROB_LO, messages, 0);
return std::nullopt;
} }
if (!ii.count(PROB_HI)) { if (!ii.count(PROB_HI)) {
require(PROB_HI); require(PROB_HI, messages, 0);
return std::nullopt;
} }
if (!ii.count(PERIODIC)) { if (!ii.count(PERIODIC)) {
require(PERIODIC); require(PERIODIC, messages, 0);
return std::nullopt;
} }
auto lows = std::get<solver::NumberArray>(ii[PROB_LO]); auto lows = std::get<solver::NumberArray>(ii[PROB_LO]);
auto highs = std::get<solver::NumberArray>(ii[PROB_HI]); auto highs = std::get<solver::NumberArray>(ii[PROB_HI]);
auto is_periodic = std::get<solver::NumberArray>(ii[PERIODIC]); auto is_periodic = std::get<solver::NumberArray>(ii[PERIODIC]);
if (lows.size() != 3) { if (lows.size() != 3) {
std::cout << "prob_lo needs 3 elements " << std::endl; require(PROB_LO, messages, 3);
return std::nullopt; } else {
std::get<0>(geo.axes).low = lows[0];
std::get<1>(geo.axes).low = lows[1];
std::get<2>(geo.axes).low = lows[2];
} }
if (highs.size() != 3) { if (highs.size() != 3) {
std::cout << "prob_hi needs 3 elements " << std::endl; require(PROB_HI, messages, 3);
return std::nullopt; } else {
std::get<0>(geo.axes).high = highs[0];
std::get<1>(geo.axes).high = highs[1];
std::get<2>(geo.axes).high = highs[2];
} }
if (is_periodic.size() != 3) { if (is_periodic.size() != 3) {
std::cout << "periodic needs 3 elements " << std::endl; require(PERIODIC, messages, 3);
return std::nullopt; } else {
std::get<0>(geo.axes).periodic = is_periodic[0];
std::get<1>(geo.axes).periodic = is_periodic[1];
std::get<2>(geo.axes).periodic = is_periodic[2];
} }
std::get<0>(geo.axes).high = highs[0];
std::get<0>(geo.axes).low = lows[0];
std::get<0>(geo.axes).periodic = is_periodic[0];
std::get<1>(geo.axes).high = highs[1];
std::get<1>(geo.axes).low = lows[1];
std::get<1>(geo.axes).periodic = is_periodic[1];
std::get<2>(geo.axes).high = highs[2];
std::get<2>(geo.axes).low = lows[2];
std::get<2>(geo.axes).periodic = is_periodic[2];
return geo; return geo;
} }
} // namespace solver } // namespace solver
#include <iostream>
#include "solver_impl.hpp" #include "solver_impl.hpp"
namespace solver { namespace solver {
std::optional<solver::MeshSettings> make_mesh(solver::InputInfo ii) { solver::MeshSettings make_mesh(solver::InputInfo ii,
std::vector<InputsMessage> messages) {
solver::MeshSettings mesh; solver::MeshSettings mesh;
auto [mx, my, mz] = mesh.axes;
if (!ii.count(N_CELL)) { if (!ii.count(N_CELL)) {
require(N_CELL); require(N_CELL, messages, 0);
return std::nullopt; } else {
}
auto n_cell = std::get<solver::NumberArray>(ii[N_CELL]);
if (n_cell.size() != 3) {
std::cout << N_CELL << " needs 3 elements " << std::endl;
return std::nullopt;
}
auto [mx, my, mz] = mesh.axes; auto n_cell = std::get<solver::NumberArray>(ii[N_CELL]);
mx.n_cell = n_cell[0]; if (n_cell.size() != 3) {
my.n_cell = n_cell[1]; require(N_CELL, messages, 3);
mz.n_cell = n_cell[2]; } else {
mx.n_cell = n_cell[0];
my.n_cell = n_cell[1];
mz.n_cell = n_cell[2];
}
}
auto fabarray_size = std::get<solver::NumberArray>(ii[FABARRAY_TILE_SZ]); auto fabarray_size = std::get<solver::NumberArray>(ii[FABARRAY_TILE_SZ]);
if (fabarray_size.size() != 3) { if (fabarray_size.size() != 3) {
std::cout << FABARRAY_TILE_SZ << " needs 3 elements " << std::endl; require(FABARRAY_TILE_SZ, messages, 3);
} else { } else {
mx.fluid_max_tile_size = fabarray_size[0]; mx.fluid_max_tile_size = fabarray_size[0];
my.fluid_max_tile_size = fabarray_size[1]; my.fluid_max_tile_size = fabarray_size[1];
...@@ -32,7 +32,7 @@ std::optional<solver::MeshSettings> make_mesh(solver::InputInfo ii) { ...@@ -32,7 +32,7 @@ std::optional<solver::MeshSettings> make_mesh(solver::InputInfo ii) {
auto part_grid_size = std::get<solver::NumberArray>(ii[PARTICLE_TILE_SZ]); auto part_grid_size = std::get<solver::NumberArray>(ii[PARTICLE_TILE_SZ]);
if (part_grid_size.size() != 3) { if (part_grid_size.size() != 3) {
std::cout << PARTICLE_TILE_SZ << " needs 3 elements " << std::endl; require(PARTICLE_TILE_SZ, messages, 3);
} else { } else {
mx.particle_max_tile_size = part_grid_size[0]; mx.particle_max_tile_size = part_grid_size[0];
my.particle_max_tile_size = part_grid_size[1]; my.particle_max_tile_size = part_grid_size[1];
...@@ -41,19 +41,19 @@ std::optional<solver::MeshSettings> make_mesh(solver::InputInfo ii) { ...@@ -41,19 +41,19 @@ std::optional<solver::MeshSettings> make_mesh(solver::InputInfo ii) {
auto grid_size_x = std::get<solver::NumberArray>(ii[GRID_SIZE_X]); auto grid_size_x = std::get<solver::NumberArray>(ii[GRID_SIZE_X]);
if (grid_size_x.size() != 1) { if (grid_size_x.size() != 1) {
std::cout << GRID_SIZE_X << " is a scalar " << std::endl; require(GRID_SIZE_X, messages, 1);
} else { } else {
mx.max_grid_size = grid_size_x[0]; mx.max_grid_size = grid_size_x[0];
} }
auto grid_size_y = std::get<solver::NumberArray>(ii[GRID_SIZE_Y]); auto grid_size_y = std::get<solver::NumberArray>(ii[GRID_SIZE_Y]);
if (grid_size_y.size() != 1) { if (grid_size_y.size() != 1) {
std::cout << GRID_SIZE_Y << " is a scalar " << std::endl; require(GRID_SIZE_Y, messages, 1);
} else { } else {
my.max_grid_size = grid_size_y[0]; my.max_grid_size = grid_size_y[0];
} }
auto grid_size_z = std::get<solver::NumberArray>(ii[GRID_SIZE_Z]); auto grid_size_z = std::get<solver::NumberArray>(ii[GRID_SIZE_Z]);
if (grid_size_z.size() != 1) { if (grid_size_z.size() != 1) {
std::cout << GRID_SIZE_Z << " is a scalar " << std::endl; require(GRID_SIZE_Z, messages, 1);
} else { } else {
mz.max_grid_size = grid_size_z[0]; mz.max_grid_size = grid_size_z[0];
} }
...@@ -61,35 +61,35 @@ std::optional<solver::MeshSettings> make_mesh(solver::InputInfo ii) { ...@@ -61,35 +61,35 @@ std::optional<solver::MeshSettings> make_mesh(solver::InputInfo ii) {
auto particle_grid_size_x = auto particle_grid_size_x =
std::get<solver::NumberArray>(ii[PARTICLE_GRID_SIZE_X]); std::get<solver::NumberArray>(ii[PARTICLE_GRID_SIZE_X]);
if (particle_grid_size_x.size() != 1) { if (particle_grid_size_x.size() != 1) {
std::cout << PARTICLE_GRID_SIZE_X << " is a scalar " << std::endl; require(PARTICLE_GRID_SIZE_X, messages, 1);
} else { } else {
mx.particle_max_grid_size = particle_grid_size_x[0]; mx.particle_max_grid_size = particle_grid_size_x[0];
} }
auto particle_grid_size_y = auto particle_grid_size_y =
std::get<solver::NumberArray>(ii[PARTICLE_GRID_SIZE_Y]); std::get<solver::NumberArray>(ii[PARTICLE_GRID_SIZE_Y]);
if (particle_grid_size_y.size() != 1) { if (particle_grid_size_y.size() != 1) {
std::cout << PARTICLE_GRID_SIZE_Y << " is a scalar " << std::endl; require(PARTICLE_GRID_SIZE_Y, messages, 1);
} else { } else {
my.particle_max_grid_size = particle_grid_size_y[0]; my.particle_max_grid_size = particle_grid_size_y[0];
} }
auto particle_grid_size_z = auto particle_grid_size_z =
std::get<solver::NumberArray>(ii[PARTICLE_GRID_SIZE_Z]); std::get<solver::NumberArray>(ii[PARTICLE_GRID_SIZE_Z]);
if (particle_grid_size_z.size() != 1) { if (particle_grid_size_z.size() != 1) {
std::cout << PARTICLE_GRID_SIZE_Z << " is a scalar " << std::endl; require(PARTICLE_GRID_SIZE_Z, messages, 1);
} else { } else {
mz.particle_max_grid_size = particle_grid_size_z[0]; mz.particle_max_grid_size = particle_grid_size_z[0];
} }
auto bf = std::get<solver::NumberArray>(ii[BLOCKING_FACTOR]); auto bf = std::get<solver::NumberArray>(ii[BLOCKING_FACTOR]);
if (bf.size() != 1) { if (bf.size() != 1) {
std::cout << BLOCKING_FACTOR << " is a scalar" << std::endl; require(BLOCKING_FACTOR, messages, 1);
} else { } else {
mesh.blocking_factor = bf[0]; mesh.blocking_factor = bf[0];
} }
auto volfrac = std::get<solver::NumberArray>(ii[SMALL_VOLFRAC]); auto volfrac = std::get<solver::NumberArray>(ii[SMALL_VOLFRAC]);
if (volfrac.size() != 1) { if (volfrac.size() != 1) {
std::cout << SMALL_VOLFRAC << " is a scalar" << std::endl; require(SMALL_VOLFRAC, messages, 1);
} else { } else {
mesh.small_volfrac = volfrac[0]; mesh.small_volfrac = volfrac[0];
} }
......
#include <iostream>
#include <sstream> #include <sstream>
#include "solver_impl.hpp" #include "solver_impl.hpp"
namespace solver { namespace solver {
void require(std::string key) { void require(std::string key, std::vector<InputsMessage> messages, int size) {
std::cout << "missing required key: " << key << std::endl; std::ostringstream oss;
if (size == 0) {
oss << "missing required key: " << key;
} else if (size == 1) {
oss << key << " is a scalar";
} else if (size == 1) {
oss << key << " requires " << size << " elements";
}
messages.push_back(InputsMessage{oss.str()});
} }
std::pair<solver::SolverSettings, std::vector<std::string>> std::pair<solver::SolverSettings, std::vector<InputsMessage>>
make_solver(solver::InputInfo ii) { make_solver(solver::InputInfo ii) {
std::vector<std::string> messages; std::vector<InputsMessage> messages;
solver::SolverSettings ss; solver::SolverSettings ss;
auto new_geo = make_geometry(ii); ss.geometry = make_geometry(ii, messages);
if (!new_geo.has_value()) { ss.mesh = make_mesh(ii, messages);
messages.push_back("Problem making geometry"); ss.time = make_time(ii, messages);
}
auto new_mesh = make_mesh(ii);
if (!new_mesh.has_value()) {
messages.push_back("Problem making mesh");
}
auto new_time = make_time(ii);
if (!new_time.has_value()) {
messages.push_back("Problem making time");
}
ss.geometry = new_geo.value();
ss.time = new_time.value();
ss.mesh = new_mesh.value();
return std::make_pair(ss, messages); return std::make_pair(ss, messages);
} }
......
...@@ -57,11 +57,13 @@ struct SolverSettings { ...@@ -57,11 +57,13 @@ struct SolverSettings {
TimeSettings time; TimeSettings time;
}; };
std::pair<solver::SolverSettings, std::vector<std::string>> struct InputsMessage {
std::string message;
};
std::pair<solver::SolverSettings, std::vector<InputsMessage>>
make_solver(solver::InputInfo); make_solver(solver::InputInfo);
std::string serialize(solver::SolverSettings); std::string serialize(solver::SolverSettings);
void require(std::string);
} // namespace solver } // namespace solver
#endif #endif
#include <solver.hpp> #include <solver.hpp>
namespace solver { namespace solver {
std::optional<GeometrySettings> make_geometry(solver::InputInfo); GeometrySettings make_geometry(solver::InputInfo,
std::optional<MeshSettings> make_mesh(solver::InputInfo); std::vector<InputsMessage> messages);
std::optional<TimeSettings> make_time(solver::InputInfo); MeshSettings make_mesh(solver::InputInfo, std::vector<InputsMessage> messages);
TimeSettings make_time(solver::InputInfo, std::vector<InputsMessage> messages);
void require(std::string, std::vector<InputsMessage>, int);
} // namespace solver } // namespace solver
#include <iostream>
#include "solver_impl.hpp" #include "solver_impl.hpp"
namespace solver { namespace solver {
std::optional<solver::TimeSettings> make_time(solver::InputInfo ii) { solver::TimeSettings make_time(solver::InputInfo ii,
std::vector<InputsMessage> messages) {
solver::TimeSettings time; solver::TimeSettings time;
auto dt_max = std::get<solver::NumberArray>(ii[DT_MAX]); auto dt_max = std::get<solver::NumberArray>(ii[DT_MAX]);
if (dt_max.size() != 1) { if (dt_max.size() != 1) {
std::cout << "dt_max is a scalar: " << dt_max.size() << std::endl; require(DT_MAX, messages, 1);
} else { } else {
time.dt_max = dt_max[0]; time.dt_max = dt_max[0];
} }
auto dt_min = std::get<solver::NumberArray>(ii[DT_MIN]); auto dt_min = std::get<solver::NumberArray>(ii[DT_MIN]);
if (dt_min.size() != 1) { if (dt_min.size() != 1) {
std::cout << "dt_min is a scalar" << std::endl; require(DT_MIN, messages, 1);
} else { } else {
time.dt_min = dt_min[0]; time.dt_min = dt_min[0];
} }
auto maxstep = std::get<solver::NumberArray>(ii[MAXSTEP]); auto maxstep = std::get<solver::NumberArray>(ii[MAXSTEP]);
if (maxstep.size() != 1) { if (maxstep.size() != 1) {
std::cout << "max_step is a scalar" << std::endl; require(MAXSTEP, messages, 1);
} else { } else {
time.max_step = maxstep[0]; time.max_step = maxstep[0];
} }
if (!ii.count(TSTOP)) { if (!ii.count(TSTOP)) {
require(TSTOP); require(TSTOP, messages, 0);
return std::nullopt;
} }
auto tstop = std::get<solver::NumberArray>(ii[TSTOP]); auto tstop = std::get<solver::NumberArray>(ii[TSTOP]);
if (tstop.size() != 1) { if (tstop.size() != 1) {
std::cout << "tstop is a scalar" << std::endl; require(TSTOP, messages, 1);
} else { } else {
time.tstop = tstop[0]; time.tstop = tstop[0];
} }
auto fixed_dt = std::get<solver::NumberArray>(ii[FIXED_DT]); auto fixed_dt = std::get<solver::NumberArray>(ii[FIXED_DT]);
if (fixed_dt.size() != 1) { if (fixed_dt.size() != 1) {
std::cout << "fixed_dt is a scalar" << std::endl; require(FIXED_DT, messages, 1);
} else { } else {
time.fixed_dt = (fixed_dt[0] != 0); time.fixed_dt = (fixed_dt[0] != 0);
} }
auto cfl = std::get<solver::NumberArray>(ii[CFL]); auto cfl = std::get<solver::NumberArray>(ii[CFL]);
if (cfl.size() != 1) { if (cfl.size() != 1) {
std::cout << "cfl is a scalar" << std::endl; require(CFL, messages, 1);
} else { } else {
time.cfl = cfl[0]; time.cfl = cfl[0];
} }
auto tcoll_ratio = std::get<solver::NumberArray>(ii[TCOLL_RATIO]); auto tcoll_ratio = std::get<solver::NumberArray>(ii[TCOLL_RATIO]);
if (tcoll_ratio.size() != 1) { if (tcoll_ratio.size() != 1) {
std::cout << "tcoll_ratio is a scalar" << std::endl; require(TCOLL_RATIO, messages, 1);
} else { } else {
time.tcoll_ratio = tcoll_ratio[0]; time.tcoll_ratio = tcoll_ratio[0];
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment