11import numpy as np
22from scipy .constants import atm , zero_Celsius
33
4- from ..mathutils .function import Function
4+ from ..mathutils .function import NUMERICAL_TYPES , Function
55from ..plots .fluid_plots import _FluidPlots
66from ..prints .fluid_prints import _FluidPrints
77
88
99class Fluid :
10- """Class that represents a fluid object and its simple quantities ,
10+ """Class that represents a fluid object and its attributes ,
1111 such as density.
1212
1313 Attributes
@@ -16,6 +16,7 @@ class Fluid:
1616 Name of the fluid.
1717 density : int, float, callable, string, array, Function
1818 Input density of the fluid in kg/m³.
19+ Used internally to compute the density_function.
1920 density_function : Function
2021 Density of the fluid as a function of temperature and pressure.
2122 """
@@ -44,6 +45,26 @@ def __init__(self, name, density):
4445 self .prints = _FluidPrints (self )
4546 self .plots = _FluidPlots (self )
4647
48+ @property
49+ def density (self ):
50+ """Density of the fluid from the class parameter input."""
51+ return self ._density
52+
53+ @density .setter
54+ def density (self , value ):
55+ """Setter of the density class parameter."""
56+ if isinstance (value , NUMERICAL_TYPES ):
57+ # Numerical value kept for retro-compatibility
58+ self ._density = value
59+ else :
60+ self ._density = Function (
61+ value ,
62+ interpolation = "linear" ,
63+ extrapolation = "natural" ,
64+ inputs = ["Temperature (K)" , "Pressure (Pa)" ],
65+ outputs = "Density (kg/m³)" ,
66+ )
67+
4768 @property
4869 def density_function (self ):
4970 """Density of the fluid as a function of temperature and pressure."""
@@ -60,7 +81,7 @@ def density_function(self, value):
6081 Density of the fluid in kg/m³ as a function of temperature
6182 and pressure. The value is used as a ``Function`` source.
6283 """
63- if isinstance (value , ( int , float , np . number ) ):
84+ if isinstance (value , NUMERICAL_TYPES ):
6485 # pylint: disable=unused-argument
6586 def density_function (temperature , pressure ):
6687 return value
@@ -91,33 +112,35 @@ def get_time_variable_density(self, temperature, pressure):
91112 Function
92113 Density of the fluid in kg/m³ as function of time.
93114 """
94- if callable (temperature .source ) and callable (pressure .source ):
115+ is_temperature_callable = callable (temperature .source )
116+ is_pressure_callable = callable (pressure .source )
117+ if is_temperature_callable and is_pressure_callable :
95118 return Function (
96119 lambda time : self .density_function .get_value (
97120 temperature .source (time ), pressure .source (time )
98121 ),
99122 inputs = "Time (s)" ,
100123 outputs = "Density (kg/m³)" ,
101124 )
125+
126+ if is_temperature_callable or is_pressure_callable :
127+ time_scale = (
128+ temperature .x_array if not is_temperature_callable else pressure .x_array
129+ )
102130 else :
103131 time_scale = np .unique (
104132 np .concatenate ((temperature .x_array , pressure .x_array ))
105133 )
106- density_time = self .density_function .get_value (
107- temperature .get_value (time_scale ), pressure .get_value (time_scale )
108- )
109- return Function (
110- np .column_stack (
111- (
112- time_scale ,
113- density_time ,
114- )
115- ),
116- interpolation = "linear" ,
117- extrapolation = "constant" ,
118- inputs = "Time (s)" ,
119- outputs = "Density (kg/m³)" ,
120- )
134+ density_time = self .density_function .get_value (
135+ temperature .get_value (time_scale ), pressure .get_value (time_scale )
136+ )
137+ return Function (
138+ np .column_stack ((time_scale , density_time )),
139+ interpolation = "linear" ,
140+ extrapolation = "constant" ,
141+ inputs = "Time (s)" ,
142+ outputs = "Density (kg/m³)" ,
143+ )
121144
122145 def __repr__ (self ):
123146 """Representation method.
@@ -143,15 +166,13 @@ def __str__(self):
143166
144167 def to_dict (self , ** kwargs ): # pylint: disable=unused-argument
145168 discretize = kwargs .get ("discretize" , False )
146- if not isinstance (self .density , (int , float , np .number )):
147- density = self .density_function
148- else :
149- density = self .density
150169
170+ density = self .density
151171 if discretize and isinstance (density , Function ):
152172 density = density .set_discrete (
153- ((100 , atm * 0.9 ), (zero_Celsius + 30 , atm * 100 )),
154- (25 , 25 ),
173+ lower = (100 , atm * 0.9 ),
174+ upper = (zero_Celsius + 30 , atm * 50 ),
175+ samples = (25 , 25 ),
155176 mutate_self = False ,
156177 )
157178
0 commit comments