API¶
Module for fluid property diagram creation.
This file is part of project fluprodia (github.com/fwitte/fluprodia). It’s copyrighted by the contributors recorded in the version control history of the file, available from its original location src/fluprodia/fluid_property_diagram.py
SPDX-License-Identifier: MIT
- class fluprodia.fluid_property_diagram.FluidPropertyDiagram(fluid, backend=None)[source]¶
Bases:
objectShort summary.
- Parameters:
fluid (str) – Fluid for diagram.
width (float) – Width of all diagrams (default value:
width=16.0).height (float) – Height of all diagrams (default value:
height=10.0).
Example
This is a small example of how to create a fluid property dataset for water and export to Ts-, hs- and logph-diagram.
>>> from fluprodia import FluidPropertyDiagram >>> import matplotlib.pyplot as plt >>> import numpy as np >>> diagram = FluidPropertyDiagram('water')
After object creation it is possible to specify isolines. There are default isolines available, but these might not suit your requirements. We will define temperature and enthalpy isolines for this case. Before that, we need to specify the units if we do not want to use SI units. For available units see the
fluprodia.fluid_property_diagram.FluidPropertyDiagram.set_unit_system()documentation.>>> diagram.set_unit_system(T='°C', p='MPa', s='kJ/kgK', h='kJ/kg') >>> iso_T = np.arange(50, 701, 50) >>> iso_h = np.arange(0, 3601, 200) >>> diagram.set_isolines(T=iso_T, h=iso_h)
Now we can calculate the diagram data and create the diagram, the data should be plotted to. If you want to plot additional data to your diagram, first call the draw_isolines method, and then plot your data.
>>> diagram.calc_isolines()
After that it is possible to specify the view range of the plot and draw the isolines of a specific type of diagram. Last step is to export your diagram. Any file format supported by matplotlib is possible.
>>> fig, ax = plt.subplots(1) >>> diagram.draw_isolines(diagram_type='Ts', fig=fig, ax=ax, x_min=0, x_max=8, y_min=0, y_max=700) >>> plt.tight_layout() >>> fig.savefig('Ts_Diagramm.pdf')
If we want to create a different diagram, e.g. hs-diagram, it is not necessary to recalculate the isolines. Instead, create a new figure and draw the isolines for a different diagram.
>>> fig, ax = plt.subplots(1, figsize=(8, 5)) >>> diagram.draw_isolines(diagram_type='hs', fig=fig, ax=ax, x_min=0, x_max=8, y_min=0, y_max=3600) >>> plt.tight_layout() >>> fig.savefig('hs_Diagramm.pdf')
>>> fig, ax = plt.subplots(1, figsize=(8, 5)) >>> diagram.draw_isolines(diagram_type='logph', fig=fig, ax=ax, x_min=0, x_max=3600, y_min=1e-2, y_max=5e2) >>> plt.tight_layout() >>> fig.savefig('logph_Diagramm.pdf')
It is also possible to specify/modify the isolines to plot. For example, the lines of constant specific enthalpy should be plotted in red color and with a linewidth of 2. Also, the lines of constant specific volumen should not be plotted at all. For more information see the
fluprodia.fluid_property_diagram.FluidPropertyDiagram.draw_isolines()method.>>> fig, ax = plt.subplots(1) >>> diagram.draw_isolines( ... diagram_type='Ts', fig=fig, ax=ax, ... isoline_data={ ... 'h': { ... 'values': iso_h, ... 'style': {'linewidth': 2, 'color': '#ff0000'} ... }, 'vol': {'values': np.array([])}}, ... x_min=0, x_max=8, y_min=0, y_max=700 ... ) >>> plt.tight_layout() >>> fig.savefig('Ts_Diagramm.pdf')
It is also possible to export the data of a diagram:
>>> diagram.to_json("./tmp/water.json")
And, if you have your data ready to use, you can import the data again like this:
>>> diagram = FluidPropertyDiagram.from_json("tmp/water.json") >>> diagram.fluid 'water' >>> len(diagram.pressure["isolines"]) 20
- calc_individual_isoline(isoline_property=None, isoline_value=None, isoline_value_end=None, starting_point_property=None, ending_point_property=None, starting_point_value=None, ending_point_value=None)[source]¶
Return data points of an individual isoline within.
Pass the isoline type, its value in the diagrams unit system, as well as the start and the endpoint of the line. Styling can be changed using the line_style property.
- Parameters:
isoline_property (str) – Type of the isoline. Choose from
line_type='...':pressure (
'p')specific volume (
'vol')temperature (
'T')enthalpy (
'h')entropy (
's')
isoline_value (float) – Value of the isoline specified in the respective unit.
starting_point (dict) – Dictionary holding the starting property and its value in the unit used for plotting the diagram, e.g.
starting_property{'p': 1e5}ending_point (dict) – Dictionary holding the ending property and its value in the unit used for plotting the diagram, e.g.
ending_property{'p': 1e4}
- Returns:
datapoints (dict) – Dictionary holding the isoline datapoints for:
pressure (key=:code:’p’)
specific volume (key=:code:’vol’)
temperature (key=:code:’T’)
enthalpy (key=:code:’h’)
entropy (key=:code:’s’)
Example
A full example can be found in the class documentation.
- convert_from_SI(value, property)[source]¶
Convert a value from SI to the active unit.
Delegates to
fluprodia._units.Units.from_SI(). Works on scalars and NumPy arrays alike.
- convert_to_SI(value, property)[source]¶
Convert a value from the active unit to SI.
Delegates to
fluprodia._units.Units.to_SI(). Works on scalars and NumPy arrays alike.
- draw_isolines(fig, ax, diagram_type, x_min, x_max, y_min, y_max, isoline_data=None, latex_units=True)[source]¶
Draw the isolines onto an axes within a matplotlib figure
- Parameters:
fig (matplotlib.pyplot.figure) – Figure to draw into
ax (matplotlib.pyplot.axes) – Axes to draw into
diagram_type (str) – Name of the diagram
x_min (number) – Minimum for x range
x_max (number) – Maximum for x range
y_min (number) – Minimum for y range
y_max (number) – Maximum for y range
isoline_data (dict, optional) – Dictionary holding additional data on the isolines to be drawn, by default None. These are per isoline type
the isoline values with key
values,the isoline style with key
style,the number of labels per isoline
labels_per_line(default: 1) andlabel every n-th line only
label_every_nth(default: 1)
following this structure: {“Q”: {“values”: np.array([0.0, 1.0])}}
The islonline style is another dictionary holding keyword arguments of a
matplotlib.lines.Line2Dobject. See https://matplotlib.org/stable/api/_as_gen/matplotlib.lines.Line2D.html for more information.latex_units (bool, optional) – Axis and isoline labels using LaTeX style units, by default True
- draw_isolines_plotly(diagram_type, x_min, x_max, y_min, y_max, isoline_data=None)[source]¶
Draw isolines and return a
plotly.graph_objects.Figure.The interface mirrors
draw_isolines(). Isoline styles are specified with the same matplotlib-style keys (color,linewidth,linestyle) and are mapped to plotly equivalents internally.- Parameters:
diagram_type (str) – Name of the diagram, same choices as for
draw_isolines().x_min (number) – Minimum for x range.
x_max (number) – Maximum for x range.
y_min (number) – Minimum for y range.
y_max (number) – Maximum for y range.
isoline_data (dict, optional) – Same structure as for
draw_isolines(). Thestylesub-dict acceptscolor,linewidthandlinestyle(matplotlib keys; mapped to plotlyline.color,line.widthandline.dashrespectively).
- Returns:
plotly.graph_objects.Figure – Fully configured figure; add further traces to overlay custom data (e.g. state points from a cycle simulation).
- set_isolines(**kwargs)[source]¶
Set the isolines.
- Parameters:
p (ndarray) – Isolines for pressure.
T (ndarray) – Isolines for temperature.
Q (ndarray) – Isolines for vapor mass fraction.
s (ndarray) – Isolines for specific entropy.
h (ndarray) – Isolines for specific enthalpy.
vol (ndarray) – Isolines for specific volume.
- set_unit_system(units=None, **kwargs)[source]¶
Set the unit system for the fluid properties.
Delegates to
fluprodia._units.Units.set_defaults(). Any pint-compatible unit string that is dimensionally consistent with the property is accepted.Both short-form keys (
p,T,vol,h,s,Q) and long-form keys (pressure,temperature,volume,enthalpy,entropy,quality) are supported.A
tespy.tools.units.Unitsinstance (or any object with adefaultattribute that is a plaindict) may be passed as the first positional argument. Keyword arguments are merged on top and take precedence. tespy properties that have no fluprodia equivalent (e.g.mass_flow,power) are silently ignored.- Parameters:
units (object, optional) – An object with a
defaultdict, e.g.network.unitsfrom a tespy simulation. Its entries are used as base values; any**kwargsoverride them.p / pressure (str) – Unit of pressure, e.g.
'Pa','bar','MPa'.T / temperature (str) – Unit of temperature, e.g.
'K','°C','°F'.s / entropy (str) – Unit of specific entropy, e.g.
'J/kgK','kJ/kgK'.h / enthalpy (str) – Unit of specific enthalpy, e.g.
'J/kg','kJ/kg'.vol / volume / specific_volume (str) – Unit of specific volume, e.g.
'm^3/kg','l/kg'.Q / quality (str) – Unit of vapour quality, e.g.
'-','%'.