File: e-nedelec.cpp

package info (click to toggle)
fenics-basix 0.10.0.post0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,156 kB
  • sloc: cpp: 23,435; python: 10,829; makefile: 43; sh: 26
file content (415 lines) | stat: -rw-r--r-- 15,408 bytes parent folder | download | duplicates (2)
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) 2020 Chris Richardson & Matthew Scroggs
// FEniCS Project
// SPDX-License-Identifier:    MIT

#include "e-nedelec.h"
#include "e-lagrange.h"
#include "e-raviart-thomas.h"
#include "element-families.h"
#include "maps.h"
#include "math.h"
#include "mdspan.hpp"
#include "moments.h"
#include "polyset.h"
#include "quadrature.h"
#include "sobolev-spaces.h"
#include <array>
#include <concepts>
#include <vector>

using namespace basix;

namespace
{
//-----------------------------------------------------------------------------
template <std::floating_point T>
impl::mdarray_t<T, 2> create_nedelec_2d_space(int degree)
{
  // Number of order (degree) vector polynomials
  const std::size_t nv = degree * (degree + 1) / 2;

  // Number of order (degree-1) vector polynomials
  const std::size_t ns0 = (degree - 1) * degree / 2;

  // Number of additional polynomials in Nedelec set
  const std::size_t ns = degree;

  // Tabulate polynomial set at quadrature points
  const auto [_pts, wts] = quadrature::make_quadrature<T>(
      quadrature::type::Default, cell::type::triangle, polyset::type::standard,
      2 * degree);
  impl::mdspan_t<const T, 2> pts(_pts.data(), wts.size(),
                                 _pts.size() / wts.size());
  const auto [_phi, shape] = polyset::tabulate(
      cell::type::triangle, polyset::type::standard, degree, 0, pts);
  impl::mdspan_t<const T, 3> phi(_phi.data(), shape);

  const std::size_t psize = phi.extent(1);

  // Create coefficients for order (degree-1) vector polynomials
  impl::mdarray_t<T, 2> wcoeffs(nv * 2 + ns, psize * 2);
  for (std::size_t i = 0; i < nv; ++i)
  {
    wcoeffs(i, i) = 1.0;
    wcoeffs(nv + i, psize + i) = 1.0;
  }

  // Create coefficients for the additional Nedelec polynomials
  for (std::size_t i = 0; i < ns; ++i)
  {
    for (std::size_t j = nv; j < psize; ++j)
    {
      wcoeffs(2 * nv + i, j) = 0.0;
      wcoeffs(2 * nv + i, j + psize) = 0.0;
      for (std::size_t k = 0; k < wts.size(); ++k)
      {
        T p = phi(0, ns0 + i, k);
        wcoeffs(2 * nv + i, j) += wts[k] * p * pts(k, 1) * phi(0, j, k);
        wcoeffs(2 * nv + i, j + psize) -= wts[k] * p * pts(k, 0) * phi(0, j, k);
      }
    }
  }

  math::orthogonalise<T>(wcoeffs, nv * 2);

  return wcoeffs;
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
impl::mdarray_t<T, 2> create_nedelec_3d_space(int degree)
{
  // Reference tetrahedron
  const std::size_t tdim = 3;

  // Number of order (degree) vector polynomials
  const std::size_t nv = degree * (degree + 1) * (degree + 2) / 6;

  // Number of order (degree-1) vector polynomials
  const std::size_t ns0 = (degree - 1) * degree * (degree + 1) / 6;

  // Number of additional Nedelec polynomials that could be added
  const std::size_t ns = degree * (degree + 1) / 2;

  // Number of polynomials that would be included that are not
  // independent so are removed
  const std::size_t ns_remove = degree * (degree - 1) / 2;

  // Number of dofs in the space, ie size of polynomial set
  const std::size_t ndofs = 6 * degree + 4 * degree * (degree - 1)
                            + (degree - 2) * (degree - 1) * degree / 2;

  // Tabulate polynomial basis at quadrature points
  const auto [_pts, wts] = quadrature::make_quadrature<T>(
      quadrature::type::Default, cell::type::tetrahedron,
      polyset::type::standard, 2 * degree);
  impl::mdspan_t<const T, 2> pts(_pts.data(), wts.size(),
                                 _pts.size() / wts.size());
  const auto [_phi, shape] = polyset::tabulate(
      cell::type::tetrahedron, polyset::type::standard, degree, 0, pts);
  impl::mdspan_t<const T, 3> phi(_phi.data(), shape);
  const std::size_t psize = phi.extent(1);

  // Create coefficients for order (degree-1) polynomials
  impl::mdarray_t<T, 2> wcoeffs(ndofs, psize * tdim);
  for (std::size_t i = 0; i < tdim; ++i)
    for (std::size_t j = 0; j < nv; ++j)
      wcoeffs(i * nv + j, i * psize + j) = 1.0;

  // Create coefficients for additional Nedelec polynomials
  for (std::size_t i = 0; i < ns; ++i)
  {
    for (std::size_t j = nv; j < psize; ++j)
    {
      T w = 0.0;
      for (std::size_t k = 0; k < wts.size(); ++k)
        w += wts[k] * phi(0, ns0 + i, k) * pts(k, 2) * phi(0, j, k);

      // Don't include polynomials (*, *, 0) that are dependent
      if (i >= ns_remove)
        wcoeffs(tdim * nv + i - ns_remove, psize + j) = -w;
      wcoeffs(tdim * nv + i + ns - ns_remove, j) = w;
    }
  }

  for (std::size_t i = 0; i < ns; ++i)
  {
    for (std::size_t j = nv; j < psize; ++j)
    {
      T w = 0.0;
      for (std::size_t k = 0; k < wts.size(); ++k)
        w += wts[k] * phi(0, ns0 + i, k) * pts(k, 1) * phi(0, j, k);
      wcoeffs(tdim * nv + i + ns * 2 - ns_remove, j) = -w;

      // Don't include polynomials (*, *, 0) that are dependent
      if (i >= ns_remove)
        wcoeffs(tdim * nv + i - ns_remove, psize * 2 + j) = w;
    }
  }

  for (std::size_t i = 0; i < ns; ++i)
  {
    for (std::size_t j = nv; j < psize; ++j)
    {
      T w = 0.0;
      for (std::size_t k = 0; k < wts.size(); ++k)
        w += wts[k] * phi(0, ns0 + i, k) * pts(k, 0) * phi(0, j, k);

      wcoeffs(tdim * nv + i + ns - ns_remove, psize * 2 + j) = -w;
      wcoeffs(tdim * nv + i + ns * 2 - ns_remove, psize + j) = w;
    }
  }

  math::orthogonalise<T>(wcoeffs, nv * 3);

  return wcoeffs;
}
//-----------------------------------------------------------------------------
} // namespace

//-----------------------------------------------------------------------------
template <std::floating_point T>
FiniteElement<T> element::create_nedelec(cell::type celltype, int degree,
                                         lagrange_variant lvariant,
                                         bool discontinuous)
{
  if (degree < 1)
    throw std::runtime_error("Degree must be at least 1");

  const std::size_t tdim = cell::topological_dimension(celltype);

  std::array<std::vector<impl::mdarray_t<T, 2>>, 4> x;
  std::array<std::vector<impl::mdarray_t<T, 4>>, 4> M;

  {
    const std::size_t num_ent = cell::num_sub_entities(celltype, 0);
    x[0] = std::vector(num_ent, impl::mdarray_t<T, 2>(0, tdim));
    M[0] = std::vector(num_ent, impl::mdarray_t<T, 4>(0, tdim, 0, 1));
  }

  std::vector<T> wcoeffs;
  std::array<std::size_t, 2> wshape;
  switch (celltype)
  {
  case cell::type::triangle:
  {
    impl::mdarray_t<T, 2> w = create_nedelec_2d_space<T>(degree);
    wshape = {w.extent(0), w.extent(1)};
    wcoeffs.resize(wshape[0] * wshape[1]);
    std::copy_n(w.data(), w.size(), wcoeffs.data());
    break;
  }
  case cell::type::tetrahedron:
  {
    impl::mdarray_t<T, 2> w = create_nedelec_3d_space<T>(degree);
    wshape = {w.extent(0), w.extent(1)};
    wcoeffs.resize(wshape[0] * wshape[1]);
    std::copy_n(w.data(), w.size(), wcoeffs.data());
    break;
  }
  default:
    throw std::runtime_error("Invalid celltype in Nedelec");
  }

  // Integral representation for the boundary (edge) dofs
  {
    FiniteElement edge_space = element::create_lagrange<T>(
        cell::type::interval, degree - 1, lvariant, true);
    auto [_x, xshape, _M, Mshape] = moments::make_tangent_integral_moments<T>(
        edge_space, celltype, polyset::type::standard, tdim, 2 * degree - 1);
    assert(_x.size() == _M.size());
    for (std::size_t i = 0; i < _x.size(); ++i)
    {
      x[1].emplace_back(std::array{xshape[0], xshape[1]}, _x[i]);
      M[1].emplace_back(std::array{Mshape[0], Mshape[1], Mshape[2], Mshape[3]},
                        _M[i]);
    }
  }

  // Face dofs
  if (degree > 1)
  {
    FiniteElement face_space = element::create_lagrange<T>(
        cell::type::triangle, degree - 2, lvariant, true);
    auto [_x, xshape, _M, Mshape] = moments::make_integral_moments<T>(
        face_space, celltype, polyset::type::standard, tdim, 2 * degree - 2);
    assert(_x.size() == _M.size());
    for (std::size_t i = 0; i < _x.size(); ++i)
    {
      x[2].emplace_back(std::array{xshape[0], xshape[1]}, _x[i]);
      M[2].emplace_back(std::array{Mshape[0], Mshape[1], Mshape[2], Mshape[3]},
                        _M[i]);
    }
  }
  else
  {
    const std::size_t num_ent = cell::num_sub_entities(celltype, 2);
    x[2] = std::vector(num_ent, impl::mdarray_t<T, 2>(0, tdim));
    M[2] = std::vector(num_ent, impl::mdarray_t<T, 4>(0, tdim, 0, 1));
  }

  // Volume dofs
  if (tdim == 3)
  {
    if (degree > 2 and tdim == 3)
    {
      auto [_x, xshape, _M, Mshape] = moments::make_integral_moments<T>(
          element::create_lagrange<T>(cell::type::tetrahedron, degree - 3,
                                      lvariant, true),
          cell::type::tetrahedron, polyset::type::standard, 3, 2 * degree - 3);
      assert(_x.size() == _M.size());
      for (std::size_t i = 0; i < _x.size(); ++i)
      {
        x[3].emplace_back(std::array{xshape[0], xshape[1]}, _x[i]);
        M[3].emplace_back(
            std::array{Mshape[0], Mshape[1], Mshape[2], Mshape[3]}, _M[i]);
      }
    }
    else
    {
      const std::size_t num_ent = cell::num_sub_entities(celltype, 3);
      x[3] = std::vector(num_ent, impl::mdarray_t<T, 2>(0, tdim));
      M[3] = std::vector(num_ent, impl::mdarray_t<T, 4>(0, tdim, 0, 1));
    }
  }

  std::array<std::vector<mdspan_t<const T, 2>>, 4> xview = impl::to_mdspan(x);
  std::array<std::vector<mdspan_t<const T, 4>>, 4> Mview = impl::to_mdspan(M);
  std::array<std::vector<std::vector<T>>, 4> xbuffer;
  std::array<std::vector<std::vector<T>>, 4> Mbuffer;
  if (discontinuous)
  {
    // std::tie(x, M) = element::make_discontinuous(x, M, tdim, tdim);
    std::array<std::vector<std::array<std::size_t, 2>>, 4> xshape;
    std::array<std::vector<std::array<std::size_t, 4>>, 4> Mshape;
    std::tie(xbuffer, xshape, Mbuffer, Mshape)
        = element::make_discontinuous(xview, Mview, tdim, tdim);
    xview = impl::to_mdspan(xbuffer, xshape);
    Mview = impl::to_mdspan(Mbuffer, Mshape);
  }

  sobolev::space space
      = discontinuous ? sobolev::space::L2 : sobolev::space::HCurl;
  return FiniteElement<T>(
      element::family::N1E, celltype, polyset::type::standard, degree, {tdim},
      impl::mdspan_t<T, 2>(wcoeffs.data(), wshape), xview, Mview, 0,
      maps::type::covariantPiola, space, discontinuous, degree - 1, degree,
      lvariant, element::dpc_variant::unset);
}
//-----------------------------------------------------------------------------
template <std::floating_point T>
FiniteElement<T> element::create_nedelec2(cell::type celltype, int degree,
                                          lagrange_variant lvariant,
                                          bool discontinuous)
{
  if (celltype != cell::type::triangle and celltype != cell::type::tetrahedron)
    throw std::runtime_error("Invalid celltype in Nedelec");

  if (degree < 1)
    throw std::runtime_error("Degree must be at least 1");

  std::array<std::vector<impl::mdarray_t<T, 2>>, 4> x;
  std::array<std::vector<impl::mdarray_t<T, 4>>, 4> M;

  const std::size_t tdim = cell::topological_dimension(celltype);
  {
    const std::size_t num_ent = cell::num_sub_entities(celltype, 0);
    x[0] = std::vector(num_ent, impl::mdarray_t<T, 2>(0, tdim));
    M[0] = std::vector(num_ent, impl::mdarray_t<T, 4>(0, tdim, 0, 1));
  }

  // Integral representation for the edge dofs

  {
    FiniteElement edge_space = element::create_lagrange<T>(
        cell::type::interval, degree, lvariant, true);
    auto [_x, xshape, _M, Mshape] = moments::make_tangent_integral_moments<T>(
        edge_space, celltype, polyset::type::standard, tdim, 2 * degree);
    assert(_x.size() == _M.size());
    for (std::size_t i = 0; i < _x.size(); ++i)
    {
      x[1].emplace_back(std::array{xshape[0], xshape[1]}, _x[i]);
      M[1].emplace_back(std::array{Mshape[0], Mshape[1], Mshape[2], Mshape[3]},
                        _M[i]);
    }
  }

  if (degree > 1)
  {
    // Integral moments on faces
    FiniteElement face_space = element::create_rt<T>(
        cell::type::triangle, degree - 1, lvariant, true);
    auto [_x, xshape, _M, Mshape] = moments::make_dot_integral_moments<T>(
        face_space, celltype, polyset::type::standard, tdim, 2 * degree - 1);
    assert(_x.size() == _M.size());
    for (std::size_t i = 0; i < _x.size(); ++i)
    {
      x[2].emplace_back(std::array{xshape[0], xshape[1]}, _x[i]);
      M[2].emplace_back(std::array{Mshape[0], Mshape[1], Mshape[2], Mshape[3]},
                        _M[i]);
    }
  }
  else
  {
    const std::size_t num_ent = cell::num_sub_entities(celltype, 2);
    x[2] = std::vector(num_ent, impl::mdarray_t<T, 2>(0, tdim));
    M[2] = std::vector(num_ent, impl::mdarray_t<T, 4>(0, tdim, 0, 1));
  }

  if (tdim == 3)
  {
    if (degree > 2)
    {
      auto [_x, xshape, _M, Mshape] = moments::make_dot_integral_moments<T>(
          element::create_rt<T>(cell::type::tetrahedron, degree - 2, lvariant,
                                true),
          celltype, polyset::type::standard, tdim, 2 * degree - 2);
      assert(_x.size() == _M.size());
      for (std::size_t i = 0; i < _x.size(); ++i)
      {
        x[3].emplace_back(std::array{xshape[0], xshape[1]}, _x[i]);
        M[3].emplace_back(
            std::array{Mshape[0], Mshape[1], Mshape[2], Mshape[3]}, _M[i]);
      }
    }
    else
    {
      const std::size_t num_ent = cell::num_sub_entities(celltype, 3);
      x[3] = std::vector(num_ent, impl::mdarray_t<T, 2>(0, tdim));
      M[3] = std::vector(num_ent, impl::mdarray_t<T, 4>(0, tdim, 0, 1));
    }
  }

  std::array<std::vector<mdspan_t<const T, 2>>, 4> xview = impl::to_mdspan(x);
  std::array<std::vector<mdspan_t<const T, 4>>, 4> Mview = impl::to_mdspan(M);
  std::array<std::vector<std::vector<T>>, 4> xbuffer;
  std::array<std::vector<std::vector<T>>, 4> Mbuffer;
  if (discontinuous)
  {
    std::array<std::vector<std::array<std::size_t, 2>>, 4> xshape;
    std::array<std::vector<std::array<std::size_t, 4>>, 4> Mshape;
    std::tie(xbuffer, xshape, Mbuffer, Mshape)
        = element::make_discontinuous(xview, Mview, tdim, tdim);
    xview = impl::to_mdspan(xbuffer, xshape);
    Mview = impl::to_mdspan(Mbuffer, Mshape);
  }

  const std::size_t psize
      = polyset::dim(celltype, polyset::type::standard, degree);
  return FiniteElement<T>(
      element::family::N2E, celltype, polyset::type::standard, degree, {tdim},
      impl::mdspan_t<T, 2>(math::eye<T>(tdim * psize).data(), tdim * psize,
                           tdim * psize),
      xview, Mview, 0, maps::type::covariantPiola, sobolev::space::HCurl,
      discontinuous, degree, degree, lvariant, element::dpc_variant::unset);
}
//-----------------------------------------------------------------------------
template FiniteElement<float> element::create_nedelec(cell::type, int,
                                                      lagrange_variant, bool);
template FiniteElement<double> element::create_nedelec(cell::type, int,
                                                       lagrange_variant, bool);

template FiniteElement<float> element::create_nedelec2(cell::type, int,
                                                       lagrange_variant, bool);
template FiniteElement<double> element::create_nedelec2(cell::type, int,
                                                        lagrange_variant, bool);
//-----------------------------------------------------------------------------