Skip to content

Commit

Permalink
Adding InterpolatedFluidProperties
Browse files Browse the repository at this point in the history
Fluid properties can be given as a grid of values. Then, bicubic interpolation
is created from those values and used for evaluation.
  • Loading branch information
andrsd committed Oct 24, 2023
1 parent 5583a4f commit fd04087
Show file tree
Hide file tree
Showing 12 changed files with 676 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ option(FPROPS_WITH_PYTHON "Build python wrapper" YES)
option(FPROPS_BUILD_TESTS "Build tests" NO)

find_package(HDF5 1.8 COMPONENTS C HL REQUIRED)
find_package(Eigen3 3.3.4 REQUIRED)
find_package(fmt 8.0 REQUIRED)
if (FPROPS_WITH_PYTHON)
add_subdirectory(python)
Expand Down
5 changes: 5 additions & 0 deletions cmake/CodeCoverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ if(FPROPS_CODE_COVERAGE)
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES "${CODE_COVERAGE_PROFRAWS}")

set(EXCLUDE_REGEX
--ignore-filename-regex=/contrib/
--ignore-filename-regex=/include/eigen3/
--ignore-filename-regex=/include/fmt/
)

set(CODE_COVERAGE_BINS
Expand Down Expand Up @@ -111,8 +114,10 @@ if(FPROPS_CODE_COVERAGE)
)

set(EXCLUDE_REGEX
--exclude=*/contrib/*
--exclude=/usr/include/*
--exclude=*/include/fmt/*
--exclude=*/include/eigen3/*
)

add_custom_target(coverage DEPENDS ${COVERAGE_INFO})
Expand Down
167 changes: 167 additions & 0 deletions include/InterpolatedFluidProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
#pragma once

#include "SinglePhaseFluidProperties.h"
#include <Eigen/Dense>
#include <vector>
#include <array>
#include <string>

namespace h5pp {
class File;
}

namespace fprops {

typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> DynDenseMatrix;
typedef Eigen::Matrix<Eigen::Matrix4d, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
CoefficientMatrix;

class InterpolatedFluidProperties : public SinglePhaseFluidProperties {
public:
InterpolatedFluidProperties();
virtual ~InterpolatedFluidProperties() = default;

/// Load data set from an HDF5 file
///
/// @param file_name HDF5 file name
virtual void load(const std::string & file_name);

State p_T(double p, double T) const override;
State rho_T(double rho, double T) const override;
State rho_p(double rho, double p) const override;
State v_u(double v, double u) const override;
State h_s(double h, double s) const override;

protected:
/// Fluid property data set
///
/// Data set consists of 2 independent variables and a set of dependent values.
/// Independent variables are given as an 1D array of values in ascending order.
/// Dependent variables are given as a 2D array of values with dimension (size of 1st indep.
/// variable, size od 2nd indep. variable).
class FPDataSet {
/// Number of dependent values
static constexpr std::size_t N = 12;

public:
/// Check if the data exists
///
/// @return `true` if this is a valid data set, `false` is this is an empty data set
bool empty() const;

/// Evaluate variables at location (x1, x2)
///
/// @param x1 Value of 1st independent variable
/// @param x2 Value of 2nd independent variable
/// @param val_idx Indices of dependent variables to compute
/// @return Values of computed variables
std::array<double, N>
eval(double x1, double x2, const std::vector<std::size_t> & val_idx) const;

/// Set range for 1st independent variable
///
/// @param values Values of 1st independent variable
void set_range_1(const Eigen::VectorXd & values);

/// Set range for 2nd independent variable
///
/// @param values Values of 2nd independent variable
void set_range_2(const Eigen::VectorXd & values);

/// Compute coefficient matrix for a dependent variable
///
/// @param idx Index of the dependent variable
/// @param y Data grid with dependent variable values
void set_values(std::size_t idx, const DynDenseMatrix & y);

protected:
/// List of the first independent variable
Eigen::VectorXd x1;
/// List of the second independent variable
Eigen::VectorXd x2;
/// Coefficient matrices for dependent variables
std::array<CoefficientMatrix, N> values;

/// Calculate derivatives of a 2D array with variable values
///
/// @param y Variable values
/// @param dydx1 Computed 1st derivative wrt 1st independent variable
/// @param dydx2 Computed 1st derivative wrt 2st independent variable
/// @param dydx1x2 Computed mixed derivative wrt 1st and 2nd independent variable
void calc_derivatives(const DynDenseMatrix & y,
DynDenseMatrix & dydx1,
DynDenseMatrix & dydx2,
DynDenseMatrix & dydx1x2);
};

/// Check units listed on the dataset
///
/// If expected unit is not found and exception is thrown
///
/// @param file HDF5 file
/// @param dataset_name Dataset name
/// @param unit Expected unit
void
check_unit(const h5pp::File & file, const std::string & dataset_name, const std::string & unit);

/// Read fluid property values from a group
///
/// @param file HDF5 file
/// @param group_name Group name to read from
/// @param vars Independent variables
/// @param idxs Indices of dependent variables
/// @return Fluid property data set to evaluate as a list of variable indices
FPDataSet read_data(const h5pp::File & file,
const std::string & group_name,
const std::pair<std::size_t, std::size_t> & vars,
const std::vector<std::size_t> & idxs);

/// Read independent variable values
///
/// @param file HDF5 file
/// @param group_name Group name
/// @param var_idx Index of an independent variable
/// @return Vector of independent variable values
Eigen::VectorXd
read_var_range(const h5pp::File & file, const std::string & group_name, std::size_t var_idx);

/// Read dependent variable values
///
/// @param file HDF5 file
/// @param group_name Group name
/// @param var_idx Index of an dependent variable
/// @return Grid values as a 2D array (represented as a matrix)
DynDenseMatrix
read_grid(const h5pp::File & file, const std::string & group_name, std::size_t var_idx);

/// Indices for computed quantities and indexing into var_names, var_units, etc.
static const std::size_t U;
static const std::size_t V;
static const std::size_t RHO;
static const std::size_t P;
static const std::size_t T;
static const std::size_t MU;
static const std::size_t CP;
static const std::size_t CV;
static const std::size_t S;
static const std::size_t K;
static const std::size_t H;
static const std::size_t W;

/// Fluid properties depending on pressure and temperature
FPDataSet pT_data;
/// Fluid properties depending on density and temperature
FPDataSet rhoT_data;
/// Fluid properties depending on density and pressure
FPDataSet rhop_data;
/// Fluid properties depending on specific volume and internal energy
FPDataSet vu_data;
/// Fluid properties depending on enthalpy and entropy
FPDataSet hs_data;
/// Variable names
std::vector<std::string> var_names;
/// Variable units
std::vector<std::string> var_units;
};

} // namespace fprops
3 changes: 2 additions & 1 deletion python/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.4...3.18)
project(pyfprops LANGUAGES CXX)
project(pyfprops LANGUAGES C CXX)

find_package(pybind11 2.9 REQUIRED)

Expand All @@ -10,6 +10,7 @@ target_include_directories(
PRIVATE
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}/include
${EIGEN3_INCLUDE_DIR}
)

target_link_libraries(
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ add_library(
Helium.cpp
Helmholtz.cpp
IdealGas.cpp
InterpolatedFluidProperties.cpp
Nitrogen.cpp
Numerics.cpp
Props.cpp
Expand Down Expand Up @@ -33,6 +34,7 @@ target_include_directories(
${CMAKE_BINARY_DIR}
${CMAKE_SOURCE_DIR}/contrib
${HDF5_INCLUDE_DIR}
${EIGEN3_INCLUDE_DIR}
)

target_link_libraries(
Expand Down
Loading

0 comments on commit fd04087

Please sign in to comment.