#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 25 14:37:01 2024

@author: david
"""

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 14 14:39:59 2024

@author: wangweiye
"""

import numpy as np
from numpy import power
import cantera as ct
import matplotlib.pyplot as plt
from scipy.interpolate import CubicHermiteSpline

font = {'family':'times',
        'color':'darkred',
        'weight':'normal',
        'size':16}
def calVaporPressure(T:float) ->float :
#    return 1e5*np.power(10,4.6543 - (1435.264/(T - 64.84))) # returns Pa, where T is in K
    #P = 1e3*np.exp(16.7 - (4060.0/(T - 37.0))) # returns Pa, where T is in K,FOR WATER
    #P = 1e5*np.power(10,4.02832-(1268.636/(T-56.199))) ; #FOR N-HEPTANE
    P = 0.00  #NO FUEL @ t0
    return P

# return 1D numpy array for liquid phase mass fraction
def genLiquidMassFracArr(gas,dropTemp:float,P:float,dropSpec:str) :
    gas.TPX = dropTemp,P,dropSpec 
    massArr_ = gas.Y
    # print(gas.Y)
    return massArr_

# return 1D numpy array for gas phase mass fraction 
def genGasMassFracArr(gas,gasTemp:float,P:float,gasSpec:str):
    gas.TPX = gasTemp,P,gasSpec
    massArr_ = gas.Y 
    return massArr_

# return 1D numpy array for gas phase spatial coordinate and temperature
def genGasTempAndRadiusArr(Rd,L,shift,Wmix,nPts,dropTemp,gasTemp,deltaT):
    dX = L/(nPts-1) 
    r_ = np.zeros(nPts)
    r_[0] = Rd
    #DEBUG
    #print("r_[0] = %15.6e\n"%(r_[0]))
    for ii in range(1, nPts-1):
        r_[ii] = r_[ii-1] + dX
    r_[nPts-1] = Rd+L
    
    x = [Rd+shift, Rd+shift+Wmix] ; 
    y = [dropTemp+deltaT, gasTemp] ; 
    m = [0,0] ; 
    spline = CubicHermiteSpline(x, y, m)
    
    T_ = np.zeros(nPts)
    for i in range(nPts):
        if r_[i] <= Rd  :
            T_[i] = dropTemp ; 
        if r_[i] > Rd and  r_[i] < (Rd+shift):
            T_[i] = dropTemp + deltaT ;
        if r_[i] >= (Rd + shift) and (r_[i] <= Rd + shift + Wmix) : 
            T_[i] = spline(r_[i]) ; 
        if r_[i] > (Rd + shift +Wmix) and r_[i] <= (Rd+L) :
            T_[i] = gasTemp ;
    return r_,T_

# return 2D numpy array with size of (nvar*nPts)
def writeGlobalArr(Rd,L,shift,Wmix,dropTemp,gasTemp,deltaT,P,dropSpec,gasSpec,gas,lnPts,gnPts,fileName):
    liquidMassFracArr_ = genLiquidMassFracArr(gas, dropTemp, P, dropSpec)
    gasMassFracArr_ = genGasMassFracArr(gas, gasTemp, P, gasSpec)
    
    # assign spatial coordinate value
    rG_,TG_ = genGasTempAndRadiusArr(Rd, L, shift, Wmix, gnPts, dropTemp, gasTemp,deltaT)
    rL_ = np.zeros(lnPts)
    dXL = Rd/(lnPts-1) 
    rL_[0] = 0.0
    for ii in range(1, lnPts-1):
        rL_[ii] = rL_[ii-1] + dXL
    rL_[lnPts-1] = Rd
    #DEBUG
    # print("last element of rL_ is :%15.6e"%(rL_[lnPts-1]))
    # print("first element of rG_ is :%15.6e"%(rG_[0]))
    r_ = np.concatenate((rL_,rG_))
    # assign temperature values
    TL_ = np.ones(lnPts)*dropTemp 
    T_ = np.concatenate((TL_,TG_))
    # assign pressure values
    P_ = np.ones(gnPts+lnPts) * P
    mdot_ = np.ones(gnPts+lnPts) * 0.0
    
    # write global arr to output file
    out=open(fileName,"w")
    for i in range(lnPts):
        out.write("%15.6e\t%15.6e\t"%(r_[i],T_[i]))
        for j in range(gas.n_species):
            out.write("%15.6e\t"%(liquidMassFracArr_[j]))
        out.write("%15.6e\t"%(P_[i]))
        out.write("%15.6e\n"%(mdot_[i]))
        
    for i in range(lnPts,lnPts+gnPts):
        out.write("%15.6e\t%15.6e\t"%(r_[i],T_[i]))
        for j in range(gas.n_species):
            out.write("%15.6e\t"%(gasMassFracArr_[j]))
        out.write("%15.6e\t"%(P_[i]))
        out.write("%15.6e\n"%(mdot_[i]))
        
    out.close()
    
    
# =============================================================================
# Following block defines the user defined parameters 
# =============================================================================
if __name__ == "__main__" :
    fileName = "initialConditionTest.dat"   #This script output the initial conditions   
    #mech='sdMechMergedVer2.cti'
    mech='redKaust-C3.xml'      #Mechanism name
    #mech='chem171.cti'
    gas = ct.Solution(mech)   
    Rd = 200.0e-6                #droplet radius, Unit:[m]
    domainLength = Rd*50.0      #domain length, Unit:[m]
    shift = Rd * 3.0e0              #shift of mixing region from droplet surface
    Wmix = Rd * 5.0e0               #thickness of mixing layer for heptane mass fraction & Temperature distribution, Unit:[m]
    gNpts = 5000                #Number of grid points in gas phase
    lNpts = 500                 #Number of grid points in liquid phase
    Tamb = 1400.00                 #Temperature of ambient air, Unit : [K]
    Pamb = 20.0*ct.one_atm          #Pressure of ambient air, Unit :[Pa]
    Td = 310.00                 #Temperature of droplet surface, Unit: [K]
    deltaT = 20.0
    #RH = 0.0                    #relative humidity
    #dropName = "NC7H16:0.80,C3H8:0.20"          #Name of droplet species
    dropName ="NC7H16:1.00"
    gasComposition = "O2:0.21,N2:0.79"
    
    writeGlobalArr(Rd, domainLength, shift, Wmix, Td, Tamb,deltaT, Pamb, dropName, gasComposition, gas, lNpts, gNpts, fileName)
    # writeGlobalArr(Rd, L, shift, Wmix, dropTemp, gasTemp, P, dropSpec, gasSpec, gas, lnPts, gnPts, fileName)
    #writeInitCond(Rd, domainLength, shift, Wmix, nGrid, Tamb, Pamb, Td, RH, gas, dropName, oxidizerName, bathName, fileName)
    # genTotalMassfracArr(Rd, domainLength, shift, Wmix, nGrid, Tamb, Pamb, Td, RH, gas, dropName, oxidizerName, bathName)
    # genTemArr(Rd, domainLength, shift, Wmix, nGrid, Tdrop, Tamb)
    # genMolefracArr(Rd, domainLength, shift, Wmix, nGrid, Tamb, Pamb, Td, RH)