BioSTEAMDevelopmentGroup / thermosteam

BioSTEAM's Premier Thermodynamic Engine
Other
57 stars 12 forks source link

Load chemicals/streams from YAML/JSON #13

Closed yoelcortes closed 4 years ago

yoelcortes commented 4 years ago

Currently we define all chemicals and streams in a module, but it would also be nice to load them from a json or yaml file. For example, let's say we have a yaml file that looks like this:

Chemicals:
  Water:
  Ethanol:
  O2:
    phase: gas
  Cellulose:
    search_db: False
    phase: solid
    formula: C6H10O5
    Hf: -975708.8
    default: True
  Octane:
Synonyms: 
  Water: H2O
  Ethanol: 
    - CH3CH2OH
    - EthylAlcohol
Streams:
  process_water:
    Water: 500
    units: kg/hr
    price: 0.00035
  gasoline:
    Octane: 400
    units: kg/hr
    price: 0.756

We could then load the file with thermosteam in just one line, and create a ThermoData object to help creating only the objects we want. The new ThermoData object would enable users to extend the chemicals before creating streams, or choose which chemicals or streams to create. For example, we could create chemicals as follows:

>>> import thermosteam as tmo
>>> thermo_data = tmo.ThermoData.from_yaml(file_path) # A from_json method would also be available
>>> thermo_data.create_chemicals() 
CompiledChemicals([Water, Ethanol, O2, Cellulose])
>>> # Or alternatively, just load a few
>>>  thermo_data.create_chemicals(['Water', 'Cellulose']) 
CompiledChemicals([Water, Cellulose])

This would make creating thermodynamic property package much cleaner. For comparison, in a python module, we would have created these chemicals as follows:

import thermosteam as tmo
chemicals = tmo.Chemicals(['Water', 'Ethanol', 'O2'])
Cellulose = Chemical.blank('Cellulose', phase='s', formula='C6H10O5', Hf=-975708.8)
Cellulose.default()
chemicals.append(Cellulose)
chemicals.O2.at_state('g')
chemicals.compile()
chemicals.set_synonym('Water', 'H2O')

As for streams, we could create them in a similar fashion:

>>> # To start creating streams; we need to set the thermo package
>>> tmo.settings.set_thermo(chemicals)
>>> # Now we can create all the streams
>>> streams = thermo_data.create_streams()
>>> streams
[<Stream: process_water>, <Stream: gasoline>]
>>> # Alternatively, choose to create the streams we want
>>> gasoline = thermo_data.create_streams('gasoline')
>>> gasoline.show(flow='kg/hr')
Stream: gasoline
 phase: 'l', T: 298.15 K, P: 101325 Pa
 flow (kg/hr): Octane  400

Hopefully this can be done within the next week or two. I think it would be very helpful for sharing the data we use in thermosteam (instead of sharing python modules), and would make collaboration even easier. Thumbs up if you feel this would be a good addition :-) @Scheideba @yalinli2 @sarangbhagwat

Scheideba commented 4 years ago

I am for it.

On Tue, May 5, 2020 at 6:09 PM Yoel notifications@github.com wrote:

Instead of defining all chemicals and streams in a module, it would be nice to load them from a json or yaml files. For example, let's say we have a yaml file that looks like this:

Chemicals:

  • Water: synonyms:
    • H2O
  • Ethanol
  • O2: phase: gas
  • Cellulose: phase: solid formula: C6H10O5 Hf: -975708.8 default: True
  • OctaneStreams:
  • process_water: Water: 500 units: kg/hr price: 0.00035
  • gasoline: Octane: 400 units: kg/hr price: 0.756

We could then load the file with thermosteam in just one line, and create a ProcessData object to help creating only the object we want. The new ProcessData object would enable users to extend chemicals before creating streams, or choose which chemicals or streams to create. For example, we could create chemicals as follows:

import thermosteam as tmo>>> process_data = tmo.load_yaml(file_path) # A load_json function would also be available>>> process_data.create_chemicals() CompiledChemicals([Water, Ethanol, O2, Cellulose])>>> # Or alternatively, just load a few>>> process_data.create_chemicals(['Water', 'Cellulose']) CompiledChemicals([Water, Cellulose])

This would make creating thermodynamic property package much cleaner. For comparison, in a python module, we would have created these chemicals as follows:

import thermosteam as tmo chemicals = tmo.Chemicals(['Water', 'Ethanol', 'O2']) Cellulose = Chemical.blank('Cellulose', phase='s', formula='C6H10O5', Hf=-975708.8) Cellulose.default() chemicals.append(Cellulose) chemicals.O2.at_state('g') chemicals.compile() chemicals.set_synonym('Water', 'H2O')

As for streams, we could create them in a similar fashion:

To start creating streams; we need to set the thermo package>>> tmo.settings.set_thermo(chemicals)>>> # Now we can create all the streams>>> streams = process_data.create_streams()>>> streams

[, ]>>> # Alternatively, choose to create the streams we want>>> gasoline = process_data.create_streams('gasoline)>>> gasoline.show(flow='kg/hr') Stream: gasoline phase: 'l', T: 298.15 K, P: 101325 Pa flow (kg/hr): Octane 400

Hopefully this can be done within the next week or two. I think it would be very helpful for sharing the data we use in thermosteam (instead of sharing python modules), and would make collaboration even easier. Thumbs up if you feel this would be a good addition :-) @Scheideba https://github.com/Scheideba @yalinli2 https://github.com/yalinli2 @sarangbhagwat https://github.com/sarangbhagwat

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/BioSTEAMDevelopmentGroup/thermosteam/issues/13, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADI55Z3YZLAQYSOFMKXZJKTRQC2ERANCNFSM4M2BC7NA .

Scheideba commented 4 years ago

Also yaml is a superset of json so if you get it working with json then there is a possibility you can also just run a yaml file through the same way.

On Tue, May 5, 2020 at 6:31 PM Brent Scheidemantle scheideba@gmail.com wrote:

I am for it.

On Tue, May 5, 2020 at 6:09 PM Yoel notifications@github.com wrote:

Instead of defining all chemicals and streams in a module, it would be nice to load them from a json or yaml files. For example, let's say we have a yaml file that looks like this:

Chemicals:

  • Water: synonyms:
    • H2O
  • Ethanol
  • O2: phase: gas
  • Cellulose: phase: solid formula: C6H10O5 Hf: -975708.8 default: True
  • OctaneStreams:
  • process_water: Water: 500 units: kg/hr price: 0.00035
  • gasoline: Octane: 400 units: kg/hr price: 0.756

We could then load the file with thermosteam in just one line, and create a ProcessData object to help creating only the object we want. The new ProcessData object would enable users to extend chemicals before creating streams, or choose which chemicals or streams to create. For example, we could create chemicals as follows:

import thermosteam as tmo>>> process_data = tmo.load_yaml(file_path) # A load_json function would also be available>>> process_data.create_chemicals() CompiledChemicals([Water, Ethanol, O2, Cellulose])>>> # Or alternatively, just load a few>>> process_data.create_chemicals(['Water', 'Cellulose']) CompiledChemicals([Water, Cellulose])

This would make creating thermodynamic property package much cleaner. For comparison, in a python module, we would have created these chemicals as follows:

import thermosteam as tmo chemicals = tmo.Chemicals(['Water', 'Ethanol', 'O2']) Cellulose = Chemical.blank('Cellulose', phase='s', formula='C6H10O5', Hf=-975708.8) Cellulose.default() chemicals.append(Cellulose) chemicals.O2.at_state('g') chemicals.compile() chemicals.set_synonym('Water', 'H2O')

As for streams, we could create them in a similar fashion:

To start creating streams; we need to set the thermo package>>> tmo.settings.set_thermo(chemicals)>>> # Now we can create all the streams>>> streams = process_data.create_streams()>>> streams

[, ]>>> # Alternatively, choose to create the streams we want>>> gasoline = process_data.create_streams('gasoline)>>> gasoline.show(flow='kg/hr') Stream: gasoline phase: 'l', T: 298.15 K, P: 101325 Pa flow (kg/hr): Octane 400

Hopefully this can be done within the next week or two. I think it would be very helpful for sharing the data we use in thermosteam (instead of sharing python modules), and would make collaboration even easier. Thumbs up if you feel this would be a good addition :-) @Scheideba https://github.com/Scheideba @yalinli2 https://github.com/yalinli2 @sarangbhagwat https://github.com/sarangbhagwat

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/BioSTEAMDevelopmentGroup/thermosteam/issues/13, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADI55Z3YZLAQYSOFMKXZJKTRQC2ERANCNFSM4M2BC7NA .

yoelcortes commented 4 years ago

@Scheideba Yeah, reading yaml or json is pretty much the same thing in python, they both return lists and dictionaries. The only difference is the package you use to load the file jaja

yalinli2 commented 4 years ago

Sounds like a great idea for the long run, it makes chemical properties much more transparent and can probably avoid unknowingly inheriting strange properties from database.

Though I'd hope to retain the ability to create chemicals like we've been doing, at least for now... I've never used YAML/JSON and I'm not sure how big a change this will be. I guess I'll learn from your lipidcane/cornstover biorefineries :)

yoelcortes commented 4 years ago

@yalinli2 The current way of creating chemicals will still stay the same. The internal process of creating chemicals from json will actually rely on it. So we'd still be looking up the database and creating the models through thermosteam. Its more of a new feature jaja

sarangbhagwat commented 4 years ago

For it too. In case there are issues, can we retain the ability to define chemicals in a module after this change?

yoelcortes commented 4 years ago

@sarangbhagwat , same question as yalin jaja, you can checkout my previous comment

sarangbhagwat commented 4 years ago

Just saw, should have read through the thread lol. Sounds good!

yalinli2 commented 4 years ago

Cool! Then I'll look forward to it! @yoelcortes

yoelcortes commented 4 years ago

Done! just pip install the latest version of thermosteam: https://thermosteam.readthedocs.io/en/latest/ThermoData.html