Page MenuHomec4science

hdf5_unsaturated.cpp
No OneTemporary

File Metadata

Created
Mon, Jan 13, 12:40

hdf5_unsaturated.cpp

/* =============================================================================
Copyright (c) 2014 - 2016
F. Georget <fabieng@princeton.edu> Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
============================================================================= */
#include "hdf5_unsaturated.hpp"
#include "../systems/unsaturated/variables.hpp"
#include <H5Cpp.h>
#include "../../utils/io/specmicp_hdf5.hpp"
#include "../../utils/io/hdf5_eigen.hpp"
#include "../../utils/io/hdf5_timesteps.hpp"
#include "../../specmicp/io/hdf5_adimensional.hpp"
#include "../../dfpm/io/hdf5_mesh.hpp"
#include "../../database/io/hdf5_database.hpp"
#include "../../physics/units.hpp"
#include "../../database/data_container.hpp"
#include "../../utils/compat.hpp"
#include <functional>
#include <iostream>
using namespace specmicp::reactmicp::systems::unsaturated;
#define MESH_GRPNAME "mesh"
#define DATABASE_GRPNAME "database"
#define UNIT_ATTRIBUTE "unit"
#define MAIN_VARIABLES_GRPNAME "main_variables"
#define CHEMISTRY_SOLUTION_GRPNAME "chemistry_solution"
#define TRANSPORT_PROPERTIES_GRPNAME "transport_properties"
#define LIQUID_SATURATION_DSET "liquid_saturation"
#define AQUEOUS_CONC_DSET "aqueous_concentration"
#define SOLID_CONC_DSET "solid_concentration"
#define PRESSURE_DSET "partial_pressure"
#define CAP_PRESSURE_DSET "capillary_pressure"
#define POROSITY_DSET "porosity"
#define LIQUID_DIFFUSIVITY_DSET "liquid_diffusivity"
#define REL_LIQUID_DIFFUSIVITY_DSET "relative_liquid_diffusivity"
#define LIQUID_PERMEABILITY_DSET "liquid_permeability"
#define REL_LIQUID_PERMEABILITY_DSET "relative_liquid_permeability"
#define RESISTANCE_GAS_DIFFUSIVITY_DSET "resistance_gas_diffusivity"
#define REL_GAS_DIFFUSIVITY_DSET "relative_gas_diffusivity"
namespace specmicp {
namespace io {
//! \brief Implementation of the UnsaturatedHDF5Saver
//!
//! \internal
struct SPECMICP_DLL_LOCAL UnsaturatedHDF5Saver::UnsaturatedHDF5SaverImpl
{
public:
std::string m_filepath;
std::shared_ptr<UnsaturatedVariables> m_vars; //!< the variables
database::RawDatabasePtr m_database; //!< the database
UnsaturatedHDF5SaverImpl(
const std::string& filename,
std::shared_ptr<UnsaturatedVariables> vars,
const units::UnitsSet the_units):
m_filepath(filename),
m_vars(vars),
m_database(vars->get_database())
{
init_file(the_units);
}
//! \brief Save a timestep
void save_timestep(
const std::string& name,
const std::string& section
);
//! \brief Save the main variables
//!
//! \param file HDF5 file
//! \param section section where to save the main variables
void save_main_variables(
io::HDF5File& file,
const std::string& section
);
//! \brief Save the chemical solution
//!
//! \param file HDF5 file
//! \param section section where to save the chemical solutions
void save_chemical_solution(
io::HDF5File& file,
const std::string& section
);
//! \brief Save the transport properties
//!
//! \param file HDF5 file
//! \param section section where to save the transport properties
void save_transport_properties(
io::HDF5File& file,
const std::string& section
);
void init_file(const units::UnitsSet& the_units);
};
struct SPECMICP_DLL_LOCAL UnsaturatedHDF5Reader::UnsaturatedHDF5ReaderImpl
{
public:
UnsaturatedHDF5ReaderImpl(const std::string& name):
m_file(name, HDF5_OpenMode::OpenReadOnly),
m_timesteps(m_file)
{
}
void initialize_variables(
scalar_t timestep,
reactmicp::systems::unsaturated::UnsaturatedVariables* vars
);
void read_main_variables(
const H5::Group& timestep_group,
reactmicp::systems::unsaturated::UnsaturatedVariables* vars
);
void read_chemical_solutions(
const H5::Group& timestep_group,
reactmicp::systems::unsaturated::UnsaturatedVariables* vars
);
void read_transport_properties(
const H5::Group& timestep_group,
reactmicp::systems::unsaturated::UnsaturatedVariables* vars
);
private:
HDF5File m_file;
HDF5Timesteps m_timesteps;
};
//
UnsaturatedHDF5Saver::UnsaturatedHDF5Saver(
const std::string& filename,
std::shared_ptr<UnsaturatedVariables> vars,
const units::UnitsSet& the_units
):
m_impl(make_unique<UnsaturatedHDF5SaverImpl>(filename, vars, the_units))
{}
UnsaturatedHDF5Saver::~UnsaturatedHDF5Saver() = default;
void UnsaturatedHDF5Saver::save_timestep(scalar_t timestep)
{
return m_impl->save_timestep(std::to_string(timestep), "/");
}
//! \brief Initialize variables from a saved timestep
//!
//! The Mesh and the database must already be ok
void UnsaturatedHDF5Reader::initialize_variables(
scalar_t timestep,
reactmicp::systems::unsaturated::UnsaturatedVariables* vars
)
{
return m_impl->initialize_variables(timestep, vars);
}
// Implementation
// ==============
//! \brief Save a timestep
void UnsaturatedHDF5Saver::UnsaturatedHDF5SaverImpl::save_timestep(
const std::string& name,
const std::string& section
)
{
HDF5File the_file(m_filepath, HDF5_OpenMode::OpenReadWrite);
auto group_name = the_file.complete_name(name, section);
auto group = the_file.create_group(group_name);
save_main_variables(the_file, group_name);
save_chemical_solution(the_file, group_name);
save_transport_properties(the_file, group_name);
}
void UnsaturatedHDF5Saver::UnsaturatedHDF5SaverImpl::save_main_variables(
HDF5File& file,
const std::string& section
)
{
auto group_name = file.complete_name(MAIN_VARIABLES_GRPNAME, section);
auto group = file.create_group(group_name);
// water
{
auto water_group = group->createGroup(m_database->get_label_component(0));
save_eigen_matrix(water_group, LIQUID_SATURATION_DSET,
m_vars->get_liquid_saturation().variable);
save_eigen_matrix(water_group, AQUEOUS_CONC_DSET,
m_vars->get_water_aqueous_concentration().variable);
save_eigen_matrix(water_group, SOLID_CONC_DSET,
m_vars->get_solid_concentration(0).variable);
if (m_vars->component_has_gas(0)) {
save_eigen_matrix(water_group, PRESSURE_DSET,
m_vars->get_pressure_main_variables(0).variable);
}
save_eigen_matrix(water_group, CAP_PRESSURE_DSET,
m_vars->get_capillary_pressure().variable);
}
for (index_t aq_component: m_database->range_aqueous_component())
{
auto aqcomp_group = group->createGroup(m_database->get_label_component(aq_component));
save_eigen_matrix(aqcomp_group, AQUEOUS_CONC_DSET,
m_vars->get_aqueous_concentration(aq_component).variable);
save_eigen_matrix(aqcomp_group, SOLID_CONC_DSET,
m_vars->get_solid_concentration(aq_component).variable);
if (m_vars->component_has_gas(aq_component)) {
save_eigen_matrix(aqcomp_group, PRESSURE_DSET,
m_vars->get_pressure_main_variables(aq_component).variable);
}
}
}
void UnsaturatedHDF5Saver::UnsaturatedHDF5SaverImpl::save_chemical_solution(
HDF5File& file,
const std::string& section
)
{
auto group_name = file.complete_name(CHEMISTRY_SOLUTION_GRPNAME, section);
auto group = file.create_group(group_name);
for (auto node: m_vars->get_mesh()->range_nodes())
{
save_adimensional_system_solution(file, std::to_string(node), group_name,
m_vars->get_adim_solution(node));
}
}
// time to get ugly and use macro
// this is simply a quick wrapper that call the save_eigen matrix function
// It avoids all the boilerplate
#define save_transport_property(name, var) \
save_eigen_matrix(*group.get(), name, m_vars->get_##var ().variable)
void UnsaturatedHDF5Saver::UnsaturatedHDF5SaverImpl::save_transport_properties(
HDF5File& file,
const std::string& section
)
{
auto group_name = file.complete_name(TRANSPORT_PROPERTIES_GRPNAME, section);
auto group = file.create_group(group_name);
save_transport_property(POROSITY_DSET, porosity);
save_transport_property(LIQUID_DIFFUSIVITY_DSET,
liquid_diffusivity);
save_transport_property(REL_LIQUID_DIFFUSIVITY_DSET,
relative_liquid_diffusivity);
save_transport_property(LIQUID_PERMEABILITY_DSET,
liquid_permeability);
save_transport_property(REL_LIQUID_PERMEABILITY_DSET,
relative_liquid_permeability);
save_transport_property(RESISTANCE_GAS_DIFFUSIVITY_DSET,
resistance_gas_diffusivity);
save_transport_property(REL_GAS_DIFFUSIVITY_DSET,
relative_gas_diffusivity);
}
#undef save_transport_property // we remove the ugly macro, no one saws anything :)
void
UnsaturatedHDF5Saver::UnsaturatedHDF5SaverImpl::init_file(
const units::UnitsSet& the_units
)
{
HDF5File the_file(m_filepath, HDF5_OpenMode::CreateTruncate);
save_mesh_coordinates(the_file, MESH_GRPNAME, "/", m_vars->get_mesh());
save_database_labels(the_file, DATABASE_GRPNAME, "/", m_vars->get_database());
hsize_t dims[] = {2};
auto dspace = H5::DataSpace(1, dims);
auto attribute = the_file.create_attribute(
UNIT_ATTRIBUTE,
H5::PredType::NATIVE_DOUBLE,
dspace);
scalar_t attribute_values[] = {
units::scaling_factor(the_units.length),
units::scaling_factor(the_units.mass)
};
attribute->write(H5::PredType::NATIVE_DOUBLE, attribute_values);
}
// Reader
// =======
void
UnsaturatedHDF5Reader::UnsaturatedHDF5ReaderImpl::initialize_variables(
scalar_t timestep,
reactmicp::systems::unsaturated::UnsaturatedVariables *vars
)
{
auto name_group = m_timesteps.get_string(timestep);
auto group = m_file.open_group(name_group);
const auto& grp_ref = *group.get();
read_main_variables(grp_ref, vars);
read_chemical_solutions(grp_ref, vars);
read_transport_properties(grp_ref, vars);
}
void
UnsaturatedHDF5Reader::UnsaturatedHDF5ReaderImpl::read_main_variables(
const H5::Group& timestep_group,
reactmicp::systems::unsaturated::UnsaturatedVariables* vars
)
{
H5::Group main_vars_grp = timestep_group.openGroup(MAIN_VARIABLES_GRPNAME);
database::RawDatabasePtr raw_database = vars->get_database();
{ // Water
auto group = main_vars_grp.openGroup(raw_database->get_label_component(0));
read_eigen_matrix(group, LIQUID_SATURATION_DSET,
vars->get_liquid_saturation().variable);
read_eigen_matrix(group, AQUEOUS_CONC_DSET,
vars->get_water_aqueous_concentration().variable);
read_eigen_matrix(group, SOLID_CONC_DSET,
vars->get_solid_concentration(0).variable);
if (vars->component_has_gas(0)) {
read_eigen_matrix(group, PRESSURE_DSET,
vars->get_pressure_main_variables(0).variable);
}
read_eigen_matrix(group, CAP_PRESSURE_DSET,
vars->get_capillary_pressure().variable);
}
for (auto component: raw_database->range_aqueous_component())
{
auto group = main_vars_grp.openGroup(
raw_database->get_label_component(component));
read_eigen_matrix(group, AQUEOUS_CONC_DSET,
vars->get_aqueous_concentration(component).variable);
read_eigen_matrix(group, SOLID_CONC_DSET,
vars->get_solid_concentration(component).variable);
if (vars->component_has_gas(component)) {
read_eigen_matrix(group, PRESSURE_DSET,
vars->get_pressure_main_variables(component).variable);
}
}
}
void
UnsaturatedHDF5Reader::UnsaturatedHDF5ReaderImpl::read_chemical_solutions(
const H5::Group& timestep_group,
reactmicp::systems::unsaturated::UnsaturatedVariables* vars
)
{
H5::Group chem_group = timestep_group.openGroup(CHEMISTRY_SOLUTION_GRPNAME);
database::RawDatabasePtr raw_database = vars->get_database();
mesh::Mesh1DPtr the_mesh = vars->get_mesh();
for (auto node: the_mesh->range_nodes())
{
auto group = chem_group.openGroup(std::to_string(node));
read_adimensional_system_solution(group, vars->get_adim_solution(node));
}
}
// time to get ugly and use macro
// this is simply a quick wrapper that call the read_eigen_matrix function
// It avoids all the boilerplate
#define read_transport_property(name, var) \
read_eigen_matrix(trans_group, name, vars->get_##var ().variable)
void
UnsaturatedHDF5Reader::UnsaturatedHDF5ReaderImpl::read_transport_properties(
const H5::Group& timestep_group,
reactmicp::systems::unsaturated::UnsaturatedVariables* vars
)
{
H5::Group trans_group = timestep_group.openGroup(TRANSPORT_PROPERTIES_GRPNAME);
read_transport_property(POROSITY_DSET, porosity);
read_transport_property(LIQUID_DIFFUSIVITY_DSET, liquid_diffusivity);
read_transport_property(REL_LIQUID_DIFFUSIVITY_DSET,
relative_liquid_diffusivity);
read_transport_property(LIQUID_PERMEABILITY_DSET,
liquid_permeability);
read_transport_property(REL_LIQUID_PERMEABILITY_DSET,
relative_liquid_permeability);
read_transport_property(RESISTANCE_GAS_DIFFUSIVITY_DSET,
resistance_gas_diffusivity);
read_transport_property(REL_GAS_DIFFUSIVITY_DSET,
relative_gas_diffusivity);
}
#undef read_transport_property // we remove the ugly macro, no one saws anything :)
} //end namespace io
} //end namespace specmicp

Event Timeline