Debian Codegraph @

dolfin/fem/SystemAssembler.cpp

raw
1 // Copyright (C) 2008-2015 Kent-Andre Mardal and Garth N. Wells
2 //
3 // This file is part of DOLFIN.
4 //
5 // DOLFIN is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // DOLFIN is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // Modified by Anders Logg 2008-2013
19 // Modified by Joachim B Haga 2012
20 // Modified by Jan Blechta 2013
21 // Modified by Martin Alnaes 2013-2015
22 // Modified by Cecile Daversin-Catty 2018
23
24 #include <algorithm>
25 #include <array>
26 #include <Eigen/Dense>
27 #include <boost/multi_array.hpp>
28
29 #include <dolfin/common/ArrayView.h>
30 #include <dolfin/common/Timer.h>
31 #include <dolfin/common/types.h>
32 #include <dolfin/function/GenericFunction.h>
33 #include <dolfin/function/FunctionSpace.h>
34 #include <dolfin/la/GenericMatrix.h>
35 #include <dolfin/la/GenericVector.h>
36 #include <dolfin/log/log.h>
37 #include <dolfin/log/Progress.h>
38 #include <dolfin/mesh/Cell.h>
39 #include <dolfin/mesh/Mesh.h>
40 #include <dolfin/mesh/Facet.h>
41 #include <dolfin/mesh/MeshFunction.h>
42 #include <dolfin/mesh/SubDomain.h>
43 #include "AssemblerBase.h"
44 #include "DirichletBC.h"
45 #include "FiniteElement.h"
46 #include "Form.h"
47 #include "GenericDofMap.h"
48 #include "UFC.h"
49 #include "SystemAssembler.h"
50
51 using namespace dolfin;
52
53 //-----------------------------------------------------------------------------
54 SystemAssembler::SystemAssembler(std::shared_ptr<const Form> a,
55 std::shared_ptr<const Form> L,
56 std::vector<std::shared_ptr<const DirichletBC>> bcs)
57 : _a({a}), _l({L}), _bcs({bcs})
58 {
59 // Check forms for correct dimensions
60 check_forms(_a, _l);
61 }
62 //-----------------------------------------------------------------------------
63 SystemAssembler::SystemAssembler(std::vector<std::shared_ptr<const Form>> a,
64 std::vector<std::shared_ptr<const Form>> L,
65 std::vector<std::vector<std::shared_ptr<const DirichletBC>>> bcs)
66 : _a(a), _l(L), _bcs(bcs)
67 {
68 // Check forms for correct dimensions
69 check_forms(_a, _l);
70 }
71 //-----------------------------------------------------------------------------
72 SystemAssembler::SystemAssembler(std::vector<std::shared_ptr<const Form>> a,
73 std::vector<std::shared_ptr<const Form>> L,
74 std::vector<std::shared_ptr<const DirichletBC>> bcs0,
75 std::vector<std::shared_ptr<const DirichletBC>> bcs1)
76 : _a(a), _l(L), _bcs({bcs0, bcs1})
77 {
78 // Check forms for correct dimensions
79 check_forms(_a, _l);
80 }
81 //-----------------------------------------------------------------------------
82 void SystemAssembler::assemble(std::vector<std::shared_ptr<GenericMatrix>> A,
83 std::vector<std::shared_ptr<GenericVector>> b)
84 {
85 if (A.size() != _a.size())
86 {
87 dolfin_error("SystemAssembler.cpp",
88 "assemble system",
89 "Incorrect number of matrices for forms");
90 }
91
92 if (b.size() != _l.size())
93 {
94 dolfin_error("SystemAssembler.cpp",
95 "assemble system",
96 "Incorrect number of vectors for forms");
97 }
98
99 if (_bcs.size() != _l.size())
100 {
101 dolfin_error("SystemAssembler.cpp",
102 "assemble system",
103 "Incorrect number of BCs for forms");
104 }
105
106 // Make sure b does not get reset each time
107 add_values = true;
108
109 std::size_t nrows = b.size();
110 dolfin_assert(A.size()%nrows == 0);
111 std::size_t ncols = A.size()/nrows;
112
113 std::vector<std::vector<std::shared_ptr<const DirichletBC>>> bcs;
114 for (std::size_t i = 0; i != nrows; ++i)
115 {
116 bool assemble_rhs = true;
117 for (std::size_t j = 0; j != ncols; ++j)
118 {
119 std::size_t k = j + i*ncols;
120 // Only assemble RHS once, but apply BCs each time
121 if (i == j)
122 bcs = {_bcs[i]};
123 else
124 bcs = {_bcs[i], _bcs[j]};
125 // Some matrices and/or forms may be NULL
126 if (_a[k] and A[k])
127 {
128 assemble(&*A[k], &*b[i], NULL, _a[k], _l[i], bcs, assemble_rhs);
129 assemble_rhs = false;
130 }
131 else if (i == j and bcs[0].size() > 0)
132 {
133 dolfin_error("SystemAssembler.cpp",
134 "assemble block system",
135 "Boundary conditions cannot be specified on NULL block");
136 }
137 }
138 }
139 }
140 //-----------------------------------------------------------------------------
141 void SystemAssembler::assemble(GenericMatrix& A, GenericVector& b)
142 {
143 dolfin_assert(_l.size() == 1);
144 dolfin_assert(_a.size() == 1);
145
146 assemble(&A, &b, NULL, _a[0], _l[0], _bcs, true);
147 }
148 //-----------------------------------------------------------------------------
149 void SystemAssembler::assemble(GenericMatrix& A)
150 {
151 dolfin_assert(_l.size() == 1);
152 dolfin_assert(_a.size() == 1);
153
154 assemble(&A, NULL, NULL, _a[0], _l[0], _bcs, true);
155 }
156 //-----------------------------------------------------------------------------
157 void SystemAssembler::assemble(GenericVector& b)
158 {
159 dolfin_assert(_l.size() == 1);
160 dolfin_assert(_a.size() == 1);
161
162 assemble(NULL, &b, NULL, _a[0], _l[0], _bcs, true);
163 }
164 //-----------------------------------------------------------------------------
165 void SystemAssembler::assemble(GenericMatrix& A, GenericVector& b,
166 const GenericVector& x0)
167 {
168 dolfin_assert(_l.size() == 1);
169 dolfin_assert(_a.size() == 1);
170
171 assemble(&A, &b, &x0, _a[0], _l[0], _bcs, true);
172 }
173 //-----------------------------------------------------------------------------
174 void SystemAssembler::assemble(GenericVector& b, const GenericVector& x0)
175 {
176 dolfin_assert(_l.size() == 1);
177 dolfin_assert(_a.size() == 1);
178
179 assemble(NULL, &b, &x0, _a[0], _l[0], _bcs, true);
180 }
181 //-----------------------------------------------------------------------------
182 void SystemAssembler::check_forms(std::vector<std::shared_ptr<const Form>> a,
183 std::vector<std::shared_ptr<const Form>> L)
184 {
185 // Check that a is a bilinear form
186 for (const auto &form : a)
187 {
188 if (form)
189 {
190 if (form->rank() != 2)
191 {
192 dolfin_error("SystemAssembler.cpp",
193 "assemble system",
194 "expected a bilinear form for a");
195 }
196 }
197 }
198
199 // Check that L is a linear form
200 for (const auto &form : L)
201 {
202 if (form)
203 {
204 if (form->rank() != 1)
205 {
206 dolfin_error("SystemAssembler.cpp",
207 "assemble system",
208 "expected a linear form for L");
209 }
210 }
211 }
212
213 std::size_t nrows = L.size();
214 dolfin_assert(a.size()%nrows == 0);
215 std::size_t ncols = a.size()/nrows;
216
217 for (std::size_t i = 0; i != nrows; ++i)
218 {
219 for (std::size_t j = 0; j != ncols; ++j)
220 {
221 const std::size_t k = j + i*ncols;
222 if(a[k])
223 {
224 auto fs_a = a[k]->function_space(0);
225 auto fs_L = L[i]->function_space(0);
226 if (fs_a != fs_L)
227 {
228 dolfin_error("SystemAssembler.cpp",
229 "create SystemAssembler",
230 "expected forms (a, L) to share a FunctionSpace");
231 }
232 }
233 }
234 }
235
236 }
237 //-----------------------------------------------------------------------------
238 std::shared_ptr<const MeshFunction<std::size_t>>
239 _pick_one_meshfunction(std::string name,
240 std::shared_ptr<const MeshFunction<std::size_t>> a,
241 std::shared_ptr<const MeshFunction<std::size_t>> b)
242 {
243 if ((a && b) && a != b)
244 {
245 warning("Bilinear and linear forms do not have same %s subdomains in "
246 "SystemAssembler. Taking %s subdomains from bilinear form",
247 name.c_str(), name.c_str());
248 }
249 return a ? a : b;
250 }
251 //-----------------------------------------------------------------------------
252 bool SystemAssembler::check_functionspace_for_bc
253 (std::shared_ptr<const FunctionSpace> fs,
254 std::shared_ptr<const DirichletBC> bc) const
255 {
256 return fs->contains(*(bc->function_space()));
257 }
258 //-----------------------------------------------------------------------------
259 void SystemAssembler::assemble(GenericMatrix* A, GenericVector* b,
260 const GenericVector* x0,
261 std::shared_ptr<const Form> a,
262 std::shared_ptr<const Form> L,
263 std::vector<std::vector<std::shared_ptr<const DirichletBC>>> bcs,
264 bool integrate_rhs)
265 {
266 dolfin_assert(a);
267 dolfin_assert(L);
268
269 // Set timer
270 Timer timer("Assemble system");
271
272 // Get mesh
273 dolfin_assert(a->mesh());
274 const Mesh& mesh = *(a->mesh());
275 dolfin_assert(mesh.ordered());
276
277 // Get cell domains
278 std::shared_ptr<const MeshFunction<std::size_t>> cell_domains
279 = _pick_one_meshfunction("cell_domains", a->cell_domains(),
280 L->cell_domains());
281
282 // Get exterior facet domains
283 std::shared_ptr<const MeshFunction<std::size_t>> exterior_facet_domains
284 = _pick_one_meshfunction("exterior_facet_domains",
285 a->exterior_facet_domains(),
286 L->exterior_facet_domains());
287
288 // Get interior facet domains
289 std::shared_ptr<const MeshFunction<std::size_t>> interior_facet_domains
290 = _pick_one_meshfunction("interior_facet_domains",
291 a->interior_facet_domains(),
292 L->interior_facet_domains());
293
294 // Check forms
295 AssemblerBase::check(*a);
296 AssemblerBase::check(*L);
297
298 // Check that we have a bilinear and a linear form
299 dolfin_assert(a->rank() == 2);
300 dolfin_assert(L->rank() == 1);
301
302 // Check that forms share a function space
303 if (*a->function_space(0) != *L->function_space(0))
304 {
305 dolfin_error("SystemAssembler.cpp",
306 "assemble system",
307 "expected forms (a, L) to share a FunctionSpace");
308 }
309
310 // Create data structures for local assembly data
311 UFC A_ufc(*a), b_ufc(*L);
312
313 // Raise error for Point integrals
314 if (A_ufc.form.has_vertex_integrals() || b_ufc.form.has_vertex_integrals())
315 {
316 dolfin_error("SystemAssembler.cpp",
317 "assemble system",
318 "Point integrals are not supported (yet)");
319 }
320
321 // Gather UFC objects
322 std::array<UFC*, 2> ufc = { { &A_ufc, &b_ufc} } ;
323
324 // Initialize global tensors
325 if (A)
326 init_global_tensor(*A, *a);
327 if (b)
328 init_global_tensor(*b, *L);
329
330 // Gather tensors
331 std::array<GenericTensor*, 2> tensors = { {A, b} };
332
333 // Allocate data
334 Scratch data(*a, *L);
335
336 // Bin boundary conditions according to which form they apply to
337 std::vector<DirichletBC::Map> boundary_values(bcs.size());
338
339 for (unsigned int axis = 0; axis != bcs.size(); ++axis)
340 {
341 for (auto &bc : bcs[axis])
342 {
343 if (!check_functionspace_for_bc(a->function_space(axis), bc))
344 {
345 dolfin_error("SystemAssembler.cpp",
346 "match boundary condition to function space",
347 "Function space on axis %d does not contain BC space", axis);
348 }
349 bc->get_boundary_values(boundary_values[axis]);
350 if (MPI::size(mesh.mpi_comm()) > 1 && bc->method() != "pointwise")
351 bc->gather(boundary_values[axis]);
352 }
353 }
354
355 // Modify boundary values for incremental (typically nonlinear)
356 // problems
357 // FIXME: not sure what happens when rectangular==true,
358 // should we raise "not implemented error" here?
359 if (x0)
360 {
361 dolfin_assert(x0->size()
362 == a->function_space(1)->dofmap()->global_dimension());
363
364 const std::size_t num_bc_dofs = boundary_values[0].size();
365 std::vector<dolfin::la_index> bc_indices;
366 std::vector<double> bc_values;
367 bc_indices.reserve(num_bc_dofs);
368 bc_values.reserve(num_bc_dofs);
369
370 // Build list of boundary dofs and values
371 for (const auto &bv : boundary_values[0])
372 {
373 bc_indices.push_back(bv.first);
374 bc_values.push_back(bv.second);
375 }
376
377 // Modify bc values
378 std::vector<double> x0_values(num_bc_dofs);
379 x0->get_local(x0_values.data(), num_bc_dofs, bc_indices.data());
380 for (std::size_t i = 0; i < num_bc_dofs; i++)
381 boundary_values[0][bc_indices[i]] = x0_values[i] - bc_values[i];
382 }
383
384 // Check whether we should do cell-wise or facet-wise assembly
385 if (!ufc[0]->form.has_interior_facet_integrals()
386 && !ufc[1]->form.has_interior_facet_integrals())
387 {
388 // Assemble cell-wise (no interior facet integrals)
389 cell_wise_assembly(tensors, ufc, data, boundary_values,
390 cell_domains, exterior_facet_domains,
391 integrate_rhs);
392 }
393 else
394 {
395 if (!integrate_rhs)
396 {
397 dolfin_error("SystemAssembler.cpp",
398 "assemble block system",
399 "Facet-wise assembly of blocked matrices not yet supported");
400 }
401
402 // Assemble facet-wise (including cell assembly)
403 facet_wise_assembly(tensors, ufc, data, boundary_values,
404 cell_domains, exterior_facet_domains,
405 interior_facet_domains);
406 }
407
408 // Finalise assembly
409 if (finalize_tensor)
410 {
411 if (A)
412 A->apply("add");
413 if (b)
414 b->apply("add");
415 }
416 }
417 //-----------------------------------------------------------------------------
418 void SystemAssembler::cell_wise_assembly(
419 std::array<GenericTensor*, 2>& tensors,
420 std::array<UFC*, 2>& ufc,
421 Scratch& data,
422 const std::vector<DirichletBC::Map>& boundary_values,
423 std::shared_ptr<const MeshFunction<std::size_t>> cell_domains,
424 std::shared_ptr<const MeshFunction<std::size_t>> exterior_facet_domains,
425 bool integrate_rhs)
426 {
427 // Extract mesh
428 dolfin_assert(ufc[0]->dolfin_form.mesh());
429 const Mesh& mesh = *(ufc[0]->dolfin_form.mesh());
430
431 // Initialize entities if using external facet integrals
432 dolfin_assert(mesh.ordered());
433 bool has_exterior_facet_integrals=ufc[0]->form.has_exterior_facet_integrals()
434 || ufc[1]->form.has_exterior_facet_integrals();
435 if (has_exterior_facet_integrals)
436 {
437 // Compute facets and facet-cell connectivity if not already computed
438 const std::size_t D = mesh.topology().dim();
439 mesh.init(D - 1);
440 mesh.init(D - 1, D);
441 }
442
443 // Collect pointers to dof maps
444 std::array<std::vector<const GenericDofMap*>, 2> dofmaps;
445 for (std::size_t i = 0; i < 2; ++i)
446 dofmaps[0].push_back(ufc[0]->dolfin_form.function_space(i)->dofmap().get());
447 dofmaps[1].push_back(ufc[1]->dolfin_form.function_space(0)->dofmap().get());
448
449 // Vector to hold dof map for a cell
450 std::array<std::vector<ArrayView<const dolfin::la_index>>, 2> cell_dofs
451 = { {std::vector<ArrayView<const dolfin::la_index>>(2),
452 std::vector<ArrayView<const dolfin::la_index>>(1)} };
453
454 // Create pointers to hold integral objects
455 std::array<const ufc::cell_integral*, 2> cell_integrals
456 = { {ufc[0]->default_cell_integral.get(),
457 ufc[1]->default_cell_integral.get()} };
458
459 std::array<const ufc::exterior_facet_integral*, 2> exterior_facet_integrals
460 = { { ufc[0]->default_exterior_facet_integral.get(),
461 ufc[1]->default_exterior_facet_integral.get()} };
462
463 // Check whether integrals are domain-dependent
464 bool use_cell_domains = cell_domains && !cell_domains->empty();
465 bool use_exterior_facet_domains
466 = exterior_facet_domains && !exterior_facet_domains->empty();
467
468 // Iterate over all cells
469 ufc::cell ufc_cell;
470 std::vector<double> coordinate_dofs;
471 Progress p("Assembling system (cell-wise)", mesh.num_cells());
472
473 std::size_t nforms = integrate_rhs ? 2 : 1;
474 for (CellIterator cell(mesh); !cell.end(); ++cell)
475 {
476 // Check that cell is not a ghost
477 dolfin_assert(!cell->is_ghost());
478
479 // Get cell vertex coordinates
480 cell->get_coordinate_dofs(coordinate_dofs);
481
482 // Get UFC cell data
483 cell->get_cell_data(ufc_cell);
484
485 // Loop over lhs and then rhs contributions
486 for (std::size_t form = 0; form < nforms; ++form)
487 {
488 // Don't need to assemble rhs if only system matrix is required
489 if (form == 1 && !tensors[form])
490 continue;
491
492 // Get rank (lhs=2, rhs=1)
493 const std::size_t rank = (form == 0) ? 2 : 1;
494
495 // Zero data
496 std::fill(data.Ae[form].begin(), data.Ae[form].end(), 0.0);
497
498 // Get cell integrals for sub domain (if any)
499 if (use_cell_domains)
500 {
501 const std::size_t domain = (*cell_domains)[*cell];
502 cell_integrals[form] = ufc[form]->get_cell_integral(domain);
503 }
504
505 // Get local-to-global dof maps for cell
506 for (std::size_t dim = 0; dim < rank; ++dim)
507 {
508 auto dmap = dofmaps[form][dim]->cell_dofs(cell->index());
509 cell_dofs[form][dim].set(dmap.size(), dmap.data());
510 }
511
512 // Compute cell tensor (if required)
513 bool tensor_required;
514 if (rank == 2) // form == 0
515 {
516 tensor_required = cell_matrix_required(tensors[form],
517 cell_integrals[form],
518 boundary_values,
519 cell_dofs[form][1]);
520 }
521 else
522 tensor_required = tensors[form] && cell_integrals[form];
523
524 if (tensor_required)
525 {
526 // Update to current cell
527 ufc[form]->update(*cell, coordinate_dofs, ufc_cell,
528 cell_integrals[form]->enabled_coefficients());
529
530 // Tabulate cell tensor
531 cell_integrals[form]->tabulate_tensor(ufc[form]->A.data(),
532 ufc[form]->w(),
533 coordinate_dofs.data(),
534 ufc_cell.orientation);
535 for (std::size_t i = 0; i < data.Ae[form].size(); ++i)
536 data.Ae[form][i] += ufc[form]->A[i];
537 }
538
539 // Compute exterior facet integral if present
540 if (has_exterior_facet_integrals)
541 {
542 for (FacetIterator facet(*cell); !facet.end(); ++facet)
543 {
544 // Only consider exterior facets
545 if (!facet->exterior())
546 continue;
547
548 // Get exterior facet integrals for sub domain (if any)
549 if (use_exterior_facet_domains)
550 {
551 const std::size_t domain = (*exterior_facet_domains)[*facet];
552 exterior_facet_integrals[form]
553 = ufc[form]->get_exterior_facet_integral(domain);
554 }
555
556 // Skip if there are no integrals
557 if (!exterior_facet_integrals[form])
558 continue;
559
560 // Extract local facet index
561 const std::size_t local_facet = cell->index(*facet);
562
563 // Determine if tensor needs to be computed
564 bool tensor_required;
565 if (rank == 2) // form == 0
566 {
567 tensor_required
568 = cell_matrix_required(tensors[form],
569 exterior_facet_integrals[form],
570 boundary_values,
571 cell_dofs[form][1]);
572 }
573 else
574 tensor_required = tensors[form];
575
576 // Add exterior facet tensor
577 if (tensor_required)
578 {
579 // Update to current cell
580 cell->get_cell_data(ufc_cell);
581 ufc[form]->update(*cell, coordinate_dofs, ufc_cell,
582 exterior_facet_integrals[form]->enabled_coefficients());
583
584 // Tabulate exterior facet tensor
585 exterior_facet_integrals[form]->tabulate_tensor(ufc[form]->A.data(),
586 ufc[form]->w(),
587 coordinate_dofs.data(),
588 local_facet,
589 ufc_cell.orientation);
590 for (std::size_t i = 0; i < data.Ae[form].size(); i++)
591 data.Ae[form][i] += ufc[form]->A[i];
592 }
593 }
594 }
595 }
596
597 // If RHS has not been integrated, still want to add BC terms
598 if (!integrate_rhs)
599 {
600 auto dmap = dofmaps[1][0]->cell_dofs(cell->index());
601 cell_dofs[1][0].set(dmap.size(), dmap.data());
602 std::fill(data.Ae[1].begin(), data.Ae[1].end(), 0.0);
603 }
604
605 // Modify local matrix/element for Dirichlet boundary conditions
606 apply_bc(data.Ae[0].data(), data.Ae[1].data(), boundary_values,
607 cell_dofs[0][0], cell_dofs[0][1]);
608
609 // Add entries to global tensor
610 for (std::size_t form = 0; form < 2; ++form)
611 {
612 if (tensors[form])
613 tensors[form]->add_local(data.Ae[form].data(), cell_dofs[form]);
614 }
615
616 p++;
617 }
618
619 }
620 //-----------------------------------------------------------------------------
621 void SystemAssembler::facet_wise_assembly(
622 std::array<GenericTensor*, 2>& tensors,
623 std::array<UFC*, 2>& ufc,
624 Scratch& data,
625 const std::vector<DirichletBC::Map>& boundary_values,
626 std::shared_ptr<const MeshFunction<std::size_t>> cell_domains,
627 std::shared_ptr<const MeshFunction<std::size_t>> exterior_facet_domains,
628 std::shared_ptr<const MeshFunction<std::size_t>> interior_facet_domains)
629 {
630 // Extract mesh
631 dolfin_assert(ufc[0]->dolfin_form.mesh());
632 const Mesh& mesh = *(ufc[0]->dolfin_form.mesh());
633
634 // Sanity check of ghost mode (proper check in AssemblerBase::check)
635 dolfin_assert(mesh.ghost_mode() == "shared_vertex"
636 || mesh.ghost_mode() == "shared_facet"
637 || MPI::size(mesh.mpi_comm()) == 1);
638
639 // Compute facets and facet - cell connectivity if not already
640 // computed
641 const std::size_t D = mesh.topology().dim();
642 mesh.init(D - 1);
643 mesh.init(D - 1, D);
644
645 // Get my MPI rank
646 const int my_mpi_rank = MPI::rank(mesh.mpi_comm());
647
648 // Collect pointers to dof maps
649 std::array<std::vector<const GenericDofMap*>, 2> dofmaps;
650 for (std::size_t i = 0; i < 2; ++i)
651 dofmaps[0].push_back(ufc[0]->dolfin_form.function_space(i)->dofmap().get());
652 dofmaps[1].push_back(ufc[1]->dolfin_form.function_space(0)->dofmap().get());
653
654 // Cell dofmaps [form][cell][form dim]
655 std::array<std::array<std::vector<ArrayView<const dolfin::la_index>>,
656 2>, 2> cell_dofs;
657 cell_dofs[0][0].resize(2);
658 cell_dofs[0][1].resize(2);
659 cell_dofs[1][0].resize(1);
660 cell_dofs[1][1].resize(1);
661
662 std::array<Cell, 2> cell;
663 std::array<std::size_t, 2> cell_index;
664 std::array<std::size_t, 2> local_facet;
665
666 // Vectors to hold dofs for macro cells
667 std::array<std::vector<std::vector<dolfin::la_index>>, 2> macro_dofs;
668 macro_dofs[0].resize(2);
669 macro_dofs[1].resize(1);
670
671 // Holder for number of dofs in macro-dofmap
672 std::vector<std::size_t> num_dofs(2);
673
674 // Holders for UFC integrals
675 std::array<const ufc::cell_integral*, 2> cell_integrals
676 = { { ufc[0]->default_cell_integral.get(),
677 ufc[1]->default_cell_integral.get() } };
678 std::array<const ufc::exterior_facet_integral*, 2> exterior_facet_integrals
679 = { { ufc[0]->default_exterior_facet_integral.get(),
680 ufc[1]->default_exterior_facet_integral.get() } };
681 std::array<const ufc::interior_facet_integral*, 2> interior_facet_integrals
682 = { { ufc[0]->default_interior_facet_integral.get(),
683 ufc[1]->default_interior_facet_integral.get() } };
684
685 // Check whether integrals are domain-dependent
686 bool use_cell_domains = cell_domains && !cell_domains->empty();
687 bool use_interior_facet_domains
688 = interior_facet_domains && !interior_facet_domains->empty();
689 bool use_exterior_facet_domains
690 = exterior_facet_domains && !exterior_facet_domains->empty();
691
692 // Indicator whether or not tensor is required
693 std::array<bool, 2> tensor_required_cell = {{false, false}};
694 std::array<bool, 2> tensor_required_facet = {{false, false}};
695
696 // Track whether or not cell contribution has been computed
697 std::array<bool, 2> compute_cell_tensor = {{true, true}};
698 std::vector<bool> cell_tensor_computed(mesh.num_cells(), false);
699
700 // Iterate over facets
701 std::array<ufc::cell, 2> ufc_cell;
702 std::array<std::vector<double>, 2> coordinate_dofs;
703 Progress p("Assembling system (facet-wise)", mesh.num_facets());
704 for (FacetIterator facet(mesh); !facet.end(); ++facet)
705 {
706 // Number of cells sharing facet
707 const std::size_t num_cells = facet->num_entities(D);
708
709 // Check that facet is not a ghost
710 dolfin_assert(!facet->is_ghost());
711
712 // Interior facet
713 if (num_cells == 2)
714 {
715 // Get cells incident with facet (which is 0 and 1 here is arbitrary)
716 dolfin_assert(facet->num_entities(D) == 2);
717 std::array<std::size_t, 2> cell_indices = {{facet->entities(D)[0],
718 facet->entities(D)[1]}};
719
720 // Make sure cell marker for '+' side is larger than cell marker
721 // for '-' side. Note: by ffc convention, 0 is + and 1 is -
722 if (use_cell_domains && (*cell_domains)[cell_indices[0]]
723 < (*cell_domains)[cell_indices[1]])
724 {
725 std::swap(cell_indices[0], cell_indices[1]);
726 }
727
728 // Get cells incident with facet and associated data
729 for (std::size_t c = 0; c < 2; ++c)
730 {
731 cell[c] = Cell(mesh, cell_indices[c]);
732 cell_index[c] = cell[c].index();
733 local_facet[c] = cell[c].index(*facet);
734 cell[c].get_coordinate_dofs(coordinate_dofs[c]);
735 cell[c].get_cell_data(ufc_cell[c], local_facet[c]);
736
737 compute_cell_tensor[c] = !cell_tensor_computed[cell_index[c]];
738 }
739
740 const bool process_facet = (cell[0].is_ghost() != cell[1].is_ghost());
741 bool facet_owner = true;
742 if (process_facet)
743 {
744 int ghost_rank = -1;
745 if (cell[0].is_ghost())
746 ghost_rank = cell[0].owner();
747 else
748 ghost_rank = cell[1].owner();
749 dolfin_assert(my_mpi_rank != ghost_rank);
750 dolfin_assert(ghost_rank != -1);
751 if (ghost_rank < my_mpi_rank)
752 facet_owner = false;
753 }
754
755 // Loop over lhs and then rhs contributions
756 for (std::size_t form = 0; form < 2; ++form)
757 {
758 // Don't need to assemble rhs if only system matrix is required
759 if (form == 1 && !tensors[form])
760 continue;
761
762 // Get rank (lhs=2, rhs=1)
763 const std::size_t rank = (form == 0) ? 2 : 1;
764
765 // Compute number of dofs in macro dofmap
766 std::fill(num_dofs.begin(), num_dofs.begin() + rank, 0);
767 for (std::size_t c = 0; c < 2; ++c)
768 {
769 for (std::size_t dim = 0; dim < rank; ++dim)
770 {
771 auto dmap = dofmaps[form][dim]->cell_dofs(cell_index[c]);
772 cell_dofs[form][c][dim].set(dmap.size(), dmap.data());
773 num_dofs[dim] += cell_dofs[form][c][dim].size();
774 }
775
776 // Resize macro dof holder
777 for (std::size_t dim = 0; dim < rank; ++dim)
778 macro_dofs[form][dim].resize(num_dofs[dim]);
779
780 // Tabulate dofs on macro element
781 const std::size_t rank = (form == 0) ? 2 : 1;
782 for (std::size_t dim = 0; dim < rank; ++dim)
783 {
784 std::copy(cell_dofs[form][c][dim].begin(),
785 cell_dofs[form][c][dim].end(),
786 macro_dofs[form][dim].begin()
787 + c*cell_dofs[form][0][dim].size());
788 }
789 }
790
791 // Get facet integral for sub domain (if any)
792 if (use_interior_facet_domains)
793 {
794 const std::size_t domain = (*interior_facet_domains)[*facet];
795 interior_facet_integrals[form]
796 = ufc[form]->get_interior_facet_integral(domain);
797 }
798
799 // Check if facet tensor is required
800 if (rank == 2)
801 {
802 for (std::size_t c = 0; c < 2; ++c)
803 {
804 tensor_required_facet[form]
805 = cell_matrix_required(tensors[form],
806 interior_facet_integrals[form],
807 boundary_values,
808 cell_dofs[form][c][1]);
809 if (tensor_required_facet[form])
810 break;
811 }
812 }
813 else
814 {
815 tensor_required_facet[form]
816 = (tensors[form] && interior_facet_integrals[form]);
817 }
818
819 // Get cell integrals (if required)
820 for (std::size_t c = 0; c < 2; ++c)
821 {
822 if (compute_cell_tensor[c])
823 {
824 // Get cell integrals for sub domain (if any)
825 if (use_cell_domains)
826 {
827 const std::size_t domain = (*cell_domains)[cell[c]];
828 cell_integrals[form] = ufc[form]->get_cell_integral(domain);
829 }
830
831 // Check if facet tensor is required
832 if (form == 0)
833 {
834 tensor_required_cell[form]
835 = cell_matrix_required(tensors[form],
836 cell_integrals[form],
837 boundary_values,
838 cell_dofs[form][c][1]);
839 }
840 else
841 tensor_required_cell[form] = tensors[form] && cell_integrals[form];
842 }
843 }
844
845 // Reset work array
846 std::fill(ufc[form]->macro_A.begin(), ufc[form]->macro_A.end(), 0.0);
847 }
848
849 // Compute cell/facet tensor for lhs and rhs
850 std::array<std::size_t, 2> matrix_size;
851 std::size_t vector_size = 0;
852 for (std::size_t c = 0; c < 2; ++c)
853 {
854 matrix_size[0] = cell_dofs[0][c][0].size();
855 matrix_size[1] = cell_dofs[0][c][1].size();
856 vector_size = cell_dofs[1][c][0].size();
857 }
858 compute_interior_facet_tensor(ufc, ufc_cell,
859 coordinate_dofs,
860 tensor_required_cell,
861 tensor_required_facet,
862 cell, local_facet,
863 facet_owner,
864 cell_integrals,
865 interior_facet_integrals,
866 matrix_size,
867 vector_size,
868 compute_cell_tensor);
869
870 // Modify local tensors for bcs
871 ArrayView<const la_index> mdofs0(macro_dofs[0][0]);
872 ArrayView<const la_index> mdofs1(macro_dofs[0][1]);
873 apply_bc(ufc[0]->macro_A.data(), ufc[1]->macro_A.data(), boundary_values,
874 mdofs0, mdofs1);
875
876 // Add entries to global tensor
877 if (tensors[1])
878 {
879 std::vector<ArrayView<const la_index>> mdofs(macro_dofs[1].size());
880 for (std::size_t i = 0; i < macro_dofs[1].size(); ++i)
881 mdofs[i].set(macro_dofs[1][i]);
882 tensors[1]->add_local(ufc[1]->macro_A.data(), mdofs);
883 }
884
885 const bool add_macro_element
886 = ufc[0]->form.has_interior_facet_integrals();
887 if (tensors[0] && add_macro_element)
888 {
889 std::vector<ArrayView<const la_index>> mdofs(macro_dofs[0].size());
890 for (std::size_t i = 0; i < macro_dofs[0].size(); ++i)
891 mdofs[i].set(macro_dofs[0][i]);
892 tensors[0]->add_local(ufc[0]->macro_A.data(), mdofs);
893 }
894 else if (tensors[0] && !add_macro_element && tensor_required_cell[0])
895 {
896 // FIXME: This can be simplied by assembling into Ae instead
897 // of macro_A.
898
899 // The sparsity pattern may not support the macro element so
900 // instead extract back out the diagonal cell blocks and add
901 // them individually
902 matrix_block_add(*tensors[0], data.Ae[0], ufc[0]->macro_A,
903 compute_cell_tensor, cell_dofs[0]);
904 }
905
906 // Mark cells as processed
907 cell_tensor_computed[cell_index[0]] = true;
908 cell_tensor_computed[cell_index[1]] = true;
909 }
910 else // Exterior facet
911 {
912 // Get mesh cell to which mesh facet belongs (pick first, there
913 // is only one)
914 Cell cell(mesh, facet->entities(mesh.topology().dim())[0]);
915
916 // Check of attached cell needs to be processed
917 compute_cell_tensor[0] = !cell_tensor_computed[cell.index()];
918
919 // Decide if tensor needs to be computed
920 for (std::size_t form = 0; form < 2; ++form)
921 {
922 // Get rank (lhs=2, rhs=1)
923 const std::size_t rank = (form == 0) ? 2 : 1;
924
925 // Get cell integrals for sub domain (if any)
926 if (use_cell_domains)
927 {
928 const std::size_t domain = (*cell_domains)[cell];
929 cell_integrals[form] = ufc[form]->get_cell_integral(domain);
930 }
931
932 // Get exterior facet integrals for sub domain (if any)
933 if (use_exterior_facet_domains)
934 {
935 const std::size_t domain = (*exterior_facet_domains)[*facet];
936 exterior_facet_integrals[form]
937 = ufc[form]->get_exterior_facet_integral(domain);
938 }
939
940 // Get local-to-global dof maps for cell
941 for (std::size_t dim = 0; dim < rank; ++dim)
942 {
943 auto dmap = dofmaps[form][dim]->cell_dofs(cell.index());
944 cell_dofs[form][0][dim].set(dmap.size(), dmap.data());
945 }
946
947 // Store if tensor is required
948 if (rank == 2)
949 {
950 tensor_required_facet[form]
951 = cell_matrix_required(tensors[form],
952 exterior_facet_integrals[form],
953 boundary_values,
954 cell_dofs[form][0][1]);
955 tensor_required_cell[form]
956 = cell_matrix_required(tensors[form],
957 cell_integrals[form],
958 boundary_values,
959 cell_dofs[form][0][1]);
960 }
961 else
962 {
963 tensor_required_facet[form]
964 = (tensors[form] && exterior_facet_integrals[form]);
965 tensor_required_cell[form]
966 = tensors[form] && cell_integrals[form];
967 }
968 }
969
970 // Compute cell/facet tensors
971 compute_exterior_facet_tensor(data.Ae, ufc, ufc_cell[0],
972 coordinate_dofs[0],
973 tensor_required_cell,
974 tensor_required_facet,
975 cell, *facet,
976 cell_integrals,
977 exterior_facet_integrals,
978 compute_cell_tensor[0]);
979
980 // Modify local matrix/element for Dirichlet boundary conditions
981 apply_bc(data.Ae[0].data(), data.Ae[1].data(), boundary_values,
982 cell_dofs[0][0][0], cell_dofs[0][0][1]);
983
984 // Add entries to global tensor
985 for (std::size_t form = 0; form < 2; ++form)
986 {
987 if (tensors[form])
988 tensors[form]->add_local(data.Ae[form].data(), cell_dofs[form][0]);
989 }
990
991 // Mark cell as processed
992 cell_tensor_computed[cell.index()] = true;
993 }
994 p++;
995 }
996 }
997 //-----------------------------------------------------------------------------
998 void SystemAssembler::compute_exterior_facet_tensor(
999 std::array<std::vector<double>, 2>& Ae,
1000 std::array<UFC*, 2>& ufc,
1001 ufc::cell& ufc_cell,
1002 std::vector<double>& coordinate_dofs,
1003 const std::array<bool, 2>& tensor_required_cell,
1004 const std::array<bool, 2>& tensor_required_facet,
1005 const Cell& cell,
1006 const Facet& facet,
1007 const std::array<const ufc::cell_integral*, 2>& cell_integrals,
1008 const std::array<const ufc::exterior_facet_integral*, 2>& exterior_facet_integrals,
1009 const bool compute_cell_tensor)
1010 {
1011 // Get local index of facet with respect to the cell
1012 const std::size_t local_facet = cell.index(facet);
1013
1014 // Get cell data
1015 cell.get_coordinate_dofs(coordinate_dofs);
1016 cell.get_cell_data(ufc_cell, local_facet);
1017
1018 // Loop over lhs and then rhs facet contributions
1019 for (std::size_t form = 0; form < 2; ++form)
1020 {
1021 // Initialize macro element matrix/vector to zero
1022 std::fill(Ae[form].begin(), Ae[form].end(), 0.0);
1023 std::fill(ufc[form]->A.begin(), ufc[form]->A.end(), 0.0);
1024
1025 // Compute facet integral,if required
1026 if (tensor_required_facet[form])
1027 {
1028 // Update UFC object
1029 ufc[form]->update(cell, coordinate_dofs, ufc_cell,
1030 exterior_facet_integrals[form]->enabled_coefficients());
1031 exterior_facet_integrals[form]->tabulate_tensor(ufc[form]->A.data(),
1032 ufc[form]->w(),
1033 coordinate_dofs.data(),
1034 local_facet,
1035 ufc_cell.orientation);
1036 for (std::size_t i = 0; i < Ae[form].size(); i++)
1037 Ae[form][i] += ufc[form]->A[i];
1038 }
1039
1040 // Assemble cell integral (if required)
1041 if (compute_cell_tensor)
1042 {
1043 dolfin_assert(!cell.is_ghost());
1044
1045 // Compute cell integral, if required
1046 if (tensor_required_cell[form])
1047 {
1048 ufc[form]->update(cell, coordinate_dofs, ufc_cell,
1049 cell_integrals[form]->enabled_coefficients());
1050 cell_integrals[form]->tabulate_tensor(ufc[form]->A.data(),
1051 ufc[form]->w(),
1052 coordinate_dofs.data(),
1053 ufc_cell.orientation);
1054 for (std::size_t i = 0; i < Ae[form].size(); i++)
1055 Ae[form][i] += ufc[form]->A[i];
1056 }
1057 }
1058 }
1059 }
1060 //-----------------------------------------------------------------------------
1061 void SystemAssembler::compute_interior_facet_tensor(
1062 std::array<UFC*, 2>& ufc,
1063 std::array<ufc::cell, 2>& ufc_cell,
1064 std::array<std::vector<double>, 2>& coordinate_dofs,
1065 const std::array<bool, 2>& tensor_required_cell,
1066 const std::array<bool, 2>& tensor_required_facet,
1067 const std::array<Cell, 2>& cell,
1068 const std::array<std::size_t, 2>& local_facet,
1069 const bool facet_owner,
1070 const std::array<const ufc::cell_integral*, 2>& cell_integrals,
1071 const std::array<const ufc::interior_facet_integral*, 2>& interior_facet_integrals,
1072 const std::array<std::size_t, 2>& matrix_size,
1073 const std::size_t vector_size,
1074 const std::array<bool, 2> compute_cell_tensor)
1075 {
1076 // Compute facet contribution to tensor, if required
1077 // Loop over lhs and then rhs facet contributions
1078 for (std::size_t form = 0; form < 2; ++form)
1079 {
1080 // Compute interior facet integral
1081 if (tensor_required_facet[form] && facet_owner)
1082 {
1083 // Update to current pair of cells
1084 ufc[form]->update(cell[0], coordinate_dofs[0], ufc_cell[0],
1085 cell[1], coordinate_dofs[1], ufc_cell[1],
1086 interior_facet_integrals[form]->enabled_coefficients());
1087 // Integrate over facet
1088 interior_facet_integrals[form]->tabulate_tensor(ufc[form]->macro_A.data(),
1089 ufc[form]->macro_w(),
1090 coordinate_dofs[0].data(),
1091 coordinate_dofs[1].data(),
1092 local_facet[0],
1093 local_facet[1],
1094 ufc_cell[0].orientation,
1095 ufc_cell[1].orientation);
1096 }
1097
1098 // Compute cell contribution
1099 for (std::size_t c = 0; c < 2; ++c)
1100 {
1101 if (compute_cell_tensor[c])
1102 {
1103 // Compute cell tensor, if required
1104 if (tensor_required_cell[form] and !cell[c].is_ghost())
1105 {
1106 ufc[form]->update(cell[c], coordinate_dofs[c], ufc_cell[c],
1107 cell_integrals[form]->enabled_coefficients());
1108 cell_integrals[form]->tabulate_tensor(ufc[form]->A.data(),
1109 ufc[form]->w(),
1110 coordinate_dofs[c].data(),
1111 ufc_cell[c].orientation);
1112
1113 // FIXME: Can the below two blocks be consolidated?
1114 const std::size_t nn = matrix_size[0];
1115 if (form == 0)
1116 {
1117 const std::size_t mm = matrix_size[1];
1118 for (std::size_t i = 0; i < mm; i++)
1119 {
1120 for (std::size_t j = 0; j < nn; j++)
1121 {
1122 ufc[form]->macro_A[2*nn*mm*c + 2*i*nn + nn*c + j]
1123 += ufc[form]->A[i*nn + j];
1124 }
1125 }
1126 }
1127 else
1128 {
1129 for (std::size_t i = 0; i < vector_size; i++)
1130 ufc[form]->macro_A[nn*c + i] += ufc[form]->A[i];
1131 }
1132 }
1133 }
1134 }
1135 }
1136 }
1137 //-----------------------------------------------------------------------------
1138 void SystemAssembler::matrix_block_add(
1139 GenericTensor& tensor,
1140 std::vector<double>& Ae,
1141 std::vector<double>& macro_A,
1142 const std::array<bool, 2>& add_local_tensor,
1143 const std::array<std::vector<ArrayView<const la_index>>, 2>& cell_dofs)
1144 {
1145 for (std::size_t c = 0; c < 2; ++c)
1146 {
1147 // Add cell tensor, if not already processed
1148 if (add_local_tensor[c])
1149 {
1150 std::fill(Ae.begin(), Ae.end(), 0.0);
1151 const std::size_t nn = cell_dofs[c][0].size();
1152 const std::size_t mm = cell_dofs[c][1].size();
1153 for (std::size_t i = 0; i < mm; i++)
1154 {
1155 for (std::size_t j = 0; j < nn; j++)
1156 Ae[i*nn + j] = macro_A[2*nn*mm*c + 2*i*nn + nn*c +j];
1157 }
1158 tensor.add_local(Ae.data(), cell_dofs[c]);
1159 }
1160 }
1161 }
1162 //-----------------------------------------------------------------------------
1163 void
1164 SystemAssembler::apply_bc(double* A, double* b,
1165 const std::vector<DirichletBC::Map>& boundary_values,
1166 const ArrayView<const dolfin::la_index>& global_dofs0,
1167 const ArrayView<const dolfin::la_index>& global_dofs1)
1168 {
1169 dolfin_assert(A);
1170 dolfin_assert(b);
1171
1172 // Wrap matrix and vector using Eigen
1173 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic,
1174 Eigen::RowMajor>>
1175 _matA(A, global_dofs0.size(), global_dofs1.size());
1176 Eigen::Map<Eigen::VectorXd> _b(b, global_dofs0.size());
1177
1178 if (boundary_values.size() == 1)
1179 {
1180 // Square matrix with same FunctionSpace on each axis
1181 // Loop over columns/rows
1182 for (int i = 0; i < _matA.cols(); ++i)
1183 {
1184 const std::size_t ii = global_dofs1[i];
1185 DirichletBC::Map::const_iterator bc_value = boundary_values[0].find(ii);
1186 if (bc_value != boundary_values[0].end())
1187 {
1188 // Zero row
1189 _matA.row(i).setZero();
1190
1191 // Modify RHS (subtract (bc_column(A))*bc_val from b)
1192 _b -= _matA.col(i)*bc_value->second;
1193
1194 // Zero column
1195 _matA.col(i).setZero();
1196
1197 // Place 1 on diagonal and bc on RHS (i th row ).
1198 _b(i) = bc_value->second;
1199 _matA(i, i) = 1.0;
1200 }
1201 }
1202 }
1203 else
1204 {
1205 // Possibly rectangular matrix with different spaces on axes
1206 // FIXME: This won't work for forms on W x W.sub(0), which would contain
1207 // diagonal. This is difficult to distinguish from form
1208 // on V x Q which would by accident contain V bc and Q bc with
1209 // same dof index, but that's not diagonal.
1210 //
1211 // Essentially we need to detect if _a->function_space(0) and
1212 // _a->function_space(1) share the common super space (use
1213 // FunctionSpace::_root_space_id). In that case dof ids are shared
1214 // and matrix has diagonal. Otherwise it does not have diagonal.
1215
1216 // Loop over rows first
1217 for (int i = 0; i < _matA.rows(); ++i)
1218 {
1219 const std::size_t ii = global_dofs0[i];
1220 DirichletBC::Map::const_iterator bc_value = boundary_values[0].find(ii);
1221 if (bc_value != boundary_values[0].end())
1222 _matA.row(i).setZero();
1223 }
1224
1225 // Loop over columns
1226 for (int j = 0; j < _matA.cols(); ++j)
1227 {
1228 const std::size_t jj = global_dofs1[j];
1229 DirichletBC::Map::const_iterator bc_value = boundary_values[1].find(jj);
1230 if (bc_value != boundary_values[1].end())
1231 {
1232 // Modify RHS (subtract (bc_column(A))*bc_val from b)
1233 _b -= _matA.col(j)*bc_value->second;
1234 _matA.col(j).setZero();
1235 }
1236 }
1237 }
1238
1239 }
1240 //-----------------------------------------------------------------------------
1241 bool SystemAssembler::has_bc(const DirichletBC::Map& boundary_values,
1242 const ArrayView<const dolfin::la_index>& dofs)
1243 {
1244 // Loop over dofs and check if bc is applied
1245 for (auto dof = dofs.begin(); dof != dofs.end(); ++dof)
1246 {
1247 DirichletBC::Map::const_iterator bc_value = boundary_values.find(*dof);
1248 if (bc_value != boundary_values.end())
1249 return true;
1250 }
1251
1252 return false;
1253 }
1254 //-----------------------------------------------------------------------------
1255 bool SystemAssembler::cell_matrix_required(
1256 const GenericTensor* A,
1257 const void* integral,
1258 const std::vector<DirichletBC::Map>& boundary_values,
1259 const ArrayView<const dolfin::la_index>& dofs)
1260 {
1261 if (A && integral)
1262 return true;
1263 else if (integral && has_bc(boundary_values[0], dofs))
1264 return true;
1265 else
1266 return false;
1267 }
1268 //-----------------------------------------------------------------------------
1269 SystemAssembler::Scratch::Scratch(const Form& a, const Form& L)
1270 {
1271 std::size_t A_num_entries
1272 = a.function_space(0)->dofmap()->max_element_dofs();
1273 A_num_entries *= a.function_space(1)->dofmap()->max_element_dofs();
1274 Ae[0].resize(A_num_entries);
1275 Ae[1].resize(L.function_space(0)->dofmap()->max_element_dofs());
1276 }
1277 //-----------------------------------------------------------------------------
1278 SystemAssembler::Scratch::~Scratch()
1279 {
1280 // Do nothing
1281 }
1282 //-----------------------------------------------------------------------------