Modified reeps to grab charge from REE in xml file for initial chlorine moles calculation

pull/1/head
titusquah 5 years ago
parent 52c8efc05e
commit 2bb9b7c24a

@ -7,6 +7,7 @@
<w>equilibrate</w> <w>equilibrate</w>
<w>extractant</w> <w>extractant</w>
<w>kmol</w> <w>kmol</w>
<w>lmse</w>
<w>ndarray</w> <w>ndarray</w>
<w>pred</w> <w>pred</w>
<w>reeps</w> <w>reeps</w>

@ -2,11 +2,11 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="f4439dc0-6756-4612-8f7d-596d8949f300" name="Default Changelist" comment=""> <list default="true" id="f4439dc0-6756-4612-8f7d-596d8949f300" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/data/xmls/elementz.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/dictionaries/Titus.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dictionaries/Titus.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/data/xmls/twophase.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/tests/one_comp_settings.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/data/xmls/twophase.xml" beforeDir="false" afterPath="$PROJECT_DIR$/data/xmls/twophase.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/reeps.py" beforeDir="false" afterPath="$PROJECT_DIR$/reeps.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/reeps.py" beforeDir="false" afterPath="$PROJECT_DIR$/reeps.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tests/one_comp_settings.txt" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/tests/test_reeps.py" beforeDir="false" afterPath="$PROJECT_DIR$/tests/test_reeps.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/tests/test_reeps.py" beforeDir="false" afterPath="$PROJECT_DIR$/tests/test_reeps.py" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -37,20 +37,17 @@
<property name="run.code.analysis.last.selected.profile" value="aDefault" /> <property name="run.code.analysis.last.selected.profile" value="aDefault" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" /> <property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component> </component>
<component name="RunManager" selected="Python.test"> <component name="RunManager" selected="Python.test_reeps">
<configuration name="fit" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true"> <configuration name="surfmethods" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="parameter-estimation" /> <module name="parameter-estimation" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" /> <option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" /> <option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/../../parameter estimation/parameter estimation" /> <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/../../anl_box/Box Sync/titus/onboarding/codes/multi/1_fit/01_Surface/1_Nd" />
<option name="IS_MODULE_SDK" value="false" /> <option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" /> <option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" /> <option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/../../parameter estimation/parameter estimation/fit.py" /> <option name="SCRIPT_NAME" value="$PROJECT_DIR$/../../anl_box/Box Sync/titus/onboarding/codes/multi/1_fit/01_Surface/1_Nd/surfmethods.py" />
<option name="PARAMETERS" value="" /> <option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" /> <option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" /> <option name="EMULATE_TERMINAL" value="false" />
@ -63,6 +60,9 @@
<module name="parameter-estimation" /> <module name="parameter-estimation" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" /> <option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" /> <option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/../../parameter estimation/parameter estimation" /> <option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/../../parameter estimation/parameter estimation" />
<option name="IS_MODULE_SDK" value="false" /> <option name="IS_MODULE_SDK" value="false" />
@ -132,7 +132,7 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Python.test" /> <item itemvalue="Python.test_reeps" />
<item itemvalue="Python.test_reeps" /> <item itemvalue="Python.test_reeps" />
<item itemvalue="Python.test_reeps" /> <item itemvalue="Python.test_reeps" />
<item itemvalue="Python.test_reeps" /> <item itemvalue="Python.test_reeps" />
@ -208,7 +208,14 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1591119253733</updated> <updated>1591119253733</updated>
</task> </task>
<option name="localTasksCounter" value="7" /> <task id="LOCAL-00007" summary="Modified reeps to grab charge from REE in xml file for initial chlorine moles calculation">
<created>1591130305884</created>
<option name="number" value="00007" />
<option name="presentableId" value="LOCAL-00007" />
<option name="project" value="LOCAL" />
<updated>1591130305884</updated>
</task>
<option name="localTasksCounter" value="8" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">
@ -227,7 +234,8 @@
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value="started project" /> <MESSAGE value="started project" />
<MESSAGE value="Completed one composition parameter estimation" /> <MESSAGE value="Completed one composition parameter estimation" />
<option name="LAST_COMMIT_MESSAGE" value="Completed one composition parameter estimation" /> <MESSAGE value="Modified reeps to grab charge from REE in xml file for initial chlorine moles calculation" />
<option name="LAST_COMMIT_MESSAGE" value="Modified reeps to grab charge from REE in xml file for initial chlorine moles calculation" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state x="-1330" y="212" key="#Inspections" timestamp="1590787654691"> <state x="-1330" y="212" key="#Inspections" timestamp="1590787654691">
@ -246,10 +254,10 @@
<screen x="-1920" y="2" width="1920" height="1040" /> <screen x="-1920" y="2" width="1920" height="1040" />
</state> </state>
<state x="-1213" y="379" key="ANALYSIS_DLG_com.intellij.analysis.BaseAnalysisAction$1/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1590787657711" /> <state x="-1213" y="379" key="ANALYSIS_DLG_com.intellij.analysis.BaseAnalysisAction$1/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1590787657711" />
<state x="-1364" y="117" key="CommitChangelistDialog2" timestamp="1591119252191"> <state x="-1364" y="117" key="CommitChangelistDialog2" timestamp="1591130304224">
<screen x="-1920" y="2" width="1920" height="1040" /> <screen x="-1920" y="2" width="1920" height="1040" />
</state> </state>
<state x="-1364" y="117" key="CommitChangelistDialog2/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591119252191" /> <state x="-1364" y="117" key="CommitChangelistDialog2/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591130304224" />
<state x="-1828" y="94" width="1736" height="856" key="DiffContextDialog" timestamp="1591048879404"> <state x="-1828" y="94" width="1736" height="856" key="DiffContextDialog" timestamp="1591048879404">
<screen x="-1920" y="2" width="1920" height="1040" /> <screen x="-1920" y="2" width="1920" height="1040" />
</state> </state>
@ -290,10 +298,10 @@
<screen x="-1920" y="2" width="1920" height="1040" /> <screen x="-1920" y="2" width="1920" height="1040" />
</state> </state>
<state x="-1368" y="256" key="Vcs.Push.Dialog.v2/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591047662716" /> <state x="-1368" y="256" key="Vcs.Push.Dialog.v2/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591047662716" />
<state x="-1341" y="300" key="com.intellij.ide.util.TipDialog" timestamp="1591116828596"> <state x="-1341" y="300" key="com.intellij.ide.util.TipDialog" timestamp="1591192469960">
<screen x="-1920" y="2" width="1920" height="1040" /> <screen x="-1920" y="2" width="1920" height="1040" />
</state> </state>
<state x="-1341" y="300" key="com.intellij.ide.util.TipDialog/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591116828596" /> <state x="-1341" y="300" key="com.intellij.ide.util.TipDialog/0.0.1536.824/-1920.2.1920.1040@-1920.2.1920.1040" timestamp="1591192469960" />
<state x="463" y="236" key="com.intellij.ide.util.TipDialog/0.0.1536.824/-1920.2.1920.1040@0.0.1536.824" timestamp="1590784072879" /> <state x="463" y="236" key="com.intellij.ide.util.TipDialog/0.0.1536.824/-1920.2.1920.1040@0.0.1536.824" timestamp="1590784072879" />
<state x="579" y="298" key="com.intellij.ide.util.TipDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1591037480473" /> <state x="579" y="298" key="com.intellij.ide.util.TipDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1591037480473" />
<state x="769" y="438" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2" timestamp="1591037545358"> <state x="769" y="438" key="com.intellij.openapi.vcs.update.UpdateOrStatusOptionsDialogupdate-v2" timestamp="1591037545358">

@ -50,7 +50,7 @@
<const_cp Tmax="300.0" Tmin="298.0"> <const_cp Tmax="300.0" Tmin="298.0">
<t0 units="K">298.14999999999998</t0> <t0 units="K">298.14999999999998</t0>
<h0 units="J/mol" updated="Updated at 18:24:54">-4848809.807683906</h0> <h0 units="J/mol" updated="Updated at 11:29 6-3-2020">-4704703.645715787</h0>
<s0 units="J/mol/K"> 1117.965 </s0> <s0 units="J/mol/K"> 1117.965 </s0>
<cp0 units="J/mol/K">0.0</cp0> <cp0 units="J/mol/K">0.0</cp0>
</const_cp> </const_cp>

@ -1,4 +1,4 @@
import time from datetime import datetime
import cantera as ct import cantera as ct
import pandas as pd import pandas as pd
import numpy as np import numpy as np
@ -7,7 +7,8 @@ from scipy.optimize import minimize
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
import seaborn as sns import seaborn as sns
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import shutil
import copy
sns.set() sns.set()
@ -17,8 +18,7 @@ class REEPS:
Returns parameters for GEM Returns parameters for GEM
:param exp_csv_filename: (str) csv file name with experimental data :param exp_csv_filename: (str) csv file name with experimental data
:param phases_xml_filename: (str) xml file with parameters for equilibrium calc :param phases_xml_filename: (str) xml file with parameters for equilibrium calc
:param x_guess: (float) guess for multiplier variable :param opt_dict: (dict) optimize info {species:{thermo_prop:guess}
:param h_guess: (float) initial guess standard enthalpy (J/kmol)
:param phase_names: (list) names of phases in xml file :param phase_names: (list) names of phases in xml file
:param aq_solvent_name: (str) name of aqueous solvent in xml file :param aq_solvent_name: (str) name of aqueous solvent in xml file
:param extractant_name: (str) name of extractant in xml file :param extractant_name: (str) name of extractant in xml file
@ -34,8 +34,7 @@ class REEPS:
def __init__(self, def __init__(self,
exp_csv_filename, exp_csv_filename,
phases_xml_filename, phases_xml_filename,
x_guess, opt_dict,
h_guess,
phase_names, phase_names,
aq_solvent_name, aq_solvent_name,
extractant_name, extractant_name,
@ -48,8 +47,7 @@ class REEPS:
): ):
self._exp_csv_filename = exp_csv_filename self._exp_csv_filename = exp_csv_filename
self._phases_xml_filename = phases_xml_filename self._phases_xml_filename = phases_xml_filename
self._x_guess = x_guess self._opt_dict = opt_dict
self._h_guess = h_guess
self._phase_names = phase_names self._phase_names = phase_names
self._aq_solvent_name = aq_solvent_name self._aq_solvent_name = aq_solvent_name
self._extractant_name = extractant_name self._extractant_name = extractant_name
@ -60,6 +58,8 @@ class REEPS:
self._extractant_rho = extractant_rho self._extractant_rho = extractant_rho
self._diluant_rho = diluant_rho self._diluant_rho = diluant_rho
self._temp_xml_filename = "temp.xml"
shutil.copyfile(phases_xml_filename, self._temp_xml_filename)
self._phases = ct.import_phases(phases_xml_filename, phase_names) self._phases = ct.import_phases(phases_xml_filename, phase_names)
self._exp_df = pd.read_csv(self._exp_csv_filename) self._exp_df = pd.read_csv(self._exp_csv_filename)
@ -90,18 +90,11 @@ class REEPS:
self.set_in_moles(feed_vol=1) self.set_in_moles(feed_vol=1)
return None return None
def get_x_guess(self) -> float: def get_opt_dict(self) -> dict:
return self._x_guess return self._opt_dict
def set_x_guess(self, x_guess):
self._x_guess = x_guess
return None
def get_h_guess(self) -> float:
return self._h_guess
def set_h_guess(self, h_guess): def set_opt_dict(self, opt_dict):
self._h_guess = h_guess self._opt_dict = opt_dict
return None return None
def get_aq_solvent_name(self) -> str: def get_aq_solvent_name(self) -> str:
@ -161,7 +154,8 @@ class REEPS:
return None return None
def set_in_moles(self, feed_vol): def set_in_moles(self, feed_vol):
""":param feed_vol: (float) feed volume of mixture (g/L)""" """Function that initializes mole fractions
:param feed_vol: (float) feed volume of mixture (g/L)"""
phases_copy = self._phases.copy() phases_copy = self._phases.copy()
exp_df = self._exp_df.copy() exp_df = self._exp_df.copy()
solvent_name = self._aq_solvent_name solvent_name = self._aq_solvent_name
@ -244,8 +238,10 @@ class REEPS:
return self._in_moles return self._in_moles
def objective(self, x): def objective(self, x):
h = x[0] * self._h_guess """Log mean squared error between measured and predicted data
phases_copy = self._phases.copy() :param x: (list) thermo properties varied to minimize LMSE"""
temp_xml_filename = self._temp_xml_filename
phase_names = self._phase_names
aq_ind = self._aq_ind aq_ind = self._aq_ind
org_ind = self._org_ind org_ind = self._org_ind
complex_name = self._complex_name complex_name = self._complex_name
@ -253,18 +249,16 @@ class REEPS:
in_moles = self._in_moles in_moles = self._in_moles
exp_df = self._exp_df exp_df = self._exp_df
complex_index = phases_copy[org_ind].species_index(complex_name) x = np.array(x)
complex_species = phases_copy[org_ind].species(complex_index) opt_dict = copy.deepcopy(self._opt_dict)
i = 0
new_thermo_coeffs = complex_species.thermo.coeffs for species_name in opt_dict.keys():
new_thermo_coeffs[1] = h for thermo_prop in opt_dict[species_name].keys():
complex_species.thermo = ct.ConstantCp( opt_dict[species_name][thermo_prop] *= x[i]
complex_species.thermo.min_temp, i += 1
complex_species.thermo.max_temp, self.update_xml(opt_dict, temp_xml_filename)
complex_species.thermo.reference_pressure,
new_thermo_coeffs)
phases_copy[org_ind].modify_species(complex_index, complex_species)
phases_copy = ct.import_phases(temp_xml_filename, phase_names)
mix = ct.Mixture(phases_copy) mix = ct.Mixture(phases_copy)
pred = [] pred = []
for row in in_moles.values: for row in in_moles.values:
@ -285,30 +279,48 @@ class REEPS:
Returns estimated complex enthalpy in J/mol Returns estimated complex enthalpy in J/mol
:param kwargs: (dict) parameters and options for scipy.minimize :param kwargs: (dict) parameters and options for scipy.minimize
""" """
x_guess = self._x_guess opt_dict = copy.deepcopy(self._opt_dict)
h_guess = self._h_guess # x_guess = []
i = 0
for species_name in opt_dict.keys():
for _ in opt_dict[species_name].keys():
# x_guess.append(opt_dict[species_name][thermo_prop])
i += 1
x_guess = np.ones(i)
res = minimize(self.objective, x_guess, **kwargs) res = minimize(self.objective, x_guess, **kwargs)
pred_complex_enthalpy = res.x[0] * h_guess / 1000 # J/mol i = 0
return pred_complex_enthalpy for species_name in opt_dict.keys():
for thermo_prop in opt_dict[species_name].keys():
opt_dict[species_name][thermo_prop] *= res.x[i]
i += 1
return opt_dict
def update_xml(self, def update_xml(self,
complex_enthalpy, info_dict,
phases_xml_filename=None, phases_xml_filename=None):
complex_name=None): """updates xml file with info_dict
"""updates xml file with new complex_enthalpy""" :param info_dict: (dict) info in {species_names:{thermo_prop:val}}
:param phases_xml_filename: (str) xml filename if editing other xml
"""
if phases_xml_filename is None: if phases_xml_filename is None:
phases_xml_filename = self._phases_xml_filename phases_xml_filename = self._phases_xml_filename
if complex_name is None:
complex_name = self._complex_name
tree = ET.parse(phases_xml_filename) tree = ET.parse(phases_xml_filename)
root = tree.getroot() root = tree.getroot()
# Update xml file # Update xml file
for species_name in info_dict.keys():
for thermo_prop in info_dict[species_name].keys():
for species in root.iter('species'): for species in root.iter('species'):
if species.attrib['name'] is complex_name: if species.attrib['name'] == species_name:
for h0 in species.iter('h0'): for changed_prop in species.iter(thermo_prop):
h0.text = str(complex_enthalpy) changed_prop.text = str(
h0.set('updated', 'Updated at ' + time.strftime('%X')) info_dict[species_name][thermo_prop])
now = datetime.now()
changed_prop.set('updated',
'Updated at {0}:{1} {2}-{3}-{4}'
.format(now.hour, now.minute,
now.month, now.day,
now.year))
tree.write(phases_xml_filename) tree.write(phases_xml_filename)

@ -1 +0,0 @@
{"exp_csv_filename": "../data/csvs/exp_data.csv", "phases_xml_filename": "../data/xmls/twophase.xml", "x_guess": 0.96, "h_guess": -4856609000.0, "phase_names": ["HCl_electrolyte", "PC88A_liquid"], "aq_solvent_name": "H2O(L)", "extractant_name": "(HA)2(org)", "diluant_name": "dodecane", "complex_name": "Nd(H(A)2)3(org)", "rare_earth_ion_name": "Nd+++", "aq_solvent_rho": 1000.0, "extractant_rho": 960.0, "diluant_rho": 750.0}

@ -5,8 +5,9 @@ sys.path.append('../')
from reeps import REEPS from reeps import REEPS
with open('one_comp_settings.txt') as file: with open('one_ree_settings.txt') as file:
testing_params = json.load(file) testing_params = json.load(file)
beaker = REEPS(**testing_params) beaker = REEPS(**testing_params)
minimizer_kwargs = {"method": 'SLSQP', minimizer_kwargs = {"method": 'SLSQP',
@ -15,5 +16,7 @@ minimizer_kwargs = {"method": 'SLSQP',
"options": {'disp': True, 'maxiter': 1000, 'ftol': 1e-6}} "options": {'disp': True, 'maxiter': 1000, 'ftol': 1e-6}}
est_enthalpy = beaker.fit(minimizer_kwargs) est_enthalpy = beaker.fit(minimizer_kwargs)
print(est_enthalpy) print(est_enthalpy)
# info_dict = {"Nd(H(A)2)3(org)": {"h0": est_enthalpy}}
#
beaker.update_xml(est_enthalpy) beaker.update_xml(est_enthalpy)
beaker.parity_plot() beaker.parity_plot()

Loading…
Cancel
Save