# Path: sucemfem/Sources/fillament_current_source.py
# class FillamentCurrentSource(CurrentSource):
#     no_integration_points = 100
# 
#     def __init__(self, *names, **kwargs):
#         self._dirty = True
#         
#     def set_no_integration_points(self, no_integration_points):
#         """Set number of integration points to use along the length of
#         the fillament"""
#         self.no_integration_points = no_integration_points
#     
#     def set_source_endpoints(self, source_endpoints):
#         """Set the current filament endpoints.
# 
#         @param source_endpoints: 2x3 array with the coordinates of the start
#             and end point of the current fillament. The conventional
#             current flows from the start point towards the endpoint.
#         """
#         self.source_endpoints = source_endpoints
#         self._dirty = True
# 
#     def set_value(self, value):
#         """Set line current value in Amperes"""
#         self.value = value
#         self._dirty = True
#         
#     def _update(self):
#         if self._dirty:
#             self.source_start, self.source_end = self.source_endpoints
#             self.source_delta = self.source_end - self.source_start
#             self.vector_value = unit_vector(self.source_delta)*self.value
#         self._dirty == False
#             
#     def get_contribution(self):
#         self._update()
#         source_len = vector_length(self.source_delta)
#         no_pts = self.no_integration_points
#         if no_pts == 1:
#             intg_pts = [self.source_start + self.source_delta*0.5]
#         else:
#             intg_pts = self.source_start + self.source_delta*np.linspace(
#                 0,1,no_pts)[:, np.newaxis]
# 
#         contribs = collections.defaultdict(lambda : 0.)
#         point_magnitude = self.vector_value*source_len/no_pts
#         for pt in intg_pts:
#             dnos, vals = calc_pointsource_contrib(
#                 self.function_space, pt, point_magnitude)
#             for dn, v in zip(dnos, vals):
#                 contribs[dn] = contribs[dn] + v
# 
#         dofnos = np.array(contribs.keys(), dtype=np.uint)
#         rhs_contribs = np.array(contribs.values())
#         return dofnos, rhs_contribs
# 
# 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))


from sucemfem.Sources.fillament_current_source import FillamentCurrentSource
from sucemfem.Utilities.Geometry import unit_vector, vector_length
## (at your option) any later version.
##
## SUCEM is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with SUCEM. If not, see <http://www.gnu.org/licenses/>. 
##
## Contact: cemagga@gmail.com 
from __future__ import division


class FillamentSource(object):
    """High level current fillament source representation"""
    def __init__(self, function_space):
        self.function_space = function_space

    def set_source_parameters(self, source_parameters):
        """Set current fillament source paerameters using dict

        source_parameters is a dict with keys:

        I -- Source current in amperes
        endpoints -- 2x3 array with start and end coordinates of the source
        """
        self.source_parameters = source_parameters
        endpoints = source_parameters['endpoints']
        delta = endpoints[1] - endpoints[0]

# Path: sucemfem/Sources/fillament_current_source.py
# class FillamentCurrentSource(CurrentSource):
#     no_integration_points = 100
# 
#     def __init__(self, *names, **kwargs):
#         self._dirty = True
#         
#     def set_no_integration_points(self, no_integration_points):
#         """Set number of integration points to use along the length of
#         the fillament"""
#         self.no_integration_points = no_integration_points
#     
#     def set_source_endpoints(self, source_endpoints):
#         """Set the current filament endpoints.
# 
#         @param source_endpoints: 2x3 array with the coordinates of the start
#             and end point of the current fillament. The conventional
#             current flows from the start point towards the endpoint.
#         """
#         self.source_endpoints = source_endpoints
#         self._dirty = True
# 
#     def set_value(self, value):
#         """Set line current value in Amperes"""
#         self.value = value
#         self._dirty = True
#         
#     def _update(self):
#         if self._dirty:
#             self.source_start, self.source_end = self.source_endpoints
#             self.source_delta = self.source_end - self.source_start
#             self.vector_value = unit_vector(self.source_delta)*self.value
#         self._dirty == False
#             
#     def get_contribution(self):
#         self._update()
#         source_len = vector_length(self.source_delta)
#         no_pts = self.no_integration_points
#         if no_pts == 1:
#             intg_pts = [self.source_start + self.source_delta*0.5]
#         else:
#             intg_pts = self.source_start + self.source_delta*np.linspace(
#                 0,1,no_pts)[:, np.newaxis]
# 
#         contribs = collections.defaultdict(lambda : 0.)
#         point_magnitude = self.vector_value*source_len/no_pts
#         for pt in intg_pts:
#             dnos, vals = calc_pointsource_contrib(
#                 self.function_space, pt, point_magnitude)
#             for dn, v in zip(dnos, vals):
#                 contribs[dn] = contribs[dn] + v
# 
#         dofnos = np.array(contribs.keys(), dtype=np.uint)
#         rhs_contribs = np.array(contribs.values())
#         return dofnos, rhs_contribs
# 
# 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/Sources/fillament_source.py
from sucemfem.Sources.fillament_current_source import FillamentCurrentSource
from sucemfem.Utilities.Geometry import unit_vector, vector_length

## (at your option) any later version.
##
## SUCEM is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with SUCEM. If not, see <http://www.gnu.org/licenses/>. 
##
## Contact: cemagga@gmail.com 
from __future__ import division


class FillamentSource(object):
    """High level current fillament source representation"""
    def __init__(self, function_space):
        self.function_space = function_space

    def set_source_parameters(self, source_parameters):
        """Set current fillament source paerameters using dict

        source_parameters is a dict with keys:

        I -- Source current in amperes
        endpoints -- 2x3 array with start and end coordinates of the source
        """
        self.source_parameters = source_parameters
        endpoints = source_parameters['endpoints']
        delta = endpoints[1] - endpoints[0]

        self.direction = unit_vector(delta)