<reponame>n-hiroshi/deltapv<gh_stars>0
import pytest,warnings
import os,sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
warnings.resetwarnings()
warnings.simplefilter('ignore', FutureWarning)
warnings.simplefilter('ignore', DeprecationWarning)
from jax import numpy as jnp, lax, vmap
from deltapv import bcond,simulator,materials,sun,solver

Array = jnp.ndarray#util.Array
f64 = jnp.float64#util.f64
i64 = jnp.int64#util.i64


def aeq(val0,val1,tol=1e-7):
    return jnp.where(lax.abs(val1)>0,lax.abs(val0-val1)<tol*lax.abs(val1),lax.abs(val0-val1)<tol)

L = 3e-4
J = 5e-6
material = materials.create_material(Chi=3.9,
                               Eg=1.5,
                               eps=9.4,
                               Nc=8e17,
                               Nv=1.8e19,
                               mn=100,
                               mp=100,
                               Et=0,
                               tn=1e-8,
                               tp=1e-8,
                               A=1e4)
des = simulator.make_design(n_points=500,
                         Ls=[J, L-J],
                         mats=[material, material],
                         Ns=[1e17, -1e15],
                         Snl=1e7,
                         Snr=0,
                         Spl=0,
                         Spr=1e7)

ls = simulator.incident_light(Lambda=sun.Lambda_eff, P_in = sun.P_in_eff)
cell = simulator.init_cell(des,ls)
bound_eq = bcond.boundary_eq(cell)
pot_ini = solver.eq_guess(cell, bound_eq)
dv = solver.vincr(cell)
v = dv*1
print(bound_eq)
bound = bcond.boundary(cell,0.0)
print('v=%f'%0.0,bound)
bound_v = bcond.boundary(cell,v)
print('v=%f'%v,bound)

def test_boundary_eq():
    #bound_eq = bcond.boundary_eq(cell)
    assert aeq(bound_eq.phi0,-152.93817712)
    assert aeq(bound_eq.phiL,-199.08319915)
    assert aeq(bound_eq.neq0,0.0)
    assert aeq(bound_eq.neqL,0.0)
    assert aeq(bound_eq.peq0,0.0)
    assert aeq(bound_eq.peqL,0.0)


def test_boundary_v():

    assert aeq(bound.phi0,-152.93817712)
    assert aeq(bound.phiL,-199.08319915)
    assert aeq(bound.neq0,0.01)
    assert aeq(bound.neqL,9.10901811e-23)
    assert aeq(bound.peq0,9.10901811e-25)
    assert aeq(bound.peqL,0.0001)

    assert aeq(bound_v.phi0,-152.93817712)
    assert aeq(bound_v.phiL,-197.1491128)
    assert aeq(bound_v.neq0,0.01)
    assert aeq(bound_v.neqL,9.10901811e-23)
    assert aeq(bound_v.peq0,9.10901811e-25)
    assert aeq(bound_v.peqL,0.0001)

def test_contact_phi():
    ctct_0_phi, ctct_L_phi = bcond.contact_phi(cell,bound_v,pot_ini)
    assert aeq(ctct_0_phi,-3.11351531)
    assert aeq(ctct_L_phi,-5.04760166)

def test_contact_phin():
    ctct_0_phin, ctct_L_phin = bcond.contact_phin(cell,bound_v,pot_ini)
    assert aeq(ctct_0_phin, 0.13971018)
    assert aeq(ctct_L_phin, 0.0)

def test_contact_phip():
    ctct_0_phip, ctct_L_phip = bcond.contact_phip(cell,bound_v,pot_ini)
    assert aeq(ctct_0_phip, 0.0)
    assert aeq(ctct_L_phip, -0.03143479)

def test_contact_phin_deriv():
    dctct_phin = bcond.contact_phin_deriv(cell,pot_ini)
    dctct_phin_true = [-0.009292383238842537,0.0027942355434598695,-0.006498147695382667,
                        0.0,-2.5452742163340038e-23,2.5452742163340038e-23,0.0,0.0]
    for i in range(len(dctct_phin)): 
        assert aeq(dctct_phin[i],dctct_phin_true[i])

def test_contact_phip_deriv():
    dctct_phip = bcond.contact_phip_deriv(cell,pot_ini)
    dctct_phip_true = [-1.28854507e-22,1.28854507e-22,0.,0.,-0.01414582,0.04704269,0.,0.03289687]
    for i in range(len(dctct_phip)): 
        assert aeq(dctct_phip[i],dctct_phip_true[i],tol=1e-6)

import pytest,warnings
import os,sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
warnings.resetwarnings()
warnings.simplefilter('ignore', FutureWarning)
warnings.simplefilter('ignore', DeprecationWarning)
from jax import numpy as jnp, lax, vmap
from deltapv import bcond,simulator,materials,sun,solver

Array = jnp.ndarray#util.Array
f64 = jnp.float64#util.f64
i64 = jnp.int64#util.i64


def aeq(val0,val1,tol=1e-7):
    return jnp.where(lax.abs(val1)>0,lax.abs(val0-val1)<tol*lax.abs(val1),lax.abs(val0-val1)<tol)

L = 3e-4
J = 5e-6
material = materials.create_material(Chi=3.9,
                               Eg=1.5,
                               eps=9.4,
                               Nc=8e17,
                               Nv=1.8e19,
                               mn=100,
                               mp=100,
                               Et=0,
                               tn=1e-8,
                               tp=1e-8,
                               A=1e4)
des = simulator.make_design(n_points=500,
                         Ls=[J, L-J],
                         mats=[material, material],
                         Ns=[1e17, -1e15],
                         Snl=1e7,
                         Snr=0,
                         Spl=0,
                         Spr=1e7)

ls = simulator.incident_light(Lambda=sun.Lambda_eff, P_in = sun.P_in_eff)
cell = simulator.init_cell(des,ls)
bound_eq = bcond.boundary_eq(cell)
pot_ini = solver.eq_guess(cell, bound_eq)
dv = solver.vincr(cell)
v = dv*1
print(bound_eq)
bound = bcond.boundary(cell,0.0)
print('v=%f'%0.0,bound)
bound_v = bcond.boundary(cell,v)
print('v=%f'%v,bound)

def test_boundary_eq():
    #bound_eq = bcond.boundary_eq(cell)
    assert aeq(bound_eq.phi0,-152.93817712)
    assert aeq(bound_eq.phiL,-199.08319915)
    assert aeq(bound_eq.neq0,0.0)
    assert aeq(bound_eq.neqL,0.0)
    assert aeq(bound_eq.peq0,0.0)
    assert aeq(bound_eq.peqL,0.0)


def test_boundary_v():

    assert aeq(bound.phi0,-152.93817712)
    assert aeq(bound.phiL,-199.08319915)
    assert aeq(bound.neq0,0.01)
    assert aeq(bound.neqL,9.10901811e-23)
    assert aeq(bound.peq0,9.10901811e-25)
    assert aeq(bound.peqL,0.0001)

    assert aeq(bound_v.phi0,-152.93817712)
    assert aeq(bound_v.phiL,-197.1491128)
    assert aeq(bound_v.neq0,0.01)
    assert aeq(bound_v.neqL,9.10901811e-23)
    assert aeq(bound_v.peq0,9.10901811e-25)
    assert aeq(bound_v.peqL,0.0001)

def test_contact_phi():
    ctct_0_phi, ctct_L_phi = bcond.contact_phi(cell,bound_v,pot_ini)
    assert aeq(ctct_0_phi,-3.11351531)
    assert aeq(ctct_L_phi,-5.04760166)

def test_contact_phin():
    ctct_0_phin, ctct_L_phin = bcond.contact_phin(cell,bound_v,pot_ini)
    assert aeq(ctct_0_phin, 0.13971018)
    assert aeq(ctct_L_phin, 0.0)

def test_contact_phip():
    ctct_0_phip, ctct_L_phip = bcond.contact_phip(cell,bound_v,pot_ini)
    assert aeq(ctct_0_phip, 0.0)
    assert aeq(ctct_L_phip, -0.03143479)

def test_contact_phin_deriv():
    dctct_phin = bcond.contact_phin_deriv(cell,pot_ini)
    dctct_phin_true = [-0.009292383238842537,0.0027942355434598695,-0.006498147695382667,
                        0.0,-2.5452742163340038e-23,2.5452742163340038e-23,0.0,0.0]
    for i in range(len(dctct_phin)): 
        assert aeq(dctct_phin[i],dctct_phin_true[i])

def test_contact_phip_deriv():
    dctct_phip = bcond.contact_phip_deriv(cell,pot_ini)
    dctct_phip_true = [-1.28854507e-22,1.28854507e-22,0.,0.,-0.01414582,0.04704269,0.,0.03289687]
    for i in range(len(dctct_phip)): 
        assert aeq(dctct_phip[i],dctct_phip_true[i],tol=1e-6)
#util.Array #util.f64 #util.i64 #bound_eq = bcond.boundary_eq(cell)