Skip to content

Commit

Permalink
Revision of OLI Cloud Flash Calculations (#1237)
Browse files Browse the repository at this point in the history
* add credential manager to oli api

* fix incorrect call from credential manager

* run black

* Update watertap/core/util/tests/test_credential_manager.py

exception test for get_credentials()

Co-authored-by: Adam Atia <[email protected]>

* fix cryptography module import

* run black on setup.py

* add missing get_login_status args

* Improving credential manager testing

* Helper functions for OLI/MCAS

* Run black

* Add test for MCAS flowsheet method

* watertap_to_oli tests

* run black

* Merge in code demo and required changes

* Add OLI API tests, minor fixes

* Fix temp OLIApi client tests

* Improved formatting and documentation

* add missing __init__.py file

* initial revisions to OLI flash calcs

* WIP - isothermal flash composition surveys

* move all features to Flash class

* isothermal composition survey methods

* update examples

* move tutorials

* flash tests and documentation

* extract properties revisions

* remove water_analysis

* remove duplicate example notebook

* revised extract_properties

* flash calcs tutorial

* flash calcs tests

* black

* unused import fixes

* notebook test fix

* trying to fix RTD error

* remove cryptography from setup.py dev

Co-authored-by: bknueven <[email protected]>

* minor flash fixes

* linting fix + documentation

* update test_flash.py

* replace some prints with logging

* black

* allow OLI access key expiry specification

* use datetime for OLI key timestamp

* black

* notebook and access key fixes

* notebook fix

* notebook and flash fixes

* access key generator fix

* credential management notes

* added bsaing info logging so usre knows what is going on

* Update credentials.py

* added flash info loggin

* fix isothermal flash bug

* refactor OLI API

* fixes for OLI tests

* removed extra flash test

* docstring fix

---------

Co-authored-by: Vecchiarelli <[email protected]>
Co-authored-by: Adam Atia <[email protected]>
Co-authored-by: bknueven <[email protected]>
Co-authored-by: OOAmusat <[email protected]>
Co-authored-by: avdudchenko <[email protected]>
  • Loading branch information
6 people authored Feb 2, 2024
1 parent 4fd16dd commit ab11c2d
Show file tree
Hide file tree
Showing 13 changed files with 1,646 additions and 1,325 deletions.
341 changes: 341 additions & 0 deletions tutorials/incorporating_oli_calculations.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,341 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"scrolled": true,
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"\n",
"# Incorporating OLI Calculations with WaterTAP\n",
"\n",
"#### Contact: Paul Vecchiarelli ([email protected])\n",
"\n",
"This tutorial will demonstrate basic usage of OLI Cloud calls using our custom API tools."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Rationale\n",
"\n",
" - Simulations for realistic water sources are mathematically complex: \n",
" > $ Interactions \\ge Cations * Anions$\n",
" - OLI improves WaterTAP approximations and offloads computational resources"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Required OLI API Inputs\n",
"\n",
"\n",
" - State variables (solute concentrations, temperature, pressure), which can beextracted from a state block\n",
" \n",
" - Login credentials\n",
" \n",
" - A chemistry (*.dbs) file\n",
" - establishes state variables, phases, etc. to be considered in flash calls"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"# used to specify state/convert variables\n",
"from pyomo.environ import units as pyunits\n",
"\n",
"# used to build survey lists\n",
"from numpy import linspace\n",
"\n",
"# used to execute OLI Cloud functions\n",
"from watertap.tools.oli_api.flash import Flash\n",
"from watertap.tools.oli_api.credentials import CredentialManager\n",
"from watertap.tools.oli_api.client import OLIApi"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 1. Specify State Variables.\n",
"\n",
"- This data is used to construct inputs to OLI Cloud"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"source_water = {\n",
" \"temperature\": 298.15,\n",
" \"pressure\": 101325,\n",
" \"components\": {\n",
" \"Cl_-\": 870,\n",
" \"Na_+\": 739,\n",
" \"SO4_2-\": 1011,\n",
" \"Mg_2+\": 90,\n",
" \"Ca_2+\": 258,\n",
" \"K_+\": 9,\n",
" \"HCO3_-\": 385,\n",
" \"SiO2\": 30,\n",
" },\n",
" \"units\": {\n",
" \"temperature\": pyunits.K,\n",
" \"pressure\": pyunits.Pa,\n",
" \"components\": pyunits.mg / pyunits.L,\n",
" },\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 2. Initialize Flash Instance.\n",
"\n",
" - We will run most of our methods with this class"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"f = Flash()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 3. Get Survey Parameters.\n",
"\n",
" - In this example, we will generate a temperature sweep survey"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true,
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"survey_conditions = {\n",
" \"Temperature\": linspace(273, 373, 10),\n",
" \"SO4_2-\": linspace(0, 1e3, 10),\n",
" \"Ca_2+\": linspace(0, 1e3, 10),\n",
"}\n",
"\n",
"survey = f.build_survey(\n",
" survey_conditions,\n",
" get_oli_names=True,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 4. Login to OLI Cloud.\n",
"\n",
"- The following code demonstrates an OLI Cloud login:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"try:\n",
" credential_manager = CredentialManager(\n",
" username=\"\",\n",
" password=\"\",\n",
" root_url=\"\",\n",
" auth_url=\"\",\n",
" access_keys=[],\n",
" )\n",
"except (OSError, ConnectionError) as e:\n",
" print(e)\n",
" credential_manager = None"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 5. Create *.dbs File and 6. Get Output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"if credential_manager:\n",
" with OLIApi(credential_manager) as oliapi:\n",
" # create a new DBS file\n",
" dbs_file_id = oliapi.get_dbs_file_id(\n",
" chemistry_source=source_water[\"components\"],\n",
" phases=[\"liquid1\", \"solid\"],\n",
" )\n",
" \n",
" # create water analysis inputs\n",
" water_analysis_input = f.build_flash_calculation_input(\n",
" \"wateranalysis\",\n",
" state_vars,\n",
" )\n",
" \n",
" # create water analysis case\n",
" water_analysis_base_case = f.run_flash(\n",
" \"wateranalysis\",\n",
" oliapi,\n",
" dbs_file_id,\n",
" water_analysis_input,\n",
" file_name=\"water_analysis_singlepoint\"\n",
" )\n",
" \n",
" # create apparent composition from water analysis output\n",
" water_analysis_apparent_composition = f.build_flash_calculation_input(\n",
" \"isothermal\",\n",
" state_vars,\n",
" water_analysis_base_case[0],\n",
" )\n",
" \n",
" # run isothermal flash for the survey parameters\n",
" isothermal_survey_result = f.run_flash(\n",
" \"isothermal\",\n",
" oliapi,\n",
" dbs_file_id,\n",
" water_analysis_apparent_composition,\n",
" survey,\n",
" \"isothermal_composition_survey\",\n",
" )"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# 7. Extract Filtered Output\n",
"\n",
" - OLI's output is robust, so WaterTAP enables printing selected results:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# extract properties from raw oli output\n",
"properties = [\n",
" \"prescalingTendencies\",\n",
" \"entropy\",\n",
" \"gibbsFreeEnergy\",\n",
" \"selfDiffusivities\",\n",
" \"molecularConcentration\",\n",
" \"kValuesMBased\",\n",
"]\n",
"if credential_manager:\n",
" extracted_properties = f.extract_properties(\n",
" isothermal_survey_result,\n",
" properties,\n",
" filter_zero=True,\n",
" file_name=\"properties\",\n",
" )"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit ab11c2d

Please sign in to comment.