'''
Tutorial: RockyChem
===================

Demonstrates how to easily perform typical compositional conversions using Rockychem.

Open this code in an executable MyBinder instance (MyBinder links may be slow to load-- please be patient!):

.. image:: https://mybinder.org/badge_logo.svg
  :target: https://mybinder.org/v2/gl/swmatthews-research%2FThermoEngineLite/main?urlpath=%2Fdoc%2Ftree%2F.%2Fdoc%2Fsource%2Fauto_examples%2F_4_unorganized%2Frockychem_basics.ipynb
'''

# %% 
# Initialization
# --------------
#
# Import necessary packages:

import numpy as np
import pandas as pd
import thermoengine
from thermoengine import rockychem, model

OXIDE_WTS = rockychem.OXIDE_WTS

# %% [markdown]
# A simple test for converting bulk compositions
# ----------------------------------------------

wt_oxides = {
    'MgO' : 2*OXIDE_WTS['MgO'],
    'SiO2': 1*OXIDE_WTS['SiO2']}
  

mol_elems = rockychem.convert_bulk_comp(wt_oxides=wt_oxides, to='mol_elems')

mol_oxides = rockychem.convert_bulk_comp(wt_oxides=wt_oxides, to='mol_oxides')

wt_oxides_rev = rockychem.convert_bulk_comp(mol_oxides=mol_oxides, to='wt_oxides')

# %%

print(wt_oxides)
print('---')

print(mol_elems)
print('---')

print(mol_oxides)
print('---')

print('... and converting in the reverse direction ...')
print(wt_oxides_rev)
print('---')


# %% [markdown]
# Converting entire tables
# ------------------------

# %%
wt_oxides_tbl = pd.DataFrame({
    'SiO2': {'SiO2' : OXIDE_WTS['SiO2']},
    'MgSiO3': {'MgO' : OXIDE_WTS['MgO'], 'SiO2':OXIDE_WTS['SiO2']},
    'Mg2SiO4': {'MgO' : 2*OXIDE_WTS['MgO'], 'SiO2':OXIDE_WTS['SiO2']},
    'MgO': {'MgO' : OXIDE_WTS['MgO']}},
    index=['SiO2','MgO']).fillna(0).T

mol_elems_tbl = rockychem.convert_bulk_comp_table(wt_oxides=wt_oxides_tbl, to='mol_elems')
mol_oxides_tbl = rockychem.convert_bulk_comp_table(wt_oxides=wt_oxides_tbl, to='mol_oxides')

wt_oxides_rev_tbl = rockychem.convert_bulk_comp_table(mol_elems=mol_elems_tbl, to='wt_oxides')

# %%
print(wt_oxides_tbl)
print('---')
print(mol_elems_tbl)

print('---')
print(mol_oxides_tbl)
print('---')

print('')
print('... and converting in the reverse direction ...')
print(wt_oxides_rev_tbl)
print('---')

# %% [markdown]
# Examples converting phase compositions
# ----------------------------------------------

# %% [markdown]
# - **You must provide a phase abbreviation. Here is the list of available abbreviations**

# %%
database = model.Database(database_name='MELTS_v1_2')
database.phase_list


# %%
wt_oxides = {
    'MgO' : 1.5*OXIDE_WTS['MgO'],
    'FeO' : 0.5*OXIDE_WTS['FeO'],
    'SiO2': 1*OXIDE_WTS['SiO2']}
mol_endmems = rockychem.convert_phase_comp('Ol', wt_oxides=wt_oxides, to='mol_endmems')
mol_endmems


# %% [markdown]
# Converting a table of phase compositions
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# - Note these must all be the same phase

wt_oxides_tbl = pd.DataFrame({
    'Mg2SiO4': {'MgO' : 2*OXIDE_WTS['MgO'], 'SiO2':OXIDE_WTS['SiO2']},
    'Mg1Fe1SiO4': {'MgO' : 1*OXIDE_WTS['MgO'], 'FeO':1*OXIDE_WTS['FeO'], 'SiO2':OXIDE_WTS['SiO2']},
    'Fe2SiO4': {'FeO' : 2*OXIDE_WTS['FeO'], 'SiO2':OXIDE_WTS['SiO2']}
    }, index=['MgO','FeO','SiO2']).fillna(0).T

print('input')
wt_oxides_tbl

# %%
mol_endmems_tbl = rockychem.convert_phase_comp_table('Ol', wt_oxides=wt_oxides_tbl, to='mol_endmems')
mol_endmems_tbl


# %%
print('... and in the reverse direction ...')

wt_oxides_rev_tbl = rockychem.convert_phase_comp_table('Ol', mol_endmems=mol_endmems_tbl, to='wt_oxides')
wt_oxides_rev_tbl

# %% [markdown]
# A simplified BSE composition (Schaefer & Fegley, 2009)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# - Converting a bulk composition to liquid endmembers

# %%
BSE_wt_oxides = {
    'SiO2':45.97, 
    'MgO':36.66, 
    'Al2O3':4.77, 
    'FeO':8.24, 
    'CaO':3.78, 
    'Na2O':0.35, 
    'K2O':0.04}

BSE_wt_oxides

# %%
mol_endmems = rockychem.convert_phase_comp('Liq', wt_oxides=BSE_wt_oxides, to='mol_endmems')
mol_endmems






