Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating units in OLI Flash #1369

Closed
adam-a-a opened this issue Apr 25, 2024 · 0 comments · Fixed by #1388
Closed

Updating units in OLI Flash #1369

adam-a-a opened this issue Apr 25, 2024 · 0 comments · Fixed by #1388
Assignees
Labels
bug Something isn't working Discussion oli Priority:Normal Normal Priority Issue or PR

Comments

@adam-a-a
Copy link
Contributor

adam-a-a commented Apr 25, 2024

Description

While attempting to alter units for properties to be used for calculation or to be returned as output, the user needs to set the oli_unit desired, which is part of a FixedKeysDict. Additionally, there is a pyomo_unit value that also needs to be updated to match the new oli_unit assigned. When changing units, the user shouldn't need to ensure that they update pyomo units in addition to setting the oli units.

For example,

input_unit_set = FixedKeysDict({
        "molecularConcentration": {
            "oli_unit": "mg/L",
            "pyomo_unit": pyunits.mg / pyunits.L,
        }, ...})

If I attempt the following change,
input_unit_set['molecularConcentration']['oli_unit'] = "mol/L",
I should then be able to print(input_unit_set['molecularConcentration']['pyomo_unit']) and see that "mol/L" is returned. However, as it stands now, I would need to set input_unit_set['molecularConcentration']['pyomo_unit'] = pyunits.mol/pyunits/L afterwards to avoid an erroneous mismatch between stated oli units and pyomo units.

Another issue is that for a given property, there is a set of acceptable units that OLI will be able to work with. Selecting an appropriate set of units for properties can result in erroneous results (or no results returned at all, without exceptions being raised). Thus, we should have predefined sets of valid entries for units per property, and if an invalid unit is supplied to a particular property, an exception should be raised.

Expected Behavior

What I was trying to implement (unsuccessfully so far) was the ability to automatically update new pyomo units after manually providing new oli_units. For example, I start with these defaults:

input_unit_set = FixedKeysDict({
        "molecularConcentration": {
            "oli_unit": "mg/L",
            "pyomo_unit": pyunits.mg / pyunits.L,
        }, ...})

Later, if I attempt the following change,
input_unit_set['molecularConcentration']['oli_unit'] = "mol/L",
I should then be able to print(input_unit_set['molecularConcentration']['pyomo_unit']) and see that "mol/L" is returned.

Steps to Reproduce

from pyomo.environ import units as pyunits
# used to execute OLI Cloud functions
from watertap.tools.oli_api.flash import Flash
from watertap.tools.oli_api.credentials import CredentialManager
from watertap.tools.oli_api.client import OLIApi
import os

if __name__ == "__main__":
    credential_manager = CredentialManager(
        encryption_key=os.environ["OLI_secret"],
        config_file=path_to_credentials_file,
    )
   case= {
            "Na_+": 130680,
            "Cl_-": 351140,
            "Mg_2+": 65940,
            "Ca_2+": 21100,
            "K_+": 20240,
            "SO4_2-": 137250,
        }
      
    source_water = {
        "temperature": 298.15,
        "pressure": 101325,
        "components": case,
        "units": {
            "temperature": pyunits.K,
            "pressure": pyunits.Pa,
            "molecularConcentration": pyunits.mg/pyunits.L,
        },
    }

    f = Flash( )
    print(f.input_unit_set["molecularConcentration"]["oli_unit"])
    print(f.input_unit_set["molecularConcentration"]["pyomo_unit"])
    
    # try setting new unit to oli_unit
    f.input_unit_set["molecularConcentration"]["oli_unit"] = "mg"
    # note that pyomo_unit will not match oli_unit, and user would have to manually correct
    print(f.input_unit_set["molecularConcentration"]["pyomo_unit"])

Environment

  • OS: win64
  • Python: v3.8
  • WaterTAP: v0.12

Anything Else?

The relative path to where the FixedKeysDict is defined, which is what I was tinkering with to address this issue: watertap\tools\oli_api\util\fixed_keys_dict.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Discussion oli Priority:Normal Normal Priority Issue or PR
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants