r"""
Laplace equation with variable material in 2D

"""
import numpy as nm
from sfepy import data_dir

filename_mesh = data_dir + '/es-lens.mesh'

options = {
    'nls' : 'newton',
    'ls' : 'ls',
}

materials = {
    'm' : 'get_r_dependence',
}

regions = {
    'Omega' : ('all', {}),
    'Omega_Source' : ('nodes in (x < 0.00001)', {}),
    'Omega_Target' : ('nodes in (x > 99.99)', {}),
    'Omega_Lens1' : ('nodes in (x > 19.00) & (x < 21.00) & (y > 15.00)', {}),
    'Omega_Lens2' : ('nodes in (x > 39.00) & (x < 41.00) & (y > 15.00)', {}),
    'Omega_Needle' : ('nodes in (x < 10.0) & (y < 1.0)', {}),
}

fields = {
    'potential' : ('real', 1, 'Omega', 1),
}

variables = {
    'u' : ('unknown field', 'potential', 0),
    'v' : ('test field',    'potential', 'u'),
}

ebcs = {
    'u1' : ('Omega_Source', {'u.0' : 0.0}),
    'u2' : ('Omega_Target', {'u.0' : 150.0}),
    'u3' : ('Omega_Lens1', {'u.0' : 30.0}),
    'u4' : ('Omega_Lens2', {'u.0' : 150.0}),
    'u5' : ('Omega_Needle', {'u.0' : 0.0}),
}

integrals = {
    'i1' : ('v', 'gauss_o1_d3'),
}

equations = {
    'Laplace equation' :
    """dw_laplace.i1.Omega( m.val, v, u ) = 0 """
}

solvers = {
    'ls' : ('ls.scipy_direct', {}),
    'newton' : ('nls.newton',
                {'i_max'      : 1,
                 'eps_a'      : 1e-10,
                 }),
}

def get_r_dependence(ts, coors, mode=None, **kwargs):
    """
    We want to add an r factor to the laplacian integral.

    For scalar parameters, the shape has to be set to `(coors.shape[0], 1, 1)`.
    """
    if mode == 'qp':
        x = coors[:,1]
        val = x.copy()
        val.shape = (coors.shape[0], 1, 1)
        return {'val' : val}

functions = {
    'get_r_dependence' : (get_r_dependence,),
}
