Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

# This file is part of the pyMOR project (http://www.pymor.org). 

# Copyright Holders: Rene Milk, Stephan Rave, Felix Schindler 

# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause) 

 

from __future__ import absolute_import, division, print_function 

 

import numpy as np 

 

from pymor.grids.interfaces import ReferenceElementInterface 

from pymor.tools.quadratures import GaussQuadratures 

 

 

class Point(ReferenceElementInterface): 

 

    dim = 0 

    volume = 1 

 

    def size(self, codim): 

        assert codim == 0, 'Invalid codimension (must be 0 but was {})'.format(codim) 

        return 1 

 

    def subentities(self, codim, subentity_codim): 

        assert codim == 0, 'Invalid codimension (must be 0 but was {})'.format(codim) 

        assert subentity_codim == 0, 'Invalid subentity codimension (must be 0 but was {})'.format(subentity_codim) 

        return np.array([0], dtype='int32') 

 

    def subentity_embedding(self, subentity_codim): 

        assert subentity_codim == 0, 'Invalid codimension (must be 0 but was {})'.format(subentity_codim) 

        return np.zeros((0, 0), dtype='int32'), np.zeros((0), dtype='int32') 

 

    def sub_reference_element(self, codim): 

        assert codim == 0, 'Invalid codimension (must be 0 but was {})'.format(codim) 

        return self 

 

    def unit_outer_normals(self): 

        return np.zeros((0)) 

 

    def center(self): 

        return np.zeros((0)) 

 

    def mapped_diameter(self, A): 

        return np.ones(A.shape[:-2]) 

 

    def quadrature_info(self): 

        # of course, the quadrature is of abritrary oder ... 

        return {'gauss': tuple(xrange(42))}, {'gauss': (1,)} 

 

    def quadrature(self, order=None, npoints=None, quadrature_type='default'): 

        if quadrature_type == 'default' or quadrature_type == 'gauss': 

            assert npoints is None or npoints == 1, 'there is only one point in dimension 0!' 

            return np.zeros((1, 0)), np.ones(1) 

        else: 

            raise NotImplementedError('quadrature_type must be "default" or "gauss"') 

 

point = Point() 

 

 

class Line(ReferenceElementInterface): 

 

    dim = 1 

    volume = 1 

 

    def size(self, codim): 

        assert 0 <= codim <= 1, 'Invalid codimension (must be 0 or 1 but was {})'.format(codim) 

        if codim == 0: 

            return 1 

        else: 

            return 2 

 

    def subentities(self, codim, subentity_codim): 

        assert 0 <= codim <= 1, 'Invalid codimension (must be 0 or 1 but was {})'.format(codim) 

        assert codim <= subentity_codim <= 1,\ 

            'Invalid codimension (must be between {} and 1 but was {})'.format(codim, subentity_codim) 

        if codim == 0: 

            return np.arange(self.size(subentity_codim), dtype='int32') 

        else: 

            return np.array(([0], [1]), dtype='int32') 

 

    def subentity_embedding(self, subentity_codim): 

        assert 0 <= subentity_codim <= 1,\ 

            'Invalid codimension (must be 0 or 1 but was {})'.format(subentity_codim) 

        if subentity_codim == 0: 

            return np.ones((1, 1, 1)), np.zeros((1, 1, 1)) 

        else: 

            return np.array((np.zeros((1, 0)), np.zeros((1, 0)))), np.array(([0.], [1.])) 

 

    def sub_reference_element(self, codim): 

        assert 0 <= codim <= 1, 'Invalid codimension (must be 0 or 1 but was {})'.format(codim) 

        if codim == 0: 

            return self 

        else: 

            return point 

 

    def unit_outer_normals(self): 

        return np.array(([-1.], [1.])) 

 

    def center(self): 

        return np.array([0.5]) 

 

    def mapped_diameter(self, A): 

        return np.apply_along_axis(np.linalg.norm, -2, A) 

 

    def quadrature_info(self): 

        return {'gauss': GaussQuadratures.orders}, {'gauss': map(len, GaussQuadratures.points)} 

 

    def quadrature(self, order=None, npoints=None, quadrature_type='default'): 

        if quadrature_type == 'default' or quadrature_type == 'gauss': 

            P, W = GaussQuadratures.quadrature(order, npoints) 

            return P[:, np.newaxis], W 

        else: 

            raise NotImplementedError('quadrature_type must be "default" or "gauss"') 

 

line = Line() 

 

 

class Square(ReferenceElementInterface): 

 

    dim = 2 

    volume = 1 

 

    def __init__(self): 

 

        def tensor_points(P): 

            PP0, PP1 = np.array(np.meshgrid(P, P)) 

            return np.array((PP0.ravel(), PP1.ravel())).T 

 

        def tensor_weights(W): 

            return np.dot(W[:, np.newaxis], W[np.newaxis, :]).ravel() 

        self._quadrature_points = [tensor_points(GaussQuadratures.quadrature(npoints=p + 1)[0]) 

                                   for p in xrange(GaussQuadratures.maxpoints())] 

        self._quadrature_weights = [tensor_weights(GaussQuadratures.quadrature(npoints=p + 1)[1]) 

                                    for p in xrange(GaussQuadratures.maxpoints())] 

        self._quadrature_npoints = np.arange(1, GaussQuadratures.maxpoints() + 1) ** 2 

        self._quadrature_orders = GaussQuadratures.orders 

        self._quadrature_order_map = GaussQuadratures.order_map 

 

    def size(self, codim): 

        assert 0 <= codim <= 2, 'Invalid codimension (must be between 0 and 2 but was {})'.format(codim) 

        if codim == 0: 

            return 1 

        elif codim == 1: 

            return 4 

        elif codim == 2: 

            return 4 

 

    def subentities(self, codim, subentity_codim): 

        assert 0 <= codim <= 2, 'Invalid codimension (must be between 0 and 2 but was {})'.format(codim) 

        assert codim <= subentity_codim <= 2,\ 

            'Invalid codimension (must be between {} and 2 but was {})'.format(codim, subentity_codim) 

        if codim == 0: 

            return np.arange(self.size(subentity_codim), dtype='int32') 

        elif codim == 1: 

            if subentity_codim == 1: 

                return np.array(([0], [1], [2], [3]), dtype='int32') 

            else: 

                return np.array(([0, 1], [1, 2], [2, 3], [3, 0]), dtype='int32') 

        elif codim == 2: 

            return np.array(([0], [1], [2], [3]), dtype='int32') 

 

    def subentity_embedding(self, subentity_codim): 

        assert 0 <= subentity_codim <= 2,\ 

            'Invalid codimension (must betwen 0 and 2 but was {})'.format(subentity_codim) 

        if subentity_codim == 0: 

            return np.eye(2), np.zeros(2) 

        elif subentity_codim == 1: 

            A = np.array((np.array(([1.], [0.])), np.array(([0.], [1.])), 

                          np.array(([-1.], [0.])), np.array(([0.], [-1.])))) 

            B = np.array((np.array([0., 0.]), np.array([1., 0.]), 

                          np.array([1., 1.]), np.array([0., 1.]))) 

            return A, B 

        else: 

            return super(Square, self).subentity_embedding(subentity_codim) 

 

    def sub_reference_element(self, codim): 

        assert 0 <= codim <= 2, 'Invalid codimension (must be between 0 and 2 but was {})'.format(codim) 

        if codim == 0: 

            return self 

        elif codim == 1: 

            return line 

        else: 

            return point 

 

    def unit_outer_normals(self): 

        return np.array(([0., -1.], [1., 0.], [0., 1], [-1., 0.])) 

 

    def center(self): 

        return np.array([0.5, 0.5]) 

 

    def mapped_diameter(self, A): 

        V0 = np.dot(A, np.array([1., 1.])) 

        V1 = np.dot(A, np.array([1., -1])) 

        VN0 = np.apply_along_axis(np.linalg.norm, -1, V0) 

        VN1 = np.apply_along_axis(np.linalg.norm, -1, V1) 

        return np.max((VN0, VN1), axis=0) 

 

    def quadrature_info(self): 

        return {'tensored_gauss': self._quadrature_orders}, {'tensored_gauss': self._quadrature_npoints} 

 

    def quadrature(self, order=None, npoints=None, quadrature_type='default'): 

        if quadrature_type == 'default' or quadrature_type == 'tensored_gauss': 

            assert order is not None or npoints is not None, 'must specify "order" or "npoints"' 

            assert order is None or npoints is None, 'cannot specify "order" and "npoints"' 

            if order is not None: 

                assert 0 <= order <= self._quadrature_order_map.size - 1,\ 

                    ValueError('order {} not implmented'.format(order)) 

                p = self._quadrature_order_map[order] 

            else: 

                assert npoints in self._quadrature_npoints, 'not implemented with {} points'.format(npoints) 

                p = np.where(self._quadrature_npoints == npoints)[0] 

            return self._quadrature_points[p], self._quadrature_weights[p] 

        else: 

            raise NotImplementedError('quadrature_type must be "default" or "tensored_gauss"') 

 

 

square = Square() 

 

 

class Triangle(ReferenceElementInterface): 

 

    dim = 2 

    volume = 0.5 

 

    def size(self, codim): 

        assert 0 <= codim <= 2, 'Invalid codimension (must be between 0 and 2 but was {})'.format(codim) 

        if codim == 0: 

            return 1 

        elif codim == 1: 

            return 3 

        elif codim == 2: 

            return 3 

 

    def subentities(self, codim, subentity_codim): 

        assert 0 <= codim <= 2, 'Invalid codimension (must be between 0 and 2 but was {})'.format(codim) 

        assert codim <= subentity_codim <= 2,\ 

            'Invalid codimension (must be between {} and 2 but was {})'.format(codim, subentity_codim) 

        if codim == 0: 

            return np.arange(self.size(subentity_codim), dtype='int32') 

        elif codim == 1: 

            if subentity_codim == 1: 

                return np.array(([0], [1], [2]), dtype='int32') 

            else: 

                return np.array(([1, 2], [2, 0], [0, 1]), dtype='int32') 

        elif codim == 2: 

            return np.array(([0], [1], [2]), dtype='int32') 

 

    def subentity_embedding(self, subentity_codim): 

        assert 0 <= subentity_codim <= 2,\ 

            'Invalid codimension (must betwen 0 and 2 but was {})'.format(subentity_codim) 

        if subentity_codim == 0: 

            return np.eye(2), np.zeros(2) 

        elif subentity_codim == 1: 

            A = np.array((np.array(([-1.], [1.])), np.array(([0.], [-1.])), 

                          np.array(([1.], [0.])))) 

            B = np.array((np.array([1., 0.]), np.array([0., 1.]), 

                          np.array([0., 0.]))) 

            return A, B 

        else: 

            return super(Triangle, self).subentity_embedding(subentity_codim) 

 

    def sub_reference_element(self, codim): 

        assert 0 <= codim <= 2, 'Invalid codimension (must be between 0 and 2 but was {})'.format(codim) 

        if codim == 0: 

            return self 

        elif codim == 1: 

            return line 

        else: 

            return point 

 

    def unit_outer_normals(self): 

        return np.array(([1., 1.], [-1., 0.], [0., -1.])) 

 

    def center(self): 

        return np.array([1. / 3., 1. / 3.]) 

 

    def mapped_diameter(self, A): 

        V0 = np.dot(A, np.array([-1., 1.])) 

        V1 = np.dot(A, np.array([0., -1.])) 

        V2 = np.dot(A, np.array([1., 0.])) 

        VN0 = np.apply_along_axis(np.linalg.norm, -1, V0) 

        VN1 = np.apply_along_axis(np.linalg.norm, -1, V1) 

        VN2 = np.apply_along_axis(np.linalg.norm, -1, V2) 

        return np.max((VN0, VN1, VN2), axis=0) 

 

    def quadrature_info(self): 

        return ({'center': (1,), 'edge_centers': (2,)}, 

                {'center': (1,), 'edge_centers': (3,)}) 

 

    def quadrature(self, order=None, npoints=None, quadrature_type='default'): 

        assert order is not None or npoints is not None, 'must specify "order" or "npoints"' 

        assert order is None or npoints is None, 'cannot specify "order" and "npoints"' 

        if quadrature_type == 'default': 

            if order == 1 or npoints == 1: 

                quadrature_type = 'center' 

            else: 

                quadrature_type = 'edge_centers' 

 

        if quadrature_type == 'center': 

            assert order is None or order == 1 

            assert npoints is None or npoints == 1 

            return np.array((self.center(),)), np.array(self.volume) 

        elif quadrature_type == 'edge_centers': 

            assert order is None or order <= 2 

            assert npoints is None or npoints == 3 

            #this would work for arbitrary reference elements 

            #L, A = self.subentity_embedding(1) 

            #return np.array(L.dot(self.sub_reference_element().center()) + A), np.ones(3) / len(A) * self.volume 

            return np.array(([0.5, 0.5], [0, 0.5], [0.5, 0])), np.ones(3) / 3 * self.volume 

        else: 

            raise NotImplementedError('quadrature_type must be "center" or "edge_centers"') 

 

 

triangle = Triangle()