# Path: sucemfem/Utilities/Geometry.py
# def unit_vector(vec):
#     """Calculate a unit vector of vec"""
#     return vec/vector_length(vec)
# 
# def vector_length(vec):
#     """Calculate the length of a vector"""
#     return np.sqrt(np.sum(vec**2))
# 
# Path: sucemfem/Utilities/Converters.py
# def as_dolfin_vector(a):
#     """Convert array to a dolfin Vector() instance"""
#     assert len(a.shape) == 1            # 1D vectors please
#     v = dolfin.Vector(len(a))
#     v.set_local(np.require(a, requirements=['C',]))
#     return v


import dolfin
import numpy as np
from scipy.integrate import romberg
from sucemfem.Utilities.Geometry import unit_vector, vector_length
from sucemfem.Utilities.Converters import as_dolfin_vector
from __future__ import division


class VoltageAlongLine(object):
    """Measure voltage along a straight line between two points"""
    def __init__(self, field_function):
        self.field_function = field_function

    def calculate_voltage(self, start_pt, end_pt):
        fn = self.field_function
        delta = end_pt - start_pt
        l_hat = unit_vector(delta)
        # Evaluate E . l_hat where E is the electric field vector and
        # l_hat is a unit vector along the integration path.
        eval_fn = lambda l: np.dot(l_hat, fn(*(start_pt + delta*l)))
        # Integrate over unit length
        intg = romberg(eval_fn, 0, 1)
        # Multiply by inteval length to de-normalise
        interval_len = vector_length(delta)
        intg = intg*interval_len
        return intg

class ComplexVoltageAlongLine(object):
    """Measure complex voltage along a straight line between two points"""
    def __init__(self, function_space):
        self.function_space = function_space

    def set_dofs(self, dofs):
        self.dofs = dofs
        self.x_r = as_dolfin_vector(self.dofs.real)

# Path: sucemfem/Utilities/Geometry.py
# def unit_vector(vec):
#     """Calculate a unit vector of vec"""
#     return vec/vector_length(vec)
# 
# def vector_length(vec):
#     """Calculate the length of a vector"""
#     return np.sqrt(np.sum(vec**2))
# 
# Path: sucemfem/Utilities/Converters.py
# def as_dolfin_vector(a):
#     """Convert array to a dolfin Vector() instance"""
#     assert len(a.shape) == 1            # 1D vectors please
#     v = dolfin.Vector(len(a))
#     v.set_local(np.require(a, requirements=['C',]))
#     return v


# Path: sucemfem/Sources/PostProcess.py
import dolfin
import numpy as np
from scipy.integrate import romberg
from sucemfem.Utilities.Geometry import unit_vector, vector_length
from sucemfem.Utilities.Converters import as_dolfin_vector

from __future__ import division


class VoltageAlongLine(object):
    """Measure voltage along a straight line between two points"""
    def __init__(self, field_function):
        self.field_function = field_function

    def calculate_voltage(self, start_pt, end_pt):
        fn = self.field_function
        delta = end_pt - start_pt
        l_hat = unit_vector(delta)
        # Evaluate E . l_hat where E is the electric field vector and
        # l_hat is a unit vector along the integration path.
        eval_fn = lambda l: np.dot(l_hat, fn(*(start_pt + delta*l)))
        # Integrate over unit length
        intg = romberg(eval_fn, 0, 1)
        # Multiply by inteval length to de-normalise
        interval_len = vector_length(delta)
        intg = intg*interval_len
        return intg

class ComplexVoltageAlongLine(object):
    """Measure complex voltage along a straight line between two points"""
    def __init__(self, function_space):
        self.function_space = function_space

    def set_dofs(self, dofs):
        self.dofs = dofs
        self.x_r = as_dolfin_vector(self.dofs.real)

        self.x_i = as_dolfin_vector(self.dofs.imag)