BioSTEAMDevelopmentGroup / thermosteam

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

AttributeError: 'CompiledChemicals' object has no attribute 'IDs' #8

Closed yalinli2 closed 4 years ago

yalinli2 commented 4 years ago

Description See the two sets of codes below, I'm not sure why the first one works fine but the second one would raise the error as in the title

To Reproduce First define the function needed for both sets of codes:

import thermosteam as tmo

def append_new_single_phase_chemical(ID, chems, *sources, **data):
    chemical = tmo.Chemical.blank(ID, **data)
    chemical.copy_missing_slots_from(*sources)
    try: chemical.at_state(phase=chemical.phase_ref)
    except: pass
    chems.append(chemical)

Below codes work fine

chems_1 = tmo.Chemicals([])
append_new_single_phase_chemical('chemical_1', chems=chems_1)
chems_1.chemical_1.MW = 1
chems_1.chemical_1.phase_ref = 's'
tmo.settings.set_thermo(chems_1)

But below ones will raise the error

chems_2 = tmo.Chemicals([])
append_new_single_phase_chemical('chemical_2', chems=chems_2, MW=1, phase_ref='s')
tmo.settings.set_thermo(chems_2)

Screenshot

Error

Environment OS: macOS Catalina 10.15.3 thermosteam: 0.3.0 python: 3.7.1

Thanks!!!

yalinli2 commented 4 years ago

Addition I further found that for the second set of codes, if I use:

append_new_single_phase_chemical('chemical_2', chems=chems_2, MW=1)

(i.e., do not set the phase_ref), then it works fine.

Alternatively, if I do not set the phase in the append_new_single_phase_chemical function, then no error will be triggered in the second set of code.

Or, if I keep the function unchanged, still use the second set of code, but change phase_ref='s' to phase_ref='l' , then no error will be triggered.

yoelcortes commented 4 years ago

An error about missing properties is first raised, but then there is an internal error with thermosteam that prevents the original error from showing up. I just fixed this bug in thermosteam.

I suggest you run <Chemicals>.compile() before you use set_thermo. The set_thermo method forces the input to become a Thermo object, so it might be easier to debug if you compile the chemicals first.

Also, the try-exception block with the at_state method may be stopping some important errors from showing up. You may want to check that out too.

yalinli2 commented 4 years ago

Great suggestions! The compile function is super helpful, and I got rid of the try-exception block.

A little suggestion that you can see whether it makes sense: in the following error message, it will be even more awesome if "key thermodynamic properties" can be explicitly defined, maybe add parentheses like "key thermodynamic properties (need MW, Cn, V, blah blah... for compiling)". The message doesn't need to be for the specific chemical object. Just to give the user a clue on what to look for from <Chemical>.get_missing_slots() output (since there can be a lot of missing properties but not all of them are needed).

Though what properties are needed will certainly depend on what process the user will simulate (missing Tb will not prevent compiling, but it can cause problems if modeling a something like a Distillation unit)...

Error message

Thanks!

yoelcortes commented 4 years ago

For sure! I'm still thinking of some good algorithms to figure out which are the key thermodynamic properties that are needed depending on the chemical, and give an error message that lists out which are the ones that are missing (instead of telling you use get_missing_slots). ~Feel free to email me a reminder if I dont get to it within a couple of weeks.~ I just implemented a temporary algorithm for this in the latest update. Now you should get the properties listed out. Tb and Psat are also key properties when the phase is not locked and its not a solid.

yalinli2 commented 4 years ago

Yep the algorithm is working! Thanks!!!