Page MenuHomec4science

hdf5_adimensional.cpp
No OneTemporary

File Metadata

Created
Thu, May 16, 23:23

hdf5_adimensional.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_adimensional.hpp"
#include "../adimensional/adimensional_system_solution.hpp"
#include "../../utils/io/specmicp_hdf5.hpp"
#include "../../utils/io/hdf5_eigen.hpp"
namespace specmicp {
namespace io {
//! \brief Implementation class to save a HDF5 file
//!
//! \internal
class SPECMICP_DLL_LOCAL AdimensionalSystemSolutionHDF5Saver
{
public:
AdimensionalSystemSolutionHDF5Saver(
const AdimensionalSystemSolution& solution):
m_solution(solution)
{}
//! \brief Save the solution into file (in subgroup section/name)
void save(
HDF5File& file,
const std::string& name,
const std::string& section
);
private:
void save_main_variables(HDF5File& file, const std::string& section);
void save_molalities(HDF5File& file, const std::string& section);
void save_loggamma(HDF5File& file, const std::string& section);
void save_gas_fugacities(HDF5File& file, const std::string& section);
void save_sorbed_molalities(HDF5File& file, const std::string& section);
const AdimensionalSystemSolution& m_solution;
};
void save_adimensional_system_solution(
HDF5File& file,
const std::string& name,
const std::string& section,
const AdimensionalSystemSolution& solution
)
{
AdimensionalSystemSolutionHDF5Saver saver(solution);
saver.save(file, name, section);
}
// Implementation
// ==============
void AdimensionalSystemSolutionHDF5Saver::save(
HDF5File& file,
const std::string& name,
const std::string& section
)
{
auto complete_sec = file.complete_name(name, section);
auto adim_group = file.create_group(complete_sec);
save_main_variables(file, complete_sec);
save_molalities(file, complete_sec);
save_loggamma(file, complete_sec);
if (m_solution.gas_fugacities.rows() > 0) {
save_gas_fugacities(file, complete_sec);
}
if (m_solution.sorbed_molalities.rows() > 0) {
save_sorbed_molalities(file, complete_sec);
}
}
void AdimensionalSystemSolutionHDF5Saver::save_main_variables(
HDF5File& file,
const std::string &section
)
{
auto dataset = save_eigen_matrix(file,
"main_variables", section,
m_solution.main_variables
);
hsize_t dims[] = {2};
auto dspace = H5::DataSpace(1, dims);
auto attribute = dataset->createAttribute("attribute",
H5::PredType::NATIVE_DOUBLE,
dspace);
scalar_t attribute_values[] = {m_solution.inert_volume_fraction,
m_solution.ionic_strength};
attribute.write(H5::PredType::NATIVE_DOUBLE, attribute_values);
}
void AdimensionalSystemSolutionHDF5Saver::save_molalities(
HDF5File& file,
const std::string &section
)
{
save_eigen_matrix(file,
"secondary_molalities", section,
m_solution.secondary_molalities
);
}
void AdimensionalSystemSolutionHDF5Saver::save_loggamma(
HDF5File& file,
const std::string &section
)
{
save_eigen_matrix(file,
"log_gamma", section,
m_solution.log_gamma
);
}
void AdimensionalSystemSolutionHDF5Saver::save_gas_fugacities(
HDF5File& file,
const std::string &section
)
{
save_eigen_matrix(file,
"gas_fugacities", section,
m_solution.gas_fugacities
);
}
void AdimensionalSystemSolutionHDF5Saver::save_sorbed_molalities(
HDF5File& file,
const std::string &section
)
{
save_eigen_matrix(file,
"sorbed_molalities", section,
m_solution.sorbed_molalities
);
}
} //end namespace io
} //end namespace specmicp

Event Timeline