Coverage for src/methodsnm/meshfct.py: 75%
52 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-27 13:22 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-27 13:22 +0000
1from abc import ABC, abstractmethod
2import numpy as np
3from numpy import array
4from methodsnm.trafo import *
6class MeshFunction:
7 mesh = None
8 def __init__(self, mesh):
9 self.mesh = mesh
10 pass
12 @abstractmethod
13 def _evaluate(self, ip, trafo):
14 raise Exception("Not implemented - Base class should not be used")
16 def _evaluate_array(self, ips, trafo):
17 ret = np.empty(ips.shape[0])
18 for i in range(ips.shape[0]):
19 ret[i] = self.evaluate(ips[i], trafo)
20 return ret
22 def evaluate(self, ip, trafo):
23 if isinstance(ip, np.ndarray):
24 if ip.ndim == 1:
25 return self._evaluate(ip, trafo)
26 else:
27 return self._evaluate_array(ip, trafo)
28 else:
29 raise Exception("Invalid input")
32class ConstantFunction(MeshFunction):
33 c = None
34 def __init__(self, c, mesh=None):
35 self.mesh = mesh
36 self.c = c
38 def _evaluate(self, ip, trafo):
39 return self.c
41class GlobalFunction(MeshFunction):
42 f = None
43 def __init__(self, function, mesh):
44 self.mesh = mesh
45 self.f = function
47 def _evaluate(self, ip, trafo):
48 return self.f(trafo(ip))
50class FEFunction(MeshFunction):
51 fes = None
52 vector = None
53 def __init__(self, fes):
54 self.mesh = fes.mesh
55 self.fes = fes
56 self.vector = np.zeros(fes.ndof)
58 def _evaluate(self, ip, trafo):
59 fe = self.fes.finite_element(trafo.elnr)
60 dofs = self.fes.element_dofs(trafo.elnr)
61 return np.dot(fe.evaluate(ip), self.vector[dofs])
63 def _evaluate_array(self, ips, trafo):
64 fe = self.fes.finite_element(trafo.elnr)
65 dofs = self.fes.element_dofs(trafo.elnr)
66 return np.dot(fe.evaluate(ips), self.vector[dofs])