a new mesh library for dolfin - fenics projectintroduction tutorial algorithms benchmarks mesh...

26
A new mesh library for DOLFIN Anders Logg [email protected] Simula Research Laboratory FEniCS’06 in Delft, November 8-9 2006

Upload: others

Post on 28-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

A new mesh library for DOLFIN

Anders [email protected]

Simula Research Laboratory

FEniCS’06 in Delft, November 8-9 2006

Page 2: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Outline

Introduction

Tutorial

Algorithms

Benchmarks

Anders Logg [email protected] A new mesh library for DOLFIN

Page 3: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

DOLFIN needs a new mesh library

◮ Old DOLFIN mesh implemented in 2002–2003

◮ Local data stored in classes Vertex, Cell, etc.

◮ Dimension dependent interface:

for (EdgeIterator e(mesh); !e.end(); ++e)

...

for (FaceIterator f(mesh); !f.end(); ++f)

...

◮ Specialized to simplicial meshes: triangles or tetrahedra

Anders Logg [email protected] A new mesh library for DOLFIN

Page 4: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Design goals for the new mesh library

◮ Simple◮ No fancy data structures◮ Only unsigned int* and double*

◮ Intuitive◮ Choose suitable abstractions◮ Simple transition from old DOLFIN mesh

◮ Generic◮ Not specialized to simplicial meshes◮ Dimension-independent interface

◮ Fast◮ Minimize object-oriented overhead

Anders Logg [email protected] A new mesh library for DOLFIN

Page 5: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Mesh abstractions

◮ Mesh = (Topology, Geometry)

◮ Topology = ({Mesh entities}, Connectivity)

◮ Mesh entity = (d, i)

◮ Connectivity = {Incidence relations d − d′}

Anders Logg [email protected] A new mesh library for DOLFIN

Page 6: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Mesh entities

Anders Logg [email protected] A new mesh library for DOLFIN

Page 7: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Named mesh entities

Entity Dimension Codimension

Vertex 0 D

Edge 1 D − 1

Face 2 D − 2

Facet D − 1 1

Cell D 0

◮ Mesh entity defined by (d, i)

◮ Named mesh entities: Vertex, Edge, Face, Facet, Cell

Anders Logg [email protected] A new mesh library for DOLFIN

Page 8: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Implementation

◮ Implemented in C++, about 4000 lines (including comments)

◮ Ships with DOLFIN 0.6.3

◮ Mesh

◮ MeshTopology, MeshGeometry

◮ MeshEntity, MeshEntityIterator

◮ Vertex, Edge, Face, Facet, Cell

◮ MeshFunction

◮ MeshEditor

◮ UnitSquare, UnitCube

Anders Logg [email protected] A new mesh library for DOLFIN

Page 9: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Input/output

Mesh mesh(’’mesh.xml’’);

File file(’’mesh.xml’’);

Mesh mesh;

file >> mesh;

File file(’’mesh.xml’’);

Mesh mesh;

file << mesh;

Anders Logg [email protected] A new mesh library for DOLFIN

Page 10: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Conversion to the new DOLFIN XML format

◮ Use dolfin-convert to convert to the DOLFIN XML format

◮ Conversion from Medit (tetgen) and Gmsh

◮ Conversion from old DOLFIN XML:

dolfin-convert --input old-xml old.xml new.xml

◮ Convert all meshes in current directory:

convertall

Anders Logg [email protected] A new mesh library for DOLFIN

Page 11: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Built-in meshes

◮ Simple built-in meshes: UnitSquare, UnitCube

◮ Contributions are welcome: UnitDisc?, UnitSphere?

UnitSquare mesh(16, 16);

UnitCube mesh(256, 256, 256);

Anders Logg [email protected] A new mesh library for DOLFIN

Page 12: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Building meshes

◮ Use class MeshEditor◮ Specialized to simplicial meshes in 1D, 2D, 3D

Mesh mesh;

MeshEditor editor(mesh, CellType::triangle, 2, 2);

editor.initVertices(4);

editor.initCells(2);

editor.addVertex(0, 0.0, 0.0);

editor.addVertex(1, 1.0, 0.0);

editor.addVertex(2, 1.0, 1.0);

editor.addVertex(3, 0.0, 1.0);

editor.addCell(0, 0, 1, 2);

editor.addCell(1, 0, 2, 3);

editor.close();

Anders Logg [email protected] A new mesh library for DOLFIN

Page 13: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Mesh iterators

Basic iteration:

unsigned int D = mesh.topology().dim();

for (MeshEntityIterator c(mesh, D); !c.end(); ++c)

for (MeshEntityIterator v(c, 0); !v.end(); ++v)

v->foo();

Iteration with named iterators:

for (CellIterator c(mesh); !c.end(); ++c)

for (VertexIterator v(c); !v.end(); ++v)

v->foo();

Anders Logg [email protected] A new mesh library for DOLFIN

Page 14: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Mesh functions

◮ A discrete function on a mesh◮ Implemented by the class MeshFunction◮ Different from the class Function◮ Takes a value on each mesh entity of given fixed dimension◮ Templated over value type:

◮ Material parameters (double)◮ Markers for mesh refinement (bool)◮ Inter-mesh connectivity (unsigned int)

MeshFunction<bool> marked_for_refinement;

for (CellIterator c(mesh); !c.end(); ++c)

{

if ( marked_for_refinement(*c) )

...

}

Anders Logg [email protected] A new mesh library for DOLFIN

Page 15: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Extracting boundaries

◮ A BoundaryMesh is a Mesh

◮ Simple boundary extraction:

Mesh mesh;

BoundaryMesh boundary(mesh);

◮ Mappings from the boundary to the mesh:

MeshFunction<unsigned int> vertices,

MeshFunction<unsigned int> cells;

BoundaryMesh boundary(mesh, vertices, cells);

Anders Logg [email protected] A new mesh library for DOLFIN

Page 16: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Mesh refinement

◮ Uniform mesh refinement implemented

◮ Adaptive mesh refinement / coarsening will be added again

Mesh mesh;

for (int i = 0; i < 3; ++i)

mesh.refine();

Anders Logg [email protected] A new mesh library for DOLFIN

Page 17: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Python interface

◮ Generated automatically by SWIG

◮ Python iterators implemented for mesh entities

◮ C++ arrays mapped to Numeric arrays (will be NumPy)

from dolfin import *

mesh = UnitSquare(16, 16)

mesh.refine()

for c in cells(mesh):

for v in vertices(c):

...

Anders Logg [email protected] A new mesh library for DOLFIN

Page 18: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Mesh connectivity

◮ Mesh of topological dimension D

◮ Connectivity D − 0 given (cells − vertices)

◮ Need to compute connectivity d − d′ for 0 ≤ d, d′ ≤ D

◮ Compute only as needed

0 1 2 D

0

1

2

D X

Anders Logg [email protected] A new mesh library for DOLFIN

Page 19: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Computing mesh connectivity

◮ Build D − d and d − 0 from D − 0 and D − D for 0 < d < D

◮ Compute d − d′ from d′ − d for d < d′ (transpose)

◮ Compute d − d′ from d − d′′ and d′′ − d′ (intersection)

◮ All algorithms are O(np)O(N) for small n and p

Anders Logg [email protected] A new mesh library for DOLFIN

Page 20: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Example: computing 2 − 2 (faces − faces)

for (FaceIterator f0(mesh); !f0.end(); ++f0)

for (FaceIterator f1(f0); !f1.end(); ++f1)

// Iterators automatically initialize 2 - 2

Anders Logg [email protected] A new mesh library for DOLFIN

Page 21: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Simple benchmarks

◮ Creating unit cube mesh

◮ Iteration over mesh entities

◮ Uniform mesh refinement

◮ Memory usage

◮ Speedup: a factor 10–100

◮ Reduced memory usage: a factor 10

Anders Logg [email protected] A new mesh library for DOLFIN

Page 22: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Creating unit cube mesh

1 0 0 1 0 1 1 0 2 1 0 3 1 0 4 1 0 5 1 0 6N u m b e r o f c e l l s1 0 � 41 0 � 31 0 � 21 0 � 11 0 01 0 1CPUti me/ s

C r e a t i n g u n i t c u b e m e s hO l d m e s hN e w m e s h

Anders Logg [email protected] A new mesh library for DOLFIN

Page 23: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Iteration over mesh entities

1 0 0 1 0 1 1 0 2 1 0 3 1 0 4 1 0 5 1 0 6N u m b e r o f c e l l s1 0 � 41 0 � 31 0 � 2

1 0 � 1CPUti me/ s

I t e r a t i o n o v e r m e s h e n t i t i e sO l d m e s hN e w m e s h

Anders Logg [email protected] A new mesh library for DOLFIN

Page 24: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Uniform mesh refinement

1 0 0 1 0 1 1 0 2 1 0 3 1 0 4 1 0 5 1 0 6N u m b e r o f c e l l s1 0 � 21 0 � 11 0 01 0 11 0 21 0 3CPUti me/ s

U n i f o r m m e s h r e f i n e m e n tO l d m e s hN e w m e s h

Anders Logg [email protected] A new mesh library for DOLFIN

Page 25: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Memory usage

0 . 0 0 . 5 1 . 0 1 . 5 2 . 0N u m b e r o f c e l l s x 1 e + 502 04 06 08 01 0 01 2 01 4 0M emorysi ze/MB

M e m o r y u s a g eO l d m e s hN e w m e s h

Anders Logg [email protected] A new mesh library for DOLFIN

Page 26: A new mesh library for DOLFIN - FEniCS ProjectIntroduction Tutorial Algorithms Benchmarks Mesh functions A discrete function on a mesh Implemented by the class MeshFunction Different

IntroductionTutorial

AlgorithmsBenchmarks

Future plans

◮ Adaptive mesh refinement / coarsening

◮ Extend functionality for ALE methods

◮ Extend functionality for parallel assembly

◮ Graphical mesh editor (Simula/Kalkulo)

Anders Logg [email protected] A new mesh library for DOLFIN