Skip to content

Commit

Permalink
Add AbstractState functions to CoolPropLib
Browse files Browse the repository at this point in the history
AbstractState_get_mole_fractions_satState
AbstractState_keyed_output_satState
AbstractState_backend_name
add_fluids_as_JSON

changes to AbstractState_get_phase_envelope_data
  • Loading branch information
friederikeboehm committed May 11, 2022
1 parent 0f6a426 commit 8782f2e
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 8 deletions.
55 changes: 53 additions & 2 deletions include/CoolPropLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,21 @@ EXPORT_CODE void CONVENTION AbstractState_set_fractions(const long handle, const
*/
EXPORT_CODE void CONVENTION AbstractState_get_mole_fractions(const long handle, double* fractions, const long maxN, long* N, long* errcode,
char* message_buffer, const long buffer_length);
/**
* @brief Get the molar fractions for the AbstractState and the desired saturated State
* @param handle The integer handle for the state class stored in memory
* @param saturated_state The string specifying the state (liquid or gas)
* @param fractions The array of fractions
* @param maxN The length of the buffer for the fractions
* @param N number of fluids
* @param errcode The errorcode that is returned (0 = no error, !0 = error)
* @param message_buffer A buffer for the error code
* @param buffer_length The length of the buffer for the error code
* @return
*/
EXPORT_CODE void CONVENTION AbstractState_get_mole_fractions_satState(const long handle, const char* saturated_state, double* fractions,
const long maxN, long* N, long* errcode, char* message_buffer,
const long buffer_length);
/**
* @brief Update the state of the AbstractState
* @param handle The integer handle for the state class stored in memory
Expand Down Expand Up @@ -559,21 +574,25 @@ EXPORT_CODE void CONVENTION AbstractState_build_phase_envelope(const long handle
* @brief Get data from the phase envelope for the given mixture composition
* @param handle The integer handle for the state class stored in memory
* @param length The number of elements stored in the arrays (both inputs and outputs MUST be the same length)
* @param maxComponents The number of fluid components for which memory is allocated
* @param T The pointer to the array of temperature (K)
* @param p The pointer to the array of pressure (Pa)
* @param rhomolar_vap The pointer to the array of molar density for vapor phase (m^3/mol)
* @param rhomolar_liq The pointer to the array of molar density for liquid phase (m^3/mol)
* @param x The compositions of the "liquid" phase (WARNING: buffer should be Ncomp*Npoints in length, at a minimum, but there is no way to check buffer length at runtime)
* @param y The compositions of the "vapor" phase (WARNING: buffer should be Ncomp*Npoints in length, at a minimum, but there is no way to check buffer length at runtime)
* @param actual_length The number of elements actually stored in the arrays
* @param actual_components The number of fluid components actually stored in the arrays
* @param errcode The errorcode that is returned (0 = no error, !0 = error)
* @param message_buffer A buffer for the error code
* @param buffer_length The length of the buffer for the error code
* @return
*
* @note If there is an error in an update call for one of the inputs, no change in the output array will be made
*/
EXPORT_CODE void CONVENTION AbstractState_get_phase_envelope_data(const long handle, const long length, double* T, double* p, double* rhomolar_vap,
double* rhomolar_liq, double* x, double* y, long* errcode, char* message_buffer,
EXPORT_CODE void CONVENTION AbstractState_get_phase_envelope_data(const long handle, const long length, const long maxComponents, double* T,
double* p, double* rhomolar_vap, double* rhomolar_liq, double* x, double* y,
long* actual_length, long* actual_components, long* errcode, char* message_buffer,
const long buffer_length);

/**
Expand Down Expand Up @@ -620,6 +639,38 @@ EXPORT_CODE void CONVENTION AbstractState_get_spinodal_data(const long handle, c
*/
EXPORT_CODE void CONVENTION AbstractState_all_critical_points(const long handle, const long length, double* T, double* p, double* rhomolar,
long* stable, long* errcode, char* message_buffer, const long buffer_length);
/**
* @brief Get an output value from the AbstractState using an integer value for the desired output value and desired saturated State
* @param handle The integer handle for the state class stored in memory
* @param saturated_state The string specifying the state (liquid or gas)
* @param param The integer value for the parameter you want
* @param errcode The errorcode that is returned (0 = no error, !0 = error)
* @param message_buffer A buffer for the error code
* @param buffer_length The length of the buffer for the error code
* @return
*/
EXPORT_CODE double CONVENTION AbstractState_keyed_output_satState(const long handle, const char* saturated_state, const long param, long* errcode,
char* message_buffer, const long buffer_length);
/**
* @brief Return the name of the backend used in the AbstractState
* @param handle The integer handle for the state class stored in memory
* @param backend The char pointer the name is written to
* @param errcode The errorcode that is returned (0 = no error, !0 = error)
* @param message_buffer A buffer for the error code
* @param buffer_length The length of the buffer for the error code
* @return
*/
EXPORT_CODE void CONVENTION AbstractState_backend_name(const long handle, char* backend, long* errcode, char* message_buffer,
const long buffer_length);
/**
* \brief Add fluids as a JSON-formatted string
* @param backend The backend to which these should be added; e.g. "HEOS", "SRK", "PR"
* @param fluidstring The JSON-formatted string
* @return
*
*/
EXPORT_CODE void CONVENTION add_fluids_as_JSON(const char* backend, const char* fluidstring, long* errcode, char* message_buffer,
const long buffer_length);

// *************************************************************************************
// *************************************************************************************
Expand Down
103 changes: 97 additions & 6 deletions src/CoolPropLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,40 @@ EXPORT_CODE void CONVENTION AbstractState_get_mole_fractions(const long handle,
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_get_mole_fractions_satState(const long handle, const char* saturated_state, double* fractions,
const long maxN, long* N, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;

try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
std::vector<double> _fractions;
double quality = AS->Q();
if (0 <= quality && quality <= 1) {
if (strcmp("liquid", saturated_state) == 0) {
_fractions = AS->mole_fractions_liquid();
} else if (strcmp("gas", saturated_state) == 0) {
_fractions = AS->mole_fractions_vapor();
} else {
throw CoolProp::ValueError(
format("Bad info string [%s] to saturated state mole fractions, options are \"liquid\" and \"gas\"", saturated_state));
}
} else {
throw CoolProp::ValueError(format("AbstractState_get_mole_fractions_satState only returns outputs for saturated states if AbstractState "
"quality [%g] is within two-phase region (0 <= quality <= 1)",
static_cast<double>(quality)));
}
*N = _fractions.size();
if (*N <= maxN) {
for (int i = 0; i < *N; i++)
fractions[i] = _fractions[i];
} else {
throw CoolProp::ValueError(format("Length of array [%d] is greater than allocated buffer length [%d]", *N, maxN));
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}
EXPORT_CODE void CONVENTION AbstractState_update(const long handle, const long input_pair, const double value1, const double value2, long* errcode,
char* message_buffer, const long buffer_length) {
*errcode = 0;
Expand Down Expand Up @@ -744,26 +778,32 @@ EXPORT_CODE void CONVENTION AbstractState_build_phase_envelope(const long handle
}
}

EXPORT_CODE void CONVENTION AbstractState_get_phase_envelope_data(const long handle, const long length, double* T, double* p, double* rhomolar_vap,
double* rhomolar_liq, double* x, double* y, long* errcode, char* message_buffer,
EXPORT_CODE void CONVENTION AbstractState_get_phase_envelope_data(const long handle, const long length, const long maxComponents, double* T,
double* p, double* rhomolar_vap, double* rhomolar_liq, double* x, double* y,
long* actual_length, long* actual_components, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;
try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
CoolProp::PhaseEnvelopeData pe = AS->get_phase_envelope_data();
*actual_length = pe.T.size();
if (pe.T.size() > static_cast<std::size_t>(length)) {
throw CoolProp::ValueError(format("Length of phase envelope vectors [%d] is greater than allocated buffer length [%d]",
static_cast<int>(pe.T.size()), static_cast<int>(length)));
}
std::size_t N = pe.x.size();
*actual_components = pe.x.size();
if (*actual_components > static_cast<std::size_t>(maxComponents)) {
throw CoolProp::ValueError(format("Length of phase envelope composition vectors [%d] is greater than allocated buffer length [%d]",
static_cast<int>(*actual_components), static_cast<int>(maxComponents)));
}
for (std::size_t i = 0; i < pe.T.size(); i++) {
*(T + i) = pe.T[i];
*(p + i) = pe.p[i];
*(rhomolar_vap + i) = pe.rhomolar_vap[i];
*(rhomolar_liq + i) = pe.rhomolar_liq[i];
for (std::size_t j = 0; j < N; ++j) {
*(x + i * N + j) = pe.x[j][i];
*(y + i * N + j) = pe.y[j][i];
for (std::size_t j = 0; j < *actual_components; ++j) {
*(x + i * *actual_components + j) = pe.x[j][i];
*(y + i * *actual_components + j) = pe.y[j][i];
}
}
} catch (...) {
Expand Down Expand Up @@ -821,3 +861,54 @@ EXPORT_CODE void CONVENTION AbstractState_all_critical_points(const long handle,
HandleException(errcode, message_buffer, buffer_length);
}
}

EXPORT_CODE double CONVENTION AbstractState_keyed_output_satState(const long handle, const char* saturated_state, const long param, long* errcode,
char* message_buffer, const long buffer_length) {
*errcode = 0;

try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
double quality = AS->Q();
if (0 <= quality && quality <= 1) {
if (strcmp("liquid", saturated_state) == 0) {
return AS->saturated_liquid_keyed_output(static_cast<CoolProp::parameters>(param));
} else if (strcmp("gas", saturated_state) == 0) {
return AS->saturated_vapor_keyed_output(static_cast<CoolProp::parameters>(param));
} else {
throw CoolProp::ValueError(
format("Bad info string [%s] to saturated state output, options are \"liquid\" and \"gas\"", saturated_state));
}
} else {
throw CoolProp::ValueError(format("AbstractState_keyed_output_satState only returns outputs for saturated states if AbstractState "
"quality [%g] is within two-phase region (0 <= quality <= 1)",
static_cast<double>(quality)));
}
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
return _HUGE;
}
}

EXPORT_CODE void CONVENTION AbstractState_backend_name(const long handle, char* backend, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;

try {
shared_ptr<CoolProp::AbstractState>& AS = handle_manager.get(handle);
strcpy(backend, AS->backend_name().c_str());

} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}

EXPORT_CODE void CONVENTION add_fluids_as_JSON(const char* backend, const char* fluidstring, long* errcode, char* message_buffer,
const long buffer_length) {
*errcode = 0;

try {
CoolProp::add_fluids_as_JSON(backend, fluidstring);
} catch (...) {
HandleException(errcode, message_buffer, buffer_length);
}
}

0 comments on commit 8782f2e

Please sign in to comment.