oemof / tespy

Thermal Engineering Systems in Python (TESPy). This package provides a powerful simulation toolkit for thermal engineering plants such as power plants, district heating systems or heat pumps.
https://tespy.readthedocs.io
MIT License
256 stars 80 forks source link

get_comps of class Network does not work prior to first solve #360

Closed fwitte closed 1 year ago

fwitte commented 1 year ago

Discussed in https://github.com/oemof/tespy/discussions/359

Originally posted by **GoldenPotatis** September 22, 2022 Hi! I'm a new user of TESPy and still learning. I found this small issue and would like to share with the community. I'm using Python3.8.9 and TESPy0.6.0 In the Network Class, it is possible to view the network connections and components by calling the `network.conns` attributes and `network.comps` attributes. The `network.conns` works perfectly fine. However, the user may find that `network.comps` does not work. It returns `AttributeError: 'Network' object has no attribute 'comps'`. The example I'm using is a simple condenser cooled by air, same as in the [Condenser example code in tespy.readthedocs](https://tespy.readthedocs.io/en/main/api/tespy.components.html#module-tespy.components.heat_exchangers.condenser): ```python from tespy.networks import Network from tespy.components import Source, Sink, Condenser from tespy.connections import Connection # create a network and components nw = Network(fluids=['water', 'air'], T_unit='C', p_unit='bar', h_unit='kJ / kg') air_in = Source(label='air inlet') air_out = Sink('air outlet') waste_steam = Source('waste steam') water = Sink('condensate water') condenser = Condenser('condenser') # create connections air_con = Connection(air_in, 'out1', condenser, 'in2', label='air to condenser') con_air = Connection(condenser, 'out2', air_out, 'in1', label='condenser to air') ws_con = Connection(waste_steam, 'out1', condenser, 'in1', label='ws to con') con_water = Connection(condenser, 'out1', water, 'in1') nw.add_conns(air_con, con_air, ws_con, con_water) ``` When calling the `nw.conns` attributes, it will return a Pandas DataFrame. But calling `nw.comps` will result in `AttributeError: 'Network' object has no attribute 'comps'`. ![image](https://user-images.githubusercontent.com/30263046/191722120-8add9f70-7cc3-42a1-8498-dc3b3a799390.png) Correspondingly, the `Network.get_comp()` doesn't work neither. After looking into the source code of Network, it is found that the `network.conns` is initiated at the beginning under `def __init__(self, fluids, memorise_fluid_properties=True, **kwargs)` and `def set_defaults(self)`. But `network.comps` is not initiated. The first time `network.comps` is initiated is actually under `def check_network(self)`, which is: ```python comps = pd.unique(self.conns[['source', 'target']].values.ravel()) # build the dataframe for components ``` After running `nw.check_network()`, the `nw.comps` then works perfectly fine, which gives a dataframe of the components. ![image](https://user-images.githubusercontent.com/30263046/191725300-efe870af-7092-41bd-a7fe-d511626e09e6.png) It is therefore suggested to initiate the `.comps` at the beginning of the code to avoid such error. Another attached question is under the example code of Condenser in readthedocs, why the air inlet is Sink and air outlet is Source? ```python amb_in = Sink('ambient air inlet') amb_out = Source('air outlet') ``` Based on the definition of Source and Sink, should it be like air inlet is Source and air outlet is Sink?
fwitte commented 1 year ago

Hi @goldenpotatis,

I made your suggestion an issue and will open a pr to fix this. You could do it yourself, if you are interested. If you need help with that, please let me know.

Thank you very much for you suggestions!

GoldenPotatis commented 1 year ago

Hi @GoldenPotatis,

I made your suggestion an issue and will open a pr to fix this. You could do it yourself, if you are interested. If you need help with that, please let me know.

Thank you very much for you suggestions!

Hi Francesco, yes I can try to fix it, thanks for opening it for me!

fwitte commented 1 year ago

@GoldenPotatis: I fixed the issue in #362 and merged it in dev already. It is now possible to access all components, which were part of the connections added to the network even before solving the model. Thank you again for reporting the bug, cheers!

GoldenPotatis commented 1 year ago

Thanks! Sorry I was ill and could not fix it on time.

Best regards, Chang

On 2 Oct 2022, at 10:54, Francesco Witte @.***> wrote:

 @GoldenPotatis: I fixed the issue in #362 and merged it in dev already. It is now possible to access all components, which were part of the connections added to the network even before solving the model. Thank you again for reporting the bug, cheers!

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.

fwitte commented 1 year ago

Oh, dont worry :).

Get well soon!