Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add defaults for Valve params and assert for check #201

Merged
merged 8 commits into from
Jul 23, 2024
28 changes: 20 additions & 8 deletions ThermofluidStream/FlowControl/BasicControlValve.mo
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ model BasicControlValve "Basic valve model with optional flow characteristics fo

parameter FlowCoeffType flowCoefficient = FlowCoeffType.Kvs "Select type of flow coefficient" annotation(Dialog(group = "Valve parameters"));
//Reference Values
parameter Real Kvs(unit = "m3/h") "Kvs-value (metric) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Kvs)));
parameter Real Cvs_US "Cvs-value (US [gal/min]) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_US)));
parameter Real Cvs_UK "Cvs-value (UK [gal/min]) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_UK)));
parameter SI.MassFlowRate m_flow_ref_set "Set reference mass flow in kg/s"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.m_flow_set)));
parameter Real Kvs(unit = "m3/h") = 0 "Kvs-value (metric) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Kvs)));
parameter Real Cvs_US = 0 "Cvs-value (US [gal/min]) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_US)));
parameter Real Cvs_UK = 0 "Cvs-value (UK [gal/min]) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_UK)));
parameter SI.MassFlowRate m_flow_ref_set = 0 "Reference mass flow rate"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.m_flow_set)));

protected
SI.VolumeFlowRate V_flow_ref=
Expand All @@ -31,6 +31,18 @@ protected
else m_flow_ref_set/rho_ref "Reference volume flow";

equation

//this if clause shall ensure that valid parameters have been entered
if flowCoefficient == FlowCoeffType.Kvs then
Copy link

@bilderbuchi bilderbuchi Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dzimmer @casella Considering only parameters are involved here, wouldn't it be better/more efficient/performant to place these if statements in an initial equation section, so they are only evaluated once instead of continuously?
Or can it be expected (is it specified, maybe?) that tools optimize that out and only execute it once, anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that Dymola will optimize for it but the specification does not say anything about it.
Moving to initial equations seems like a good idea.

Copy link
Contributor

@tobolar tobolar Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the assertion level is set to "error". So the current model's evaluation shall be aborted. The simulation itself still may continue with another evaluation, but IMO is not the case here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stupid me... Of course that is why I did not care when implementing it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh? It will only abort if the assert fails, no? If not, does this not check if Kvs>0 over and over again?

Copy link

@bilderbuchi bilderbuchi Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meaning, if you have FlowCoeffType.Kvs, and a Kvs of, say, 0.1, you would still trigger an assert check at every time step?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least there is a Modelica example with assert() for parameters.
https://build.openmodelica.org/Documentation/ModelicaReference.Operators.%27assert()%27.html

and assert for parameters is used e.g. in:

  • Modelica.Blocks.Continuous.Filter
  • Modelica.Electrical.Analog.Semiconductors.Diode2
  • Modelica.Fluid.Pipes.BaseClasses.PartialStraightPipe
  • Modelica.Mechanics.MultiBody.Joints.Revolute

I can't tell how often the assert is evaluated tho.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • corrected some typos
  • changed V_flow_ref to final parameter V_flow_ref
  • added a default value + assert for d_valve (ThermofluidStream.FlowControl.SpecificValveType).

I didnt know how to suggest the changes, i hope its fine, that i directly pushed them.

Could we also change m_flow_ref in ThermofluidStream.FlowControl.Internal.PartialValve to parameter m_flow_ref, or does that cause issues concerning conversion script or so?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushing directly is the best way.
I'll move the assert to the initial and if I do not find any reason against it would change m_flow_ref to a parameter

assert(Kvs > 0, "Invalid coefficeint for Kvs. Default value 0 shall not be used", level=AssertionLevel.error);
elseif flowCoefficient == FlowCoeffType.Cvs_US then
assert(Cvs_US > 0, "Invalid coefficeint for Cvs_US. Default value 0 shall not be used", level=AssertionLevel.error);
elseif flowCoefficient == FlowCoeffType.Cvs_UK then
assert(Cvs_UK > 0, "Invalid coefficeint for Cvs_UK. Default value 0 shall not be used", level=AssertionLevel.error);
else
assert(m_flow_ref_set > 0, "Invalid coefficeint for m_flow_ref_set. Default value 0 shall not be used", level=AssertionLevel.error);
end if;

//Calculate reference mass flow rate from reference volume flow rate
m_flow_ref = V_flow_ref*rho_ref;

Expand Down
27 changes: 19 additions & 8 deletions ThermofluidStream/FlowControl/SpecificValveType.mo
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ model SpecificValveType "Specific technical valve types"
parameter Modelica.Units.SI.Diameter d_valve "Flow diameter"
annotation (Evaluate=true, Dialog(group="Valve parameters", enable=(flowCoefficient== FlowCoeffType.flowDiameter)));
//Reference Values
parameter Real Kvs(unit = "m3/h") "Kvs-value (metric) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Kvs)));
parameter Real Cvs_US "Cvs-value (US [gal/min]) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_US)));
parameter Real Cvs_UK "Cvs-value (UK [gal/min]) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_UK)));
parameter SI.MassFlowRate m_flow_ref_set "Reference mass flow rate"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.m_flow_set)));
parameter Real Kvs(unit = "m3/h") = 0 "Kvs-value (metric) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Kvs)));
parameter Real Cvs_US = 0 "Cvs-value (US [gal/min]) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_US)));
parameter Real Cvs_UK = 0 "Cvs-value (UK [gal/min]) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_UK)));
parameter SI.MassFlowRate m_flow_ref_set = 0 "Reference mass flow rate"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.m_flow_set)));

protected
Modelica.Units.SI.Area A_valve=0.25*Modelica.Constants.pi*d_valve^2 "Cross-sectional area";
Expand All @@ -47,6 +47,17 @@ protected

equation

//this if clause shall ensure that valid parameters have been entered
if flowCoefficient == FlowCoeffType.Kvs then
assert(Kvs > 0, "Invalid coefficeint for Kvs. Default value 0 shall not be used", level=AssertionLevel.error);
elseif flowCoefficient == FlowCoeffType.Cvs_US then
assert(Cvs_US > 0, "Invalid coefficeint for Cvs_US. Default value 0 shall not be used", level=AssertionLevel.error);
elseif flowCoefficient == FlowCoeffType.Cvs_UK then
assert(Cvs_UK > 0, "Invalid coefficeint for Cvs_UK. Default value 0 shall not be used", level=AssertionLevel.error);
else
assert(m_flow_ref_set > 0, "Invalid coefficeint for m_flow_ref_set. Default value 0 shall not be used", level=AssertionLevel.error);
end if;

//Calculate reference mass flow rate from reference volume flow rate
m_flow_ref = V_flow_ref*rho_ref;

Expand Down
30 changes: 21 additions & 9 deletions ThermofluidStream/Undirected/FlowControl/BasicControlValve.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ model BasicControlValve "Basic valve model with optional flow characteristics fo

import FlowCoeffType = ThermofluidStream.FlowControl.Internal.Types.FlowCoefficientTypesBasic;

replaceable function valveCharacteristics = ThermofluidStream.FlowControl.Internal.ControlValve.linearCharacteristics
replaceable function valveCharacteristics =
ThermofluidStream.FlowControl.Internal.ControlValve.linearCharacteristics
constrainedby ThermofluidStream.FlowControl.Internal.ControlValve.partialValveCharacteristics
"Select valve characteristics"
annotation (
Expand All @@ -18,14 +19,14 @@ model BasicControlValve "Basic valve model with optional flow characteristics fo
parameter FlowCoeffType flowCoefficient = FlowCoeffType.Kvs "Select type of flow coefficient"
annotation(Dialog(group = "Valve parameters"));
//Reference Values
parameter Real Kvs(unit = "m3/h") "Kvs-value (metric) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Kvs)));
parameter Real Cvs_US "Cvs-value (US [gal/min]) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_US)));
parameter Real Cvs_UK "Cvs-value (UK [gal/min]) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_UK)));
parameter SI.MassFlowRate m_flow_ref_set "Set reference mass flow in kg/s"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.m_flow_set)));
parameter Real Kvs(unit = "m3/h") = 0 "Kvs-value (metric) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Kvs)));
parameter Real Cvs_US = 0 "Cvs-value (US [gal/min]) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_US)));
parameter Real Cvs_UK = 0 "Cvs-value (UK [gal/min]) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_UK)));
parameter SI.MassFlowRate m_flow_ref_set = 0 "Reference mass flow rate"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.m_flow_set)));

protected
SI.VolumeFlowRate V_flow_ref=
Expand All @@ -35,6 +36,17 @@ protected
else m_flow_ref_set/rho_ref "Reference volume flow rate";

equation
//this if clause shall ensure that valid parameters have been entered
if flowCoefficient == FlowCoeffType.Kvs then
assert(Kvs > 0, "Invalid coefficeint for Kvs. Default value 0 shall not be used", level=AssertionLevel.error);
elseif flowCoefficient == FlowCoeffType.Cvs_US then
assert(Cvs_US > 0, "Invalid coefficeint for Cvs_US. Default value 0 shall not be used", level=AssertionLevel.error);
elseif flowCoefficient == FlowCoeffType.Cvs_UK then
assert(Cvs_UK > 0, "Invalid coefficeint for Cvs_UK. Default value 0 shall not be used", level=AssertionLevel.error);
else
assert(m_flow_ref_set > 0, "Invalid coefficeint for m_flow_ref_set. Default value 0 shall not be used", level=AssertionLevel.error);
end if;

//Calculate reference mass flow from reference volume flow
m_flow_ref = V_flow_ref*rho_ref;

Expand Down
31 changes: 22 additions & 9 deletions ThermofluidStream/Undirected/FlowControl/SpecificValveType.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ model SpecificValveType "Specific technical valve types"

import FlowCoeffType = ThermofluidStream.FlowControl.Internal.Types.FlowCoefficientTypes;

replaceable record ZetaValueRecord = ThermofluidStream.FlowControl.Internal.Curves.SlideValveZetaCurve
replaceable record ZetaValueRecord =
ThermofluidStream.FlowControl.Internal.Curves.SlideValveZetaCurve
constrainedby ThermofluidStream.FlowControl.Internal.Curves.PartialCharacteristicZetaCurves "Select valve type"
annotation(choicesAllMatching = true, Dialog(group = "Valve parameters"));

Expand All @@ -15,14 +16,14 @@ model SpecificValveType "Specific technical valve types"
parameter Modelica.Units.SI.Diameter d_valve "Flow diameter"
annotation (Evaluate=true, Dialog(group="Valve parameters", enable=(flowCoefficient== FlowCoeffType.flowDiameter)));
//Reference Values
parameter Real Kvs(unit = "m3/h") "Kvs-value (metric) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Kvs)));
parameter Real Cvs_US "Cvs-value (US [gal/min]) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_US)));
parameter Real Cvs_UK "Cvs-value (UK [gal/min]) from data sheet (valve fully open)"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_UK)));
parameter SI.MassFlowRate m_flow_ref_set "Set reference mass flow in kg/s"
annotation(Evaluate = true, Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.m_flow_set)));
parameter Real Kvs(unit = "m3/h") = 0 "Kvs-value (metric) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Kvs)));
parameter Real Cvs_US = 0 "Cvs-value (US [gal/min]) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_US)));
parameter Real Cvs_UK = 0 "Cvs-value (UK [gal/min]) from data sheet (valve fully open)"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.Cvs_UK)));
parameter SI.MassFlowRate m_flow_ref_set = 0 "Reference mass flow rate"
annotation(Dialog(group = "Valve parameters",enable = (flowCoefficient ==FlowCoeffType.m_flow_set)));

protected
constant ZetaValueRecord valveData;
Expand All @@ -47,6 +48,18 @@ protected
else m_flow_ref_set/rho_ref "Reference volume flow rate";

equation
//this if clause shall ensure that valid parameters have been entered
if flowCoefficient == FlowCoeffType.Kvs then
assert(Kvs > 0, "Invalid coefficeint for Kvs. Default value 0 shall not be used", level=AssertionLevel.error);
elseif flowCoefficient == FlowCoeffType.Cvs_US then
assert(Cvs_US > 0, "Invalid coefficeint for Cvs_US. Default value 0 shall not be used", level=AssertionLevel.error);
elseif flowCoefficient == FlowCoeffType.Cvs_UK then
assert(Cvs_UK > 0, "Invalid coefficeint for Cvs_UK. Default value 0 shall not be used", level=AssertionLevel.error);
else
assert(m_flow_ref_set > 0, "Invalid coefficeint for m_flow_ref_set. Default value 0 shall not be used", level=AssertionLevel.error);
end if;


//Calculate reference mass flow rate from reference volume flow rate
m_flow_ref = V_flow_ref*rho_ref;

Expand Down
Loading