Coverage for src/methodsnm/intrule.py: 73%

30 statements  

« 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 

4 

5class IntRule: 

6 nodes = None 

7 weights = None 

8 exactness_degree = None 

9 def __init__(self): 

10 pass 

11 

12 def integrate(self, f): 

13 # f is assumed to be a function that takes a numpy array as input 

14 fvals = array([f(self.nodes[i]) for i in range(len(self.weights))]) 

15 return np.dot(fvals.T, self.weights) 

16 

17 def __str__(self): 

18 return f"Integration rule \"{self.__class__.__name__}\" with {len(self.nodes)} nodes (exactness degree {self.exactness_degree}):\nnodes = {self.nodes}\nweights = {self.weights}" 

19 

20from methodsnm.intrule_1d import MidPointRule, NewtonCotesRule, NP_GaussLegendreRule 

21from methodsnm.intrule_2d import EdgeMidPointRule, DuffyBasedRule 

22 

23npgauss_warned = False 

24def select_integration_rule(order, eltype): 

25 global npgauss_warned 

26 if eltype == "segment": 

27 if order == 1: 

28 return MidPointRule() 

29 else: 

30 if npgauss_warned == False: 

31 print("Warning: Using NumPy Gauss rules!") 

32 npgauss_warned = True 

33 return NP_GaussLegendreRule (n=order//2+1) 

34 #return NewtonCotesRule(n=order+1) 

35 elif eltype == "triangle": 

36 if order <= 1: 

37 return EdgeMidPointRule() 

38 else: 

39 return DuffyBasedRule(order) 

40 else: 

41 raise NotImplementedError("select_integration_rule only implemented for segments and triangles (not for " + eltype + ", yet)") 

42