Skip to content

Commit

Permalink
Merge branch 'trm-bentonite-tests-prep' into 'master'
Browse files Browse the repository at this point in the history
Preparations to add tests of a bentonite material model to OGS

See merge request ogs/ogs!5063
  • Loading branch information
chleh committed Aug 22, 2024
2 parents 24d62b4 + a9635ea commit d6bb24a
Show file tree
Hide file tree
Showing 23 changed files with 118 additions and 91 deletions.
23 changes: 17 additions & 6 deletions Applications/ApplicationsLib/ProjectData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ void readGeometry(std::string const& fname, GeoLib::GEOObjects& geo_objects,
{
DBUG("Reading geometry file '{:s}'.", fname);
GeoLib::IO::BoostXmlGmlInterface gml_reader(geo_objects);
std::string geometry_file = BaseLib::copyPathToFileName(fname, dir_first);
std::string geometry_file = BaseLib::joinPaths(dir_first, fname);
if (!BaseLib::IsFileExisting(geometry_file))
{
// Fallback to reading gml from prj-file directory
geometry_file = BaseLib::copyPathToFileName(fname, dir_second);
geometry_file = BaseLib::joinPaths(dir_second, fname);
WARN("File {:s} not found in {:s}! Trying reading from {:s}.", fname,
dir_first, dir_second);
if (!BaseLib::IsFileExisting(geometry_file))
Expand All @@ -177,16 +177,27 @@ std::unique_ptr<MeshLib::Mesh> readSingleMesh(
BaseLib::ConfigTree const& mesh_config_parameter,
std::string const& directory)
{
std::string const mesh_file = BaseLib::copyPathToFileName(
mesh_config_parameter.getValue<std::string>(), directory);
std::string const mesh_file = BaseLib::joinPaths(
directory, mesh_config_parameter.getValue<std::string>());
DBUG("Reading mesh file '{:s}'.", mesh_file);

auto mesh = std::unique_ptr<MeshLib::Mesh>(MeshLib::IO::readMeshFromFile(
mesh_file, true /* compute_element_neighbors */));
if (!mesh)
{
std::filesystem::path abspath{mesh_file};
try
{
abspath = std::filesystem::absolute(mesh_file);
}
catch (std::filesystem::filesystem_error const& e)
{
ERR("Determining the absolute path of '{}' failed: {}", mesh_file,
e.what());
}

OGS_FATAL("Could not read mesh from '{:s}' file. No mesh added.",
mesh_file);
abspath.string());
}

#ifdef DOXYGEN_DOCU_ONLY
Expand Down Expand Up @@ -369,7 +380,7 @@ ProjectData::ProjectData(BaseLib::ConfigTree const& project_config,
py_path.attr("append")(script_directory); // .prj or -s directory

auto const script_path =
BaseLib::copyPathToFileName(*python_script, script_directory);
BaseLib::joinPaths(script_directory, *python_script);

// Evaluate in scope of main module
py::object scope = py::module::import("__main__").attr("__dict__");
Expand Down
11 changes: 0 additions & 11 deletions BaseLib/FileTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,6 @@ bool hasFileExtension(std::string const& extension, std::string const& filename)
return boost::iequals(extension, getFileExtension(filename));
}

std::string copyPathToFileName(const std::string& file_name,
const std::string& source)
{
auto filePath = std::filesystem::path(file_name);
if (filePath.has_parent_path())
{
return filePath.string();
}
return (std::filesystem::path(source) /= filePath).string();
}

std::string extractPath(std::string const& pathname)
{
return std::filesystem::path(pathname).parent_path().string();
Expand Down
7 changes: 0 additions & 7 deletions BaseLib/FileTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,6 @@ std::string getFileExtension(std::string const& path);
bool hasFileExtension(std::string const& extension,
std::string const& filename);

/**
* Checks if file_name already contains a qualified path and if not copies the
* path from source.
*/
std::string copyPathToFileName(const std::string& file_name,
const std::string& source);

/** Returns a string with file extension as found by getFileExtension()
* dropped.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This setting modifies where the shared library containing the MFront behaviour will be looked for.

The default setting of `true` should work for almost all use cases. `false` is used for some special tests in OGS's test suite.

See also the documentation of the \ref ogs_file_param__material__solid__constitutive_relation__MFront__library "<code>&lt;library&gt;</code>" tag.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Path of the shared library containing the material model.

Relative paths are interpreted relative to the project directory unless \ref ogs_file_attr__material__solid__constitutive_relation__MFront__library__path_is_relative_to_prj_file "<code>path_is_relative_to_prj_file</code>" is set to `false`, in which case the library path/name is used verbatim and will be searched based on the operating system's library lookup rules.

This file was deleted.

4 changes: 2 additions & 2 deletions GeoLib/IO/XmlIO/Qt/XmlStnInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ void XmlStnInterface::readStations(const QDomNode& stationsRoot,
s->setStationValue(stationValue);
if (!sensor_data_file_name.empty())
{
s->addSensorDataFromCSV(BaseLib::copyPathToFileName(
sensor_data_file_name, station_file_name));
s->addSensorDataFromCSV(BaseLib::joinPaths(
station_file_name, sensor_data_file_name));
}
stations.push_back(s);
}
Expand Down
4 changes: 1 addition & 3 deletions MaterialLib/SolidModels/MFront/CreateMFront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ std::unique_ptr<MechanicsBase<DisplacementDim>> createMFront(
local_coordinate_system,
BaseLib::ConfigTree const& config)
{
bool const library_path_is_relative_to_prj_file = true;
auto conf = createMFrontConfig(DisplacementDim, parameters, config,
library_path_is_relative_to_prj_file);
auto conf = createMFrontConfig(DisplacementDim, parameters, config);

return std::make_unique<MFront<DisplacementDim>>(
std::move(conf.behaviour), std::move(conf.material_properties),
Expand Down
28 changes: 18 additions & 10 deletions MaterialLib/SolidModels/MFront/CreateMFrontGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,23 +324,31 @@ namespace MaterialLib::Solids::MFront
MFrontConfig createMFrontConfig(
int const displacement_dim,
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
BaseLib::ConfigTree const& config,
bool const library_path_is_relative_to_prj_file)
BaseLib::ConfigTree const& config)
{
INFO("### MFRONT ########################################################");

//! \ogs_file_param{material__solid__constitutive_relation__type}
config.checkConfigParameter("type", "MFront");

auto const library_name =
//! \ogs_file_param{material__solid__constitutive_relation__MFront__library}
config.getConfigParameterOptional<std::string>("library");
//! \ogs_file_param{material__solid__constitutive_relation__MFront__library}
auto const library = config.getConfigSubtreeOptional("library");

bool const library_path_is_relative_to_prj_file =
library /* If no library tag is specified in the prj file, the lib
shipped with OGS is used, whose path is not relative to the
project file. */
&&
//! \ogs_file_attr{material__solid__constitutive_relation__MFront__library__path_is_relative_to_prj_file}
library->getConfigAttribute("path_is_relative_to_prj_file", true);

std::string const library_name =
library ? library->getValue<std::string>() : "libOgsMFrontBehaviour";

auto const lib_path =
library_name ? (library_path_is_relative_to_prj_file
? BaseLib::joinPaths(BaseLib::getProjectDirectory(),
*library_name)
: *library_name)
: "libOgsMFrontBehaviour";
library_path_is_relative_to_prj_file
? BaseLib::joinPaths(BaseLib::getProjectDirectory(), library_name)
: library_name;

mgis::behaviour::Hypothesis hypothesis;
if (displacement_dim == 2)
Expand Down
9 changes: 3 additions & 6 deletions MaterialLib/SolidModels/MFront/CreateMFrontGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ struct MFrontConfig
MFrontConfig createMFrontConfig(
int const displacement_dim,
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
BaseLib::ConfigTree const& config,
bool const library_path_is_relative_to_prj_file);
BaseLib::ConfigTree const& config);

template <int DisplacementDim, typename Gradients, typename TDynForces,
typename ExtStateVars>
Expand All @@ -37,11 +36,9 @@ createMFrontGeneric(
std::vector<std::unique_ptr<ParameterLib::ParameterBase>> const& parameters,
std::optional<ParameterLib::CoordinateSystem> const&
local_coordinate_system,
BaseLib::ConfigTree const& config,
bool const library_path_is_relative_to_prj_file)
BaseLib::ConfigTree const& config)
{
auto conf = createMFrontConfig(DisplacementDim, parameters, config,
library_path_is_relative_to_prj_file);
auto conf = createMFrontConfig(DisplacementDim, parameters, config);

return std::make_unique<
MFrontGeneric<DisplacementDim, Gradients, TDynForces, ExtStateVars>>(
Expand Down
6 changes: 6 additions & 0 deletions MeshLib/IO/readMeshFromFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ MeshLib::Mesh* readMeshFromFile(const std::string& file_name,
{
std::unique_ptr<Mesh> mesh{
readMeshFromFileSerial(file_name, compute_element_neighbors)};

if (!mesh)
{
return nullptr;
}

return new MeshLib::NodePartitionedMesh(*mesh);
}
return nullptr;
Expand Down
9 changes: 9 additions & 0 deletions ProcessLib/HeatConduction/Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,15 @@ AddTest(
line_1_line_1e2_ts_500_t_39062500.000000_reference.vtu line_1_line_1e2_ts_500_t_39062500.000000.vtu temperature temperature 1e-10 0.0
)

# failing test - mesh not found
AddTest(
NAME 1D_HeatConduction_dirichlet_SourceTerm_fail_mesh_not_found
PATH Parabolic/T/1D_dirichlet_source-term
EXECUTABLE ogs
EXECUTABLE_ARGS line_1_line_1e2_source_term_fail_mesh_not_found.xml
PROPERTIES PASS_REGULAR_EXPRESSION "Could not read mesh from '.*' file[.] No mesh added[.]"
)

AddTest(
NAME HeatConduction_t1_1Dsource
PATH Parabolic/T/t1_1Dsource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ std::unique_ptr<SolidConstitutiveRelation<DisplacementDim>> createMFrontGeneric(
namespace MSM = MaterialLib::Solids::MFront;
using namespace boost::mp11;

bool const library_path_is_relative_to_prj_file = true;

return MSM::createMFrontGeneric<
DisplacementDim, mp_list<MSM::DeformationGradient>,
mp_list<MSM::SecondPiolaKirchhoffStress>, mp_list<MSM::Temperature>>(
parameters, local_coordinate_system, config,
library_path_is_relative_to_prj_file);
parameters, local_coordinate_system, config);
}

template <int DisplacementDim>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ std::unique_ptr<SolidConstitutiveRelation<DisplacementDim>> createMFrontGeneric(
namespace MSM = MaterialLib::Solids::MFront;
using namespace boost::mp11;

bool const library_path_is_relative_to_prj_file = true;

return MSM::createMFrontGeneric<
DisplacementDim, mp_list<MSM::Strain, MSM::LiquidPressure>,
mp_list<MSM::Stress, MSM::Saturation>, mp_list<MSM::Temperature>>(
parameters, local_coordinate_system, config,
library_path_is_relative_to_prj_file);
parameters, local_coordinate_system, config);
}

template <int DisplacementDim>
Expand Down
37 changes: 17 additions & 20 deletions Tests/BaseLib/TestFilePathStringManipulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,27 @@
#include "BaseLib/FileTools.h"

#ifdef WIN32
TEST(BaseLib, CopyPathToFileNameWin)
TEST(BaseLib, JoinPathsWin)
{
ASSERT_EQ("extend\\file", BaseLib::copyPathToFileName("file", "extend"));
ASSERT_EQ("path\\file",
BaseLib::copyPathToFileName("path\\file", "extend"));
ASSERT_EQ("extend\\file", BaseLib::copyPathToFileName("file", "extend\\"));
ASSERT_EQ("path\\file",
BaseLib::copyPathToFileName("path\\file", "extend\\"));
ASSERT_EQ("extend\\smth\\file",
BaseLib::copyPathToFileName("file", "extend\\smth"));
ASSERT_EQ("path\\file",
BaseLib::copyPathToFileName("path\\file", "extend\\smth"));
ASSERT_EQ("extend\\file", BaseLib::joinPaths("extend", "file"));
ASSERT_EQ("extend\\path\\file", BaseLib::joinPaths("extend", "path\\file"));
ASSERT_EQ("extend\\file", BaseLib::joinPaths("extend\\", "file"));
ASSERT_EQ("extend\\path\\file",
BaseLib::joinPaths("extend\\", "path\\file"));
ASSERT_EQ("extend\\smth\\file", BaseLib::joinPaths("extend\\smth", "file"));
ASSERT_EQ("extend\\smth\\path\\file",
BaseLib::joinPaths("extend\\smth", "path\\file"));
}
#else
TEST(BaseLib, CopyPathToFileNameUnix)
TEST(BaseLib, JoinPathsUnix)
{
ASSERT_EQ("extend/file", BaseLib::copyPathToFileName("file", "extend"));
ASSERT_EQ("path/file", BaseLib::copyPathToFileName("path/file", "extend"));
ASSERT_EQ("extend/file", BaseLib::copyPathToFileName("file", "extend/"));
ASSERT_EQ("path/file", BaseLib::copyPathToFileName("path/file", "extend/"));
ASSERT_EQ("extend/file", BaseLib::joinPaths("extend", "file"));
ASSERT_EQ("extend/path/file", BaseLib::joinPaths("extend", "path/file"));
ASSERT_EQ("extend/file", BaseLib::joinPaths("extend/", "file"));
ASSERT_EQ("extend/path/file", BaseLib::joinPaths("extend/", "path/file"));

ASSERT_EQ("extend/smth/file",
BaseLib::copyPathToFileName("file", "extend/smth"));
ASSERT_EQ("path/file",
BaseLib::copyPathToFileName("path/file", "extend/smth"));
ASSERT_EQ("extend/smth/file", BaseLib::joinPaths("extend/smth", "file"));
ASSERT_EQ("extend/smth/path/file",
BaseLib::joinPaths("extend/smth", "path/file"));
}
#endif
4 changes: 3 additions & 1 deletion Tests/Data/Mechanics/Linear/SimpleMechanics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@
")\n",
"try:\n",
" model.write_input()\n",
" model.run_model(logfile=(out_dir / f\"{prj_name}.txt\"), args=f\"-o {out_dir}\")\n",
" model.run_model(\n",
" logfile=(out_dir / f\"{prj_name}-out.txt\"), args=f\"-o {out_dir} -m .\"\n",
" )\n",
"except Exception as inst:\n",
" print(f\"{type(inst)}: {inst.args[0]}\")\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="ISO-8859-1"?>
<OpenGeoSysProjectDiff base_file="line_1_line_1e2_source_term.prj">
<replace msel="/*/meshes/mesh[1]/text()">
NON_EXISTENT_MESH.vtu
</replace>
</OpenGeoSysProjectDiff>

Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
"metadata": {},
"outputs": [],
"source": [
"data_dir = os.environ.get(\"OGS_DATA_DIR\", \"../../..\")\n",
"\n",
"from pathlib import Path\n",
"\n",
"out_dir = Path(os.environ.get(\"OGS_TESTRUNNER_OUT_DIR\", \"_out\"))\n",
Expand Down Expand Up @@ -147,7 +145,9 @@
" print(\n",
" f\"> Running single edge notched shear test {phasefield_model} - {energy_split_model} ... <\"\n",
" )\n",
" logfile = f\"{out_dir}/log_{phasefield_model}_{energy_split_model}.txt\" # noqa: F841\n",
" # fmt: off\n",
" logfile = f\"{out_dir}/log_{phasefield_model}_{energy_split_model}_out.txt\" # noqa: F841\n",
" # fmt: on\n",
" model = ogs.OGS(INPUT_FILE=prj_name, PROJECT_FILE=f\"{out_dir}/{prj_name}\", MKL=True)\n",
"\n",
" # generate prefix from properties\n",
Expand Down Expand Up @@ -201,16 +201,18 @@
" xpath=\"./linear_solvers/linear_solver/petsc/parameters\",\n",
" occurrence=1,\n",
" )\n",
" model.replace_text(\"./shear.gml\", xpath=\"./geometry\")\n",
" model.replace_text(Path(\"./shear.gml\").resolve(), xpath=\"./geometry\")\n",
" model.write_input()\n",
" # run ogs\n",
" t0 = time.time()\n",
" if MPI:\n",
" print(f\" > OGS started execution with MPI - {ncores} cores...\")\n",
" ! mpirun --bind-to none -np {ncores} ogs {out_dir}/{prj_name} -o {output_dir} >> {logfile}\n",
" assert _exit_code == 0 # noqa: F821\n",
" else:\n",
" print(\" > OGS started execution - \")\n",
" ! ogs {out_dir}/{prj_name} -o {output_dir} >> {logfile}\n",
" assert _exit_code == 0 # noqa: F821\n",
" tf = time.time()\n",
" print(\" > OGS terminated execution. Elapsed time: \", round(tf - t0, 2), \" s.\")"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,19 @@
" MKL=True,\n",
" args=f\"-o {out_dir}\",\n",
" )\n",
"\n",
" gml_file = Path(\"./Kregime_Static.gml\").resolve()\n",
"\n",
" model.replace_parameter_value(name=\"ls\", value=ls)\n",
" model.replace_text(\"./Kregime_Static.gml\", xpath=\"./geometry\")\n",
" model.replace_text(gml_file, xpath=\"./geometry\")\n",
" model.replace_text(filename, xpath=\"./time_loop/output/prefix\")\n",
" model.write_input()\n",
" # run simulation with ogs\n",
" t0 = time.time()\n",
" print(\">>> OGS started execution ... <<<\")\n",
" !ogs {out_dir}/{prj_name} -o {out_dir} > {out_dir}/log.txt\n",
"\n",
" !ogs {out_dir}/{prj_name} -o {out_dir} -m {out_dir} > {out_dir}/ogs-out.txt\n",
" assert _exit_code == 0 # noqa: F821\n",
" tf = time.time()\n",
" print(\">>> OGS terminated execution <<< Elapsed time: \", round(tf - t0, 2), \" s.\")"
]
Expand Down
Loading

0 comments on commit d6bb24a

Please sign in to comment.