<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 simulator,materials,sun,bcond,solver,poisson,adjoint

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

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_eq = simulator.equilibrium(des, ls)

dv = solver.vincr(cell)
v = dv*1.0
bound = bcond.boundary(cell,v)
guess = solver.ooe_guess(cell,pot_eq)
total_j, pot = adjoint.solve_pdd(cell, v, guess)

def aeq(val0,val1,tol=1e-7):
    return lax.abs(val0-val1)<tol

def test_pois():
    pot_ini = solver.eq_guess(cell, bound_eq)
    pois = poisson.pois(cell, pot_ini)
    pois0 = -0.00955556  
    pois7 = 1.70496925   
    pois8 = -1.71667481
    poisL = -0.00215
    #print(pois.shape)
    assert aeq(pois[:7],pois0*jnp.ones(7,f64)).all() 
    assert aeq(pois[7],f64(pois7))
    assert aeq(pois[8],f64(pois8))
    assert aeq(pois[9:],poisL*jnp.ones(489,f64)).all()

def test_pois_deriv_eq():
    pot_ini = solver.eq_guess(cell, bound_eq)
    dpois_phi_, dpois_phi__, dpois_phi___ = poisson.pois_deriv_eq(cell, pot_ini)

    assert vmap(aeq,(None,0))(-0.03715514,dpois_phi_).all()
    assert vmap(aeq,(None,0))( 0.07475473,dpois_phi__[:8]).all()
    assert vmap(aeq,(None,0))( 0.07656028,dpois_phi__[8:]).all()
    assert vmap(aeq,(None,0))(-0.03715514,dpois_phi___).all()

def test_pois_deriv():
    dpois_phi_, dpois_phi__, dpois_phi___, dpois_dphin__, dpois_dphip__ = poisson.pois_deriv(cell, pot)

    assert vmap(aeq,(None,0))(-0.03715514,dpois_phi_).all()

    #print(dpois_phi__)
    #jnp.save('assert_dpois_phi__',dpois_phi__)
    dpois_phi___true=jnp.load('assert_dpois_phi__.npy')
    assert aeq(dpois_phi__,dpois_phi___true).all()

    assert vmap(aeq,(None,0))(-0.03715514,dpois_phi___).all()

    #print(dpois_dphin__)
    #jnp.save('assert_dpois_dphin__',dpois_dphin__)
    dpois_dphin___true=jnp.load('assert_dpois_dphin__.npy')
    assert aeq(dpois_dphin__,dpois_dphin___true).all()

    #print(dpois_dphip__)
    #jnp.save('assert_dpois_dphip__',dpois_dphip__)
    dpois_dphip___true=jnp.load('assert_dpois_dphip__.npy')
    assert aeq(dpois_dphip__,dpois_dphip___true).all()


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 simulator,materials,sun,bcond,solver,poisson,adjoint

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

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_eq = simulator.equilibrium(des, ls)

dv = solver.vincr(cell)
v = dv*1.0
bound = bcond.boundary(cell,v)
guess = solver.ooe_guess(cell,pot_eq)
total_j, pot = adjoint.solve_pdd(cell, v, guess)

def aeq(val0,val1,tol=1e-7):
    return lax.abs(val0-val1)<tol

def test_pois():
    pot_ini = solver.eq_guess(cell, bound_eq)
    pois = poisson.pois(cell, pot_ini)
    pois0 = -0.00955556  
    pois7 = 1.70496925   
    pois8 = -1.71667481
    poisL = -0.00215
    #print(pois.shape)
    assert aeq(pois[:7],pois0*jnp.ones(7,f64)).all() 
    assert aeq(pois[7],f64(pois7))
    assert aeq(pois[8],f64(pois8))
    assert aeq(pois[9:],poisL*jnp.ones(489,f64)).all()

def test_pois_deriv_eq():
    pot_ini = solver.eq_guess(cell, bound_eq)
    dpois_phi_, dpois_phi__, dpois_phi___ = poisson.pois_deriv_eq(cell, pot_ini)

    assert vmap(aeq,(None,0))(-0.03715514,dpois_phi_).all()
    assert vmap(aeq,(None,0))( 0.07475473,dpois_phi__[:8]).all()
    assert vmap(aeq,(None,0))( 0.07656028,dpois_phi__[8:]).all()
    assert vmap(aeq,(None,0))(-0.03715514,dpois_phi___).all()

def test_pois_deriv():
    dpois_phi_, dpois_phi__, dpois_phi___, dpois_dphin__, dpois_dphip__ = poisson.pois_deriv(cell, pot)

    assert vmap(aeq,(None,0))(-0.03715514,dpois_phi_).all()

    #print(dpois_phi__)
    #jnp.save('assert_dpois_phi__',dpois_phi__)
    dpois_phi___true=jnp.load('assert_dpois_phi__.npy')
    assert aeq(dpois_phi__,dpois_phi___true).all()

    assert vmap(aeq,(None,0))(-0.03715514,dpois_phi___).all()

    #print(dpois_dphin__)
    #jnp.save('assert_dpois_dphin__',dpois_dphin__)
    dpois_dphin___true=jnp.load('assert_dpois_dphin__.npy')
    assert aeq(dpois_dphin__,dpois_dphin___true).all()

    #print(dpois_dphip__)
    #jnp.save('assert_dpois_dphip__',dpois_dphip__)
    dpois_dphip___true=jnp.load('assert_dpois_dphip__.npy')
    assert aeq(dpois_dphip__,dpois_dphip___true).all()
#util.Array #util.f64 #util.i64 #print(pois.shape) #print(dpois_phi__) #jnp.save('assert_dpois_phi__',dpois_phi__) #print(dpois_dphin__) #jnp.save('assert_dpois_dphin__',dpois_dphin__) #print(dpois_dphip__) #jnp.save('assert_dpois_dphip__',dpois_dphip__)