Coverage for /usr/share/miniconda3/envs/dolfin/lib/python3.8/site-packages/block/block_util.py: 67%

104 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-20 13:03 +0000

1def isequal(op1, op2, eps=1e-3): 

2 from . import block_vec 

3 v = op1.create_vec() 

4 block_vec([v]).randomize() 

5 xv = op1*v 

6 err = (xv-op2*v).norm('l2')/(xv).norm('l2') 

7 return err < eps 

8 

9def issymmetric(op): 

10 x = op.create_vec() 

11 if hasattr(x, 'randomize'): 

12 x.randomize() 

13 else: 

14 from . import block_vec 

15 block_vec([x]).randomize(); 

16 opx = op*x 

17 err = (opx - op.T*x).norm('l2')/opx.norm('l2') 

18 return (err < 1e-6) 

19 

20def sign_of(op): 

21 from numpy.random import random 

22 if isscalar(op): 

23 return -1 if op < 0 else 1 

24 else: 

25 x = op.create_vec(dim=1) 

26 x.set_local(random(x.local_size())) 

27 x.apply('') 

28 return -1 if x.inner(op*x) < 0 else 1 

29 

30def mult(op, x, transposed=False): 

31 if not transposed or isscalar(op): 31 ↛ 34line 31 didn't jump to line 34, because the condition on line 31 was never false

32 return op*x 

33 else: 

34 return op.transpmult(x) 

35 

36def isscalar(obj): 

37 """Return True if obj is convertible to float. Use this instead of 

38 numpy.isscalar, becuase the latter returns true for e.g. strings""" 

39 try: 

40 float(obj) 

41 return True 

42 except: 

43 return False 

44 

45def copy(obj): 

46 """Return a deep copy of the object""" 

47 if hasattr(obj, 'copy'): 

48 return obj.copy() 

49 else: 

50 import copy 

51 try: 

52 return copy.deepcopy(obj) 

53 except TypeError: 

54# from dolfin import warning 

55# print ("Don't know how to make a deep copy of (%d,%d), making shallow copy"%(i,j)) 

56 print ("Don't know how to make a deep copy, making shallow copy") 

57 return copy.copy(obj) 

58 

59def block_tensor(obj): 

60 """Return either a block_vec or a block_mat, depending on the shape of the object""" 

61 from . import block_mat, block_vec 

62 import numpy 

63 if isinstance(obj, (block_mat, block_vec)): 63 ↛ 64line 63 didn't jump to line 64, because the condition on line 63 was never true

64 return obj 

65 

66 from ufl import Form 

67 if isinstance(obj, Form): 

68 from .splitting import split_form 

69 obj = split_form(obj) 

70 blocks = numpy.array(obj) 

71 if len(blocks.shape) == 2: 

72 return block_mat(blocks) 

73 elif len(blocks.shape) == 1: 73 ↛ 76line 73 didn't jump to line 76, because the condition on line 73 was never false

74 return block_vec(blocks) 

75 else: 

76 raise RuntimeError("Not able to create block container of rank %d"%len(blocks.shape)) 

77 

78def _create_vec(template, dim): 

79 from dolfin import DirichletBC, FunctionSpace, Function 

80 if dim is not None and hasattr(template, 'create_vec'): 

81 return template.create_vec(dim) 

82 if isinstance(template, DirichletBC): 

83 V = FunctionSpace(template.function_space()) 

84 elif isinstance(template, FunctionSpace): 84 ↛ 85line 84 didn't jump to line 85, because the condition on line 84 was never true

85 V = template 

86 else: 

87 return None 

88 if V.component(): 88 ↛ 89line 88 didn't jump to line 89, because the condition on line 88 was never true

89 return None 

90 return Function(V).vector() 

91 

92def create_vec_from(templates, dim=None): 

93 """Try to create a dolfin vector from a (list of) templates. A template is 

94 anything that we can retrieve a function space from (currently a 

95 FunctionSpace or a DirichletBC), or anything with a create_vec method (if 

96 dim is set). 

97 """ 

98 for template in wrap_in_list(templates): 98 ↛ 102line 98 didn't jump to line 102, because the loop on line 98 didn't complete

99 v = _create_vec(template, dim) 

100 if v: 

101 return v 

102 raise ValueError("Unable to create vector from template") 

103 

104def wrap_in_list(obj, types=object): 

105 """Make the argument into a list, suitable for iterating over. If it is 

106 already iterable, return it; if it is None, return the empty list; if it is 

107 a not iterable, return a length-one list. Optionally check the type.""" 

108 if obj is None: 

109 lst = [] 

110 elif hasattr(obj, '__iter__'): 

111 lst = list(obj) 

112 else: 

113 lst = [obj] 

114 for obj in lst: 

115 if not isinstance(obj, types): 115 ↛ 116line 115 didn't jump to line 116, because the condition on line 115 was never true

116 raise TypeError("expected a (list of) %s, not %s" % (types, type(obj))) 

117 return lst 

118 

119def flatten(l): 

120 if isinstance(l, (list, tuple)): 

121 for el in l: 

122 for sub in flatten(el): 

123 yield sub 

124 else: 

125 yield l 

126 

127def create_diagonal_matrix(V, val=1.0): 

128 from dolfin import TrialFunction, TestFunction 

129 from dolfin import assemble, Constant, inner, dx 

130 import numpy 

131 

132 u,v = TrialFunction(V),TestFunction(V) 

133 Z = assemble(Constant(0)*inner(u,v)*dx) 

134 if val != 0.0: 134 ↛ 135line 134 didn't jump to line 135, because the condition on line 134 was never true

135 idx = numpy.arange(*Z.local_range(0), dtype=numpy.intc) 

136 Z.ident(idx) 

137 if val != 1.0: 

138 Z *= val 

139 return Z