Skip to content

Commit

Permalink
Added derivStrings branch to trunk and fixed the last issue with deri…
Browse files Browse the repository at this point in the history
…vative smoothing in CoolProp

git-svn-id: https://svn.modelica.org/projects/ExternalMediaLibrary/trunk@8047 7ce873d0-865f-4ce7-a662-4bb36ea78beb
  • Loading branch information
jorrit committed Jan 8, 2015
1 parent 8ead1cb commit fcf4470
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ package ExternalTwoPhaseMedium "Generic external two phase medium package"
annotation(Include="#include \"externalmedialib.h\"", Library="ExternalMediaLib", IncludeDirectory="modelica://ExternalMedia/Resources/Include", LibraryDirectory="modelica://ExternalMedia/Resources/Library");
end setState_hs;

replaceable function partialDeriv_state
"Return partial derivative from a thermodynamic state record"
extends Modelica.Icons.Function;
input String of "The property to differentiate";
input String wrt "Differentiate with respect to this";
input String cst "Keep this constant";
input ThermodynamicState state;
output Real partialDerivative;
external "C" partialDerivative= TwoPhaseMedium_partialDeriv_state_C_impl(of, wrt, cst, state, mediumName, libraryName, substanceName)
annotation(Include="#include \"externalmedialib.h\"", Library="ExternalMediaLib", IncludeDirectory="modelica://ExternalMedia/Resources/Include", LibraryDirectory="modelica://ExternalMedia/Resources/Library");
end partialDeriv_state;

redeclare function extends setState_phX
algorithm
// The composition is an empty vector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
Francesco Casella, Christoph Richter, Roberto Bonifetto
2006-2012
Copyright Politecnico di Milano, TU Braunschweig, Politecnico di Torino
Minor additions in 2014 to make ExternalMedia compatible
Minor additions in 2014 to make ExternalMedia compatible
with GCC on Linux operating systems
Jorrit Wronski (Technical University of Denmark)
*/
Expand All @@ -24,7 +24,7 @@
#define CHOICE_ps 4
#define CHOICE_pT 5

/*! Detect the platform in order to avoid the DLL commands from
/*! Detect the platform in order to avoid the DLL commands from
* making g++ choke. Code taken from CoolProp...
*/
#if defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(__WIN64__)
Expand All @@ -35,7 +35,7 @@
# define __ISLINUX__
#endif

/*!
/*!
Portable definitions of the EXPORT macro
*/
#ifndef EXPORT
Expand Down Expand Up @@ -146,6 +146,8 @@ extern "C" {
EXPORT void TwoPhaseMedium_setState_ps_C_impl(double p, double s, int phase, ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);
EXPORT void TwoPhaseMedium_setState_hs_C_impl(double h, double s, int phase, ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);

EXPORT double TwoPhaseMedium_partialDeriv_state_C_impl(const char *of, const char *wrt, const char *cst, ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);

EXPORT double TwoPhaseMedium_prandtlNumber_C_impl(ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);
EXPORT double TwoPhaseMedium_temperature_C_impl(ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);
EXPORT double TwoPhaseMedium_velocityOfSound_C_impl(ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);
Expand Down
5 changes: 2 additions & 3 deletions Modelica/ExternalMedia 3.2.1/Test.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1100,9 +1100,8 @@ package Test "Test models for the different solvers"
p = 10E5;
sat_std = fluid_std.setSat_p(p);
h_start = fluid_std.bubbleEnthalpy(sat_std);
h_end = h_start;
//h_end = fluid_std.dewEnthalpy(sat_std);
h_delta = 1e3;
h_end = fluid_std.dewEnthalpy(sat_std);
h_delta = 3e3;
h = (h_start - h_delta) + (h_end-h_start+2*h_delta)*time/t;
state_std = fluid_std.setState_ph(p,h);
state_spl = fluid_spl.setState_ph(p,h);
Expand Down
35 changes: 27 additions & 8 deletions Projects/Sources/basesolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ double BaseSolver::criticalEntropy() const{
This function sets the fluid constants which are defined in the
FluidConstants record in Modelica. It should be called when a new
solver is created.
Must be re-implemented in the specific solver
*/
void BaseSolver::setFluidConstants(){
Expand Down Expand Up @@ -151,6 +151,25 @@ void BaseSolver::setState_hs(double &h, double &s, int &phase, ExternalThermodyn
errorMessage((char*)"Internal error: setState_hs() not implemented in the Solver object");
}

//! Compute partial derivative from a populated state record
/*!
This function computes the derivative of the specified input. Note that it requires
a populated state record as input.
@param of Property to differentiate
@param wrt Property to differentiate in
@param cst Property to remain constant
@param state Pointer to input values in state record
@param mediumName Medium name
@param libraryName Library name
@param substanceName Substance name
*/
double BaseSolver::partialDeriv_state(const string &of, const string &wrt, const string &cst, ExternalThermodynamicState *const properties){
//double BaseSolver::partialDeriv_state(const char *of, const char *wrt, const char *cst, ExternalThermodynamicState *const properties){
errorMessage((char*)"Internal error: partialDeriv_state() not implemented in the Solver object");
return 0.;
}

//! Compute Prandtl number
/*!
This function returns the Prandtl number
Expand Down Expand Up @@ -434,11 +453,11 @@ void BaseSolver::setSat_T(double &T, ExternalSaturationProperties *const propert

//! Set bubble state
/*!
This function sets the bubble state record bubbleProperties corresponding to the
This function sets the bubble state record bubbleProperties corresponding to the
saturation data contained in the properties record.
The default implementation of the setBubbleState function is relying on the correct
behaviour of setState_ph with respect to the state input. Can be overridden
The default implementation of the setBubbleState function is relying on the correct
behaviour of setState_ph with respect to the state input. Can be overridden
in the specific solver code to get more efficient or correct handling of this situation.
@param properties ExternalSaturationProperties record with saturation properties data
@param phase Phase (1: one-phase, 2: two-phase)
Expand All @@ -452,11 +471,11 @@ void BaseSolver::setBubbleState(ExternalSaturationProperties *const properties,

//! Set dew state
/*!
This function sets the dew state record dewProperties corresponding to the
This function sets the dew state record dewProperties corresponding to the
saturation data contained in the properties record.
The default implementation of the setDewState function is relying on the correct
behaviour of setState_ph with respect to the state input. Can be overridden
The default implementation of the setDewState function is relying on the correct
behaviour of setState_ph with respect to the state input. Can be overridden
in the specific solver code to get more efficient or correct handling of this situation.
@param properties ExternalSaturationProperties record with saturation properties data
@param phase Phase (1: one-phase, 2: two-phase)
Expand Down Expand Up @@ -640,7 +659,7 @@ double BaseSolver::sv(ExternalSaturationProperties *const properties){
/*!
This function computes the derivatives according to the Bridgman's table.
The computed values are written to the two phase medium property struct.
This function can be called from within the setState_XX routines
This function can be called from within the setState_XX routines
when implementing a new solver. Please be aware that cp, beta and
kappa have to be provided to allow the computation of the derivatives. It
returns false if the computation failed.
Expand Down
4 changes: 3 additions & 1 deletion Projects/Sources/basesolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class BaseSolver{
virtual void setState_ps(double &p, double &s, int &phase, ExternalThermodynamicState *const properties);
virtual void setState_hs(double &h, double &s, int &phase, ExternalThermodynamicState *const properties);

virtual double partialDeriv_state(const string &of, const string &wrt, const string &cst, ExternalThermodynamicState *const properties);

virtual double Pr(ExternalThermodynamicState *const properties);
virtual double T(ExternalThermodynamicState *const properties);
virtual double a(ExternalThermodynamicState *const properties);
Expand Down Expand Up @@ -97,7 +99,7 @@ class BaseSolver{

protected:
//! Fluid constants
FluidConstants _fluidConstants;
FluidConstants _fluidConstants;
};

#endif // BASESOLVER_H_
41 changes: 41 additions & 0 deletions Projects/Sources/coolpropsolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,47 @@ void CoolPropSolver::setState_hs(double &h, double &s, int &phase, ExternalTherm
}
}

double CoolPropSolver::partialDeriv_state(const string &of, const string &wrt, const string &cst, ExternalThermodynamicState *const properties){
if (debug_level > 5)
std::cout << format("partialDeriv_state(of=%s,wrt=%s,cst=%s,state)\n",of.c_str(),wrt.c_str(),cst.c_str());

long derivTerm = makeDerivString(of,wrt,cst);
double res = NAN;

try{
//res = DerivTerms(derivTerm, properties->d, properties->T, this->substanceName);
state->update(iT,properties->T,iD,properties->d);
// Get the output value
res = state->keyed_output(derivTerm);
} catch(std::exception &e) {
errorMessage((char*)e.what());
}
return res;
}

long CoolPropSolver::makeDerivString(const string &of, const string &wrt, const string &cst){
std::string derivTerm;
if (!of.compare("d")){ derivTerm = "drho"; }
else if (!of.compare("p")){ derivTerm = "dp"; }
else {
errorMessage((char*) format("Internal error: Derivatives of %s are not defined in the Solver object.",of.c_str()).c_str());
}
if (!wrt.compare("p")){ derivTerm.append("dp"); }
else if (!wrt.compare("h")){ derivTerm.append("dh"); }
else if (!wrt.compare("T")){ derivTerm.append("dT"); }
else {
errorMessage((char*) format("Internal error: Derivatives with respect to %s are not defined in the Solver object.",wrt.c_str()).c_str());
}
if (!cst.compare("p")){ derivTerm.append("|p"); }
else if (!cst.compare("h")){ derivTerm.append("|h"); }
else if (!cst.compare("d")){ derivTerm.append("|rho"); }
else {
errorMessage((char*) format("Internal error: Derivatives at constant %s are not defined in the Solver object.",cst.c_str()).c_str());
}
long iOutput = get_param_index(derivTerm);
return iOutput;
}


double CoolPropSolver::Pr(ExternalThermodynamicState *const properties){
// Base function returns an error if called - should be redeclared by the solver object
Expand Down
3 changes: 3 additions & 0 deletions Projects/Sources/coolpropsolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CoolPropSolver : public BaseSolver{

virtual void preStateChange(void);
virtual void postStateChange(ExternalThermodynamicState *const properties);
long makeDerivString(const string &of, const string &wrt, const string &cst);

public:
CoolPropSolver(const std::string &mediumName, const std::string &libraryName, const std::string &substanceName);
Expand All @@ -55,6 +56,8 @@ class CoolPropSolver : public BaseSolver{
virtual void setState_ps(double &p, double &s, int &phase, ExternalThermodynamicState *const properties);
virtual void setState_hs(double &h, double &s, int &phase, ExternalThermodynamicState *const properties);

virtual double partialDeriv_state(const string &of, const string &wrt, const string &cst, ExternalThermodynamicState *const properties);

virtual double Pr(ExternalThermodynamicState *const properties);
virtual double T(ExternalThermodynamicState *const properties);
virtual double a(ExternalThermodynamicState *const properties);
Expand Down
18 changes: 18 additions & 0 deletions Projects/Sources/externalmedialib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,24 @@ void TwoPhaseMedium_setState_hs_C_impl(double h, double s, int phase, ExternalTh
solver->setState_hs(h, s, phase, state);
}

//! Compute partial derivative from a populated state record
/*!
This function computes the derivative of the specified input.
@param of Property to differentiate
@param wrt Property to differentiate in
@param cst Property to remain constant
@param state Pointer to input values in state record
@param mediumName Medium name
@param libraryName Library name
@param substanceName Substance name
*/
double TwoPhaseMedium_partialDeriv_state_C_impl(const char *of, const char *wrt, const char *cst,
ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName){
BaseSolver *solver = SolverMap::getSolver(mediumName, libraryName, substanceName);
return solver->partialDeriv_state(of, wrt, cst, state);
}


//! Return Prandtl number of specified medium
/*! Note: This function is not used by the default implementation of ExternalTwoPhaseMedium class.
It might be used by external medium models customized solvers redeclaring the default functions
Expand Down
10 changes: 6 additions & 4 deletions Projects/Sources/externalmedialib.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
Francesco Casella, Christoph Richter, Roberto Bonifetto
2006-2012
Copyright Politecnico di Milano, TU Braunschweig, Politecnico di Torino
Minor additions in 2014 to make ExternalMedia compatible
Minor additions in 2014 to make ExternalMedia compatible
with GCC on Linux operating systems
Jorrit Wronski (Technical University of Denmark)
*/
Expand All @@ -24,7 +24,7 @@
#define CHOICE_ps 4
#define CHOICE_pT 5

/*! Detect the platform in order to avoid the DLL commands from
/*! Detect the platform in order to avoid the DLL commands from
* making g++ choke. Code taken from CoolProp...
*/
#if defined(_WIN32) || defined(__WIN32__) || defined(_WIN64) || defined(__WIN64__)
Expand All @@ -35,7 +35,7 @@
# define __ISLINUX__
#endif

/*!
/*!
Portable definitions of the EXPORT macro
*/
#ifndef EXPORT
Expand Down Expand Up @@ -146,6 +146,8 @@ extern "C" {
EXPORT void TwoPhaseMedium_setState_ps_C_impl(double p, double s, int phase, ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);
EXPORT void TwoPhaseMedium_setState_hs_C_impl(double h, double s, int phase, ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);

EXPORT double TwoPhaseMedium_partialDeriv_state_C_impl(const char *of, const char *wrt, const char *cst, ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);

EXPORT double TwoPhaseMedium_prandtlNumber_C_impl(ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);
EXPORT double TwoPhaseMedium_temperature_C_impl(ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);
EXPORT double TwoPhaseMedium_velocityOfSound_C_impl(ExternalThermodynamicState *state, const char *mediumName, const char *libraryName, const char *substanceName);
Expand Down

0 comments on commit fcf4470

Please sign in to comment.