Introduction to the DUNE Software Library · 2013. 1. 16. · Code Example: Grid View Outcome using...

46
Dune: Crete 2013 Introduction to the DUNE Software Library Andreas Dedner, Heraklion, January 16, 2013 Department of Mathematics University of Warwick www.warwick.ac.uk/go/dune

Transcript of Introduction to the DUNE Software Library · 2013. 1. 16. · Code Example: Grid View Outcome using...

  • Dune: Crete 2013

    Introduction to the DUNE Software Library

    Andreas Dedner,

    Heraklion, January 16, 2013

    Department of Mathematics

    University of Warwick

    www.warwick.ac.uk/go/dune

  • Introduction to DUNE-FEM (Part 2)

    OutlineDune: Crete 2013

    1 Introduction to DUNE-FEM (Part 2)

    2 Measuring Efficiency

    3 DUNE-fem-howto

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 2 / 44

  • Introduction to DUNE-FEM (Part 2)

    Discontinuous Galerkin Method, Approach IDune: Crete 2013

    find piecewise polynomial approximation Uh of

    ∂t U(x , t) + ∇ · (F (U(x , t), x , t)− D(U(x , t), x , t)∇U(x , t)) = 0

    ∫K∂t uKϕ =

    ∫K

    (F (uK ) · ∇ϕ)−∫∂K

    F̂ (u) · nKϕ

    −∫

    K(D(uK )∇uK · ∇ϕ) +

    ∫∂K

    D̂1(u) · nKϕ+ D̂2(u) · ∇ϕ

    =: − < LAK [uh], ϕ > − < LDK [uh], ϕ >

    • hyperbolic operator LAK ≈ ∇ · F (u) on one element K , possibly with explicit time stepF̂ : suitable upwind flux for advection requiring only data on direct neighbors

    • elliptic operator: LDK ≈ −∇ · D(u)∇u on element K , possibly with implicit time stepD̂1, D̂2: suitable flux for diffusion requires only data on direct neighbors

    • use IMEX Runge-Kutta scheme

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 3 / 44

  • Introduction to DUNE-FEM (Part 2)

    Discontinuous Galerkin Method, Approach IDune: Crete 2013

    find piecewise polynomial approximation Uh of

    ∂t U(x , t) + ∇ · (F (U(x , t), x , t)− D(U(x , t), x , t)∇U(x , t)) = 0

    ∂t uh = −(LA[uh] + LD [uh]

    )

    LAK [uh] ≈ ∇ · F (U(x , t), x , t)

    LDK [uh] ≈ −∇ · D(U(x , t), x , t)∇U(x , t)

    • LA is an approximation for a first order hyperbolic equation (e.g., Euler equation)• LD is an approximation for an elliptic or parabolic equation (e.g., Laplace/Heat equation)• Suitable ODE for time integration

    DescriptionModel: function F and D

    Discrete Model: F̂ and D̂1, D̂2 (using F ,D)

    Problem: other functions, e.g., initial data

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 4 / 44

  • Introduction to DUNE-FEM (Part 2)

    Discontinuous Galerkin Method, Approach IIDune: Crete 2013

    find piecewise polynomial approximation Uh of

    ∂t U(x , t) + ∇ · (f (U(x , t), x , t)− D(U(x , t), x , t)∇d(U(x , t))) = 0

    Rewritte as first order system for (σ, u) and use LA:σ(x , t) +∇d(U(x , t)) = 0, ∂t U(x , t) +∇ · (f (U(x , t), x , t) + D(U(x , t), x , t)σ(x , t)) = 0.

    σh = −LA1 [uh], ∂t uh = −LA2 [uh, σh].

    or ∂t uh = LAD [uh] := LA2 [uh,LA1 [uh]].

    Use the same operator tow times with different flux F :

    LA1 [uh] ≈ ∇ · F with F (uh, x , t) = d(uh))

    LA2 [(uh, σh] ≈ ∇ · F with F ((uh, σh), x , t) = f (uh(x , t), x , t) + D(uh(x , t), x , t)σh

    DescriptionModel: function F ,D and d

    Discrete Model 1: F̂1 (using d)

    Discrete Model 2: F̂2 (using F ,D)

    Problem: other functions, e.g., initial data

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 5 / 44

  • Introduction to DUNE-FEM (Part 2)

    Approach I and II ComparedDune: Crete 2013

    DG Spatial Operators for

    L[u] = −∇ · (f (U(x , t), x , t)− D(U(x , t), x , t)∇d(U(x , t)))

    Approach I

    LAD [uh] := LD [uh] + LA[uh]

    Model: function F and D (d ≡ 1)

    Discrete Model: F̂ and D̂1, D̂2 (using F ,D)

    Problem: other functions, e.g., initial data

    Approach II

    LAD [uh] := LA2 [uh,LA1 [uh]]

    Model: function f ,D and d

    Discrete Model 1: F̂1 (using d)

    Discrete Model 2: F̂2 (using F ,D)

    Problem: other functions, e.g., initial dataAndreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 6 / 44

  • Introduction to DUNE-FEM (Part 2)

    Using Interface ClassesDune: Crete 2013

    Operators < DiscreteModel >: examples LA, LD , LADDiscreteModel < Model >: part of discretization which is not part of continuous problem(numerical fluxes)Model< grid dimension >: first part of continuous problemProblem< grid dimension >: second part of continuous problemOften only Problem needs to be implemented (e.g., use EulerModel to solve Euler equations)Otherwise often only Problem and Model needs to be implemented

    Difference between Problem and ModelDistinction is somewhat arbitrary, general idea

    Problem: use dynamic polymorphism to allow runtime selection

    Model: use static polymorphism for maximal efficiency

    ODESolver < Operator >: un → un+1 to solve ∂t u = L[u]InverseOperator < Operator >: u = L−1[f ] to solve L[u] = f

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 7 / 44

  • Introduction to DUNE-FEM (Part 2)

    Using Interface ClassesDune: Crete 2013

    Operators < DiscreteModel >: examples LA, LD , LADDiscreteModel < Model >: part of discretization which is not part of continuous problem(numerical fluxes)Model< grid dimension >: first part of continuous problemProblem< grid dimension >: second part of continuous problemOften only Problem needs to be implemented (e.g., use EulerModel to solve Euler equations)Otherwise often only Problem and Model needs to be implemented

    Difference between Problem and ModelDistinction is somewhat arbitrary, general idea

    Problem: use dynamic polymorphism to allow runtime selection

    Model: use static polymorphism for maximal efficiency

    ODESolver < Operator >: un → un+1 to solve ∂t u = L[u]InverseOperator < Operator >: u = L−1[f ] to solve L[u] = f

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 7 / 44

  • Introduction to DUNE-FEM (Part 2)

    Using Interface ClassesDune: Crete 2013

    Operators < DiscreteModel >: examples LA, LD , LADDiscreteModel < Model >: part of discretization which is not part of continuous problem(numerical fluxes)Model< grid dimension >: first part of continuous problemProblem< grid dimension >: second part of continuous problemOften only Problem needs to be implemented (e.g., use EulerModel to solve Euler equations)Otherwise often only Problem and Model needs to be implemented

    Difference between Problem and ModelDistinction is somewhat arbitrary, general idea

    Problem: use dynamic polymorphism to allow runtime selection

    Model: use static polymorphism for maximal efficiency

    ODESolver < Operator >: un → un+1 to solve ∂t u = L[u]InverseOperator < Operator >: u = L−1[f ] to solve L[u] = f

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 7 / 44

  • Introduction to DUNE-FEM (Part 2)

    Three Level ConceptDune: Crete 2013

    Three levels:High level: implement the problem class, i.e., PDE data

    Middle level; implement the (discrete) model class, i.e., numerical flux,automatic differentiation

    Lowest level: assembly routines, discrete spaces...

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 8 / 44

  • Introduction to DUNE-FEM (Part 2)

    Free surface hydrostatic flowDune: Crete 2013

    Time dependent domain

    Ω(t) ={

    (x , z)T ∈ Rd : x ∈ Ωx , b(x) < z < b(x) + h(x , t)}

    With shallow water scaling the free surface h(t , ·) : Ωx → R and the 3dvelocity field u = (ux ,w)T : Ω(t)→ Rd satisfy

    ∂th +∇x ·(∫ b+h

    b uxdz)

    = 0 in Ωx ,∂thux +∇ · (hu⊗ ux ) + g∇xh2 = −gh∇xb + visc. in Ω(t),

    ∂zw = −∇x · ux in Ω(t),

    Discretization (in space):1 compute the integrals of the horizontal velocities

    ∫ b+hb uxdz

    (special grid)2 compute the vertical velocity w (integration in z on special grid)3 apply advection-diffusion discretization for (h,ux ) (LAD)

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 9 / 44

  • Introduction to DUNE-FEM (Part 2)

    Simulation ResultsDune: Crete 2013

    early time later time

    parallel prisma grid

    diploma thesis C. Gersbacher

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 10 / 44

    ./movies/swe3d_viewB.avi

  • Introduction to DUNE-FEM (Part 2)

    Code Example: Grid ViewDune: Crete 2013

    // type of hierarchical gridtypedef Dune::GridSelector::GridType GridType;Dune::GridPtr< GridType > gridPtr( gridfile );GridType &grid = *gridPtr;grid.loadBalance();Dune::Fem::GlobalRefine::apply( grid, level * refineStepsForHalf );

    // create host grid part consisting of leaf level elementstypedef Dune::Fem::LeafGridPart< GridType > HostGridPartType;HostGridPartType hostGridPart( grid );

    // create filtertypedef School::RadialFilter< HostGridPartType > FilterType;typename FilterType::GlobalCoordinateType center( 0.5 );typename FilterType::ctype radius( .25 );FilterType filter( hostGridPart, center, radius );

    // create filtered grid part, only a subset of the host grid part’sentities are contained

    typedef Dune::Fem::FilteredGridPart< HostGridPartType, FilterType >GridPartType;

    GridPartType gridPart( hostGridPart, filter );

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 11 / 44

  • Introduction to DUNE-FEM (Part 2)

    Code Example: Grid ViewDune: Crete 2013

    Outcome using YASPGrid (Cartesian)

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 12 / 44

  • Introduction to DUNE-FEM (Part 2)

    Code Example: Discrete FunctionsDune: Crete 2013

    // function spacetypedef Dune::Fem::FunctionSpace< double, double,

    GridType::dimensionworld, range > FunctionSpaceType;

    // construct an instance of the problemtypedef School::QuadraticFunction< FunctionSpaceType > FunctionType;FunctionType function;

    // convert the continuously given problem into a grid functiontypedef Dune::Fem::GridFunctionAdapter< FunctionType, GridPartType >

    GridExactSolutionType;GridExactSolutionType exact( "exact solution", function, gridPart, 5

    );

    //! choose type of discrete function spacetypedef Dune::Fem::LagrangeDiscreteFunctionSpace< FunctionSpaceType,

    GridPartType, POLORDER > DiscreteFunctionSpaceType;typedef Dune::Fem::AdaptiveDiscreteFunction<

    DiscreteFunctionSpaceType > DiscreteFunctionType;

    DiscreteFunctionSpace discreteSpace_( gridPart );DiscreteFunction rhs( "rhs functional", discreteSpace_ );DiscreteFunction solution( "solution", discreteSpace_ );

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 13 / 44

  • Introduction to DUNE-FEM (Part 2)

    Code Example: L2-ProjectionDune: Crete 2013

    // choose type of discrete function, Matrix implementation andsolver implementation

    typedef Dune::Fem::SparseRowMatrixOperator< DiscreteFunctionType,DiscreteFunctionType, MatrixTraits > LinearOperatorType;

    typedef Dune::Fem::CGInverseOperator< DiscreteFunctionType >LinearInverseOperatorType;

    assembleRHS ( function, rhs );

    // setup mass operatorLinearOperatorType linearOperator( "assempled mass operator",

    discreteSpace_, discreteSpace_ );assembleMass( linearOperator );// inverse operator using linear operatorLinearInverseOperatorType invOperator( linearOperator, 1e-8, 1e-8 );// solve systemsolution.clear();invOperator( rhs, solution );

    // setup tuple of functions to output (discrete solution, exactsolution, and difference)

    typedef Dune::tuple< const DiscreteFunctionType *,GridExactSolutionType *, ErrorFunctionType * > IOTupleType;

    IOTupleType ioTuple( &solution, &exact, &errorFunction );

    // write datatypedef Dune::Fem::DataOutput< GridType, IOTupleType >

    DataOutputType;DataOutputType dataOutput( grid, ioTuple, MyDataOutputParameters(

    step ) );dataOutput.write();

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 14 / 44

  • Introduction to DUNE-FEM (Part 2)

    Code Example: Grid ViewDune: Crete 2013

    Outcome using YASPGrid (Cartesian)

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 15 / 44

  • Introduction to DUNE-FEM (Part 2)

    Example: Assembling a FunctionalDune: Crete 2013

    F ϕ =∫G

    f ϕ ∀ϕ ∈ XG

    F.clear();for(Iterator it = space.begin(); it != space.end(); ++it){

    const Entity &entity = *it;const Geometry &geo = entity.geometry();LocalFunction F_loc = F.localFunction(entity);Quadrature quadrature(entity, 2*space.order()+1);for(size_t pt = 0; pt < quadrature.nop(); ++pt){const DomainType &x = quadrature.point(pt);RangeType f_x;f.evaluate(geo.global(x), f_x);f_x *= quadrature.weight(pt) * geo.integrationElement(x);

    // F Ei + = + = f (xEi ) · ϕ

    Ei (x

    Ei ), i = 1, . . . ,N

    K

    F_loc.axpy(quadrature[pt], f_x);}

    }F.communicate();

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 16 / 44

  • Measuring Efficiency

    OutlineDune: Crete 2013

    1 Introduction to DUNE-FEM (Part 2)

    2 Measuring Efficiency

    3 DUNE-fem-howto

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 17 / 44

  • Measuring Efficiency

    Strong scaling on Blue Gene/PDune: Crete 2013

    Strong scaling and efficiency on the supercomputer JUGENE(Jülich, Germany)

    #cores #cells/corea #DOFs/core time (ms)b speed-up efficiency

    512 474 296250 46216 — —4096 59 36875 6294 7.34 0.91

    32768 7 4375 949 48.71 0.7665536 3 1875 504 91.70 0.72

    aaverage #cells/corebaverage run time per time step

    • Navier-Stokes equations in 3D (k = 4)• overall number of cells 243 000 (#DOFS ≈ 1.52 · 108) on Cartesian grid• explicit Runge-Kutta method of order 3• programming techniques to increase performance:

    • template meta programming• automated code generation of DG kernels• hybrid parallelization (MPI / pthreads , work to be done)• overlap computation and communication

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 18 / 44

  • Measuring Efficiency

    Weak scaling on Blue Gene/PDune: Crete 2013

    weak parallel scaling#cpus #cells speedup efficiency

    8 30t 1.0 1.0064 243t 7.9 0.99

    256 972t 31.0 0.981024 3 950t 123.6 0.964096 15 552t 498.2 0.97

    16384 63 701t 2136.3 1.00

    • Navier-Stokes equations in 3D (k = 4)• overall number of cells 243 000• explicit Runge-Kutta method of order 3• programming techniques for performance

    • template meta programming• automated code generation of DG kernels• hybrid parallelization (MPI / pthreads , work in progress)• overlap computation and communication

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 19 / 44

  • Measuring Efficiency

    Efficiency of DG implementation in DUNE-FEMDune: Crete 2013

    • Intel(R) Core(TM) i7 CPU QM 720 @ 1.60GHz (Nehalem),theoretical peak performance 25.6 GFLOP/s

    • best performance measurement: Intel(R) Optimized LINPACKBenchmark

    • performance measurement tool: likwid-2.2.1

    multi core (4)

    1 LINPACK 24.6 GFLOP/s

    2 LINPACK(likwid) 19.5 GFLOP/s

    k # DOF/cell GFLOP/s w.r.t. (1) w.r.t. (2) w.r.t. peak.1 40 3.59 14.6 % 18.4 % 13.9 %2 135 4.80 19.5 % 24.6 % 18.8 %3 320 4.95 20.1 % 25.4 % 19.3 %4 625 4.26 17.3 % 21.8 % 16.6 %

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 20 / 44

  • Measuring Efficiency

    Strong scaling on Blue Gene/P (implicit)Dune: Crete 2013

    C =#cells (without ghosts), N = #DOFS, η̄ = average run time pertimestep

    strong scaling with implicit time discretization

    P C/P N/P η̄ S512 E512

    512 474.6 296 630.8 367 483 — —4 096 59.3 37 324.9 51 892 7.08 0.89

    32 768 7.4 4 634.8 7 666 47.94 0.75

    • Density current (compressible Navier-Stokes) solved with CDG2(k = 4 in 3D =⇒ 625 DOFs per cell)

    • overall number of cells 243 000 (#DOFS ≈ 1.52 · 108) on Cartesian grid• ≈ 6.1 GB memory consumption on a desktop machine• implicit Runge-Kutta DIRK34 based on matrix-free Newton-Krylov

    (GMRES)

    Thanks to Robert Klöfkorn for all these scalling results!Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 21 / 44

  • Measuring Efficiency

    Comparison with COSMO (German weather service)Dune: Crete 2013

    DUNEPrognostic variables:density, momentum, and potential temperature density.

    ∂tρ +∇ · (ρ~v) = 0,

    ∂t (ρ~v) +∇ · (ρ~v~v + p) = −ρ~g +∇ · (µρ∇~v),

    ∂(ρθ) +∇ · (ρθ~v) = ∇ · (µρ∇θ),

    Space:DG method of order k = 3, 4, 5 on cubes includingorography (linear)Diffussion: CDG2 method (Brdar, D., Klöfkorn ’11)

    Time:explicit time discretization (order 3)

    COSMOPrognostic variables:velocity, pressure, temperature.

    ∂t~v +~v · ∇~v = −1

    ρ∇p′ − ~g

    ρ′

    ρ+

    1

    ρ∇T,

    ∂t p′ +~v · ∇(p̄ + p′) = −

    cpcv

    p∇ ·~v,

    ∂t T′ +~v · ∇(T̄ + T ′) = −

    R

    cvT∇ ·~v −

    1

    cpρ∇~Js,

    Space:Staggered grid FD scheme with terrain following coordinates.5th order upwind for horizontal advectionsecond order central otherwise (sound and gravity waves)Time:Large time steps for horizontal terms, otherwise small timesteps and implicit for vertical terms.

    Comparison of dynamical cores for NWP models: comparison of COSMO and Dune in TCFD.

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 22 / 44

  • Measuring Efficiency

    Falling warm bubble (Straka ’93)Dune: Crete 2013

    error vs. run time

    Comparison: potential temperature perturbation at z = 1200msame number of dofs same error

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 23 / 44

    ./movies/straka.mpg

  • DUNE-fem-howto

    OutlineDune: Crete 2013

    1 Introduction to DUNE-FEM (Part 2)

    2 Measuring Efficiency

    3 DUNE-fem-howto

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 24 / 44

  • DUNE-fem-howto

    Structure of howto (same code base used for school)Dune: Crete 2013

    01.intro-1 setup a grid and do some simple calculations02.intro-2 compute the error of an L2 interpolation

    03a.fv-1 solve ∂tu +∇ · f (u) = S(u) (fv scheme)04a.fv-2 ... extend to Euler equations of gas dynamics

    03b.poisson-1 solve −∇ · D(x)∇u(x) + m(x)u(x) = f (x) (matrix free)04b.poisson-2 ... assemble system matrix (continuous finite-elements)06.nonlinear ... extend to −∇ · D(x ,u(x),∇u(x)) + m(x ,u(x)) = 0

    09.afem ... add local adaptivity10.poisson-dg solve elliptic problem using DG-IP method

    05.heat add theta scheme07.moving ... add moving domain

    08.spiral ... system of reaction diffusion equationsexample-dataio checkpointing and extended result outputexample-odesolver use IMEX schemesexample-solvers switch between different solvers (DUNE-FEM internal,

    DUNE-ISTL, umfpack, PetSc)Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 25 / 44

  • DUNE-fem-howto

    Results for the Poisson equation −∆u = fDune: Crete 2013

    Same code: different grids, different order(part of DUNE-fem-howto)

    FEM Q1

    FEM P1

    FEM Q6

    FEM P1Strong scalling

    64 128 256 512

    6e-4

    1e-3

    2e-3

    3e-3 4e-3

    6e-3

    1e-2

    ove

    rall

    run

    tim

    e

    number of processors

    optimal slopecached ALUCubeGrid

    64 128 256 512 0.55

    0.6

    0.65

    0.7

    0.75

    0.8

    0.85

    0.9

    0.95

    1

    eff

    icie

    ncy

    number of processors

    optimal efficiencycached ALUCubeGrid

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 26 / 44

  • DUNE-fem-howto

    Image Registration: ∂t~u − ε4~u = ~F (~u) + statisticsDune: Crete 2013

    Project with computer science group in BaselDamaged hand (test case)

    blue: target image I0black: new image I

    red: registrationI(x + u(x))

    (zero level set shown)

    Damaged femur (with artificial hip joint)

    using 50 previous registrations

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 27 / 44

  • DUNE-fem-howto

    Tsunami Near the Pacific Coast (Chile)Dune: Crete 2013

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 28 / 44

    ./movies/chile2a.avi

  • DUNE-fem-howto

    PDEs on moving surfacesDune: Crete 2013

    Heat equation (DUNE-fem-howto)

    Burgers equation

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 29 / 44

    ./movies/bat.avi./movies/newshock.avi

  • DUNE-fem-howto

    Parabolic models within the DUNE-FEM-HOWTODune: Crete 2013

    We are interested in the solution of the non linear elliptic problem:

    L[u](x) = f (x) ,

    with

    L[u](x) = −∇ · D(u(x), x)∇u(x) + m(u(x), x) ,

    and natural boundary conditions.

    The weak formulation of this problem is (ϕ ∈ H1(Ω)):∫ΩG

    m(u(x), x)︸ ︷︷ ︸source

    ϕ(x) + D(u(x), x)∇u(x)︸ ︷︷ ︸diffusiveFlux

    ·∇ϕ(x) dx =∫

    ΩG

    f (x)ϕ(x) dx .

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 30 / 44

  • DUNE-fem-howto

    Interface MethodsDune: Crete 2013

    The problem is described by a model class:templatestruct Model{

    // return m(u,x)templatevoid source (const Entity &entity, const Point &x,

    const RangeType &value, RangeType &flux) const;//! return D(u,x) grad utemplatevoid diffusiveFlux (const Entity &entity, const Point &x,

    const RangeType &value,const JacobianRangeType &gradient,JacobianRangeType &flux) const;

    // return a grid function for fRightHandSideType rightHandSide() const;

    This is the interface used to implement L withsource(e,x,v,f) : f = m(v ,Fe(x))diffusiveFlux(e,x,v,g,f) : f = D(v ,Fe(x))g

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 31 / 44

  • DUNE-fem-howto

    Newton MethodDune: Crete 2013

    Given a fixed state ū, the linearization L[v ; ū] of L[u] is defined by∫ΩG

    DL[v ; ū]ϕ =∫

    ΩG

    (Du(ū)∇ūv + D(ū)∇v

    )· ∇ϕ+

    ∫ΩG

    mu(ū)vϕ.

    The Newton method for solving the non linear problem is

    un+1 = un + (DL[·; un])−1L[un].

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 32 / 44

  • DUNE-fem-howto

    Newton SolverDune: Crete 2013

    The NewtonInverseOperator is an inverse operator:templateclass NewtonInverseOperator;

    The template arguments of NewtonInverseOperator are:Operator: The operator to be inverted (must be a

    DifferentiableOperator)LinearInvOperator: A linear inverse operator

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 33 / 44

  • DUNE-fem-howto

    New Interface MethodsDune: Crete 2013

    In the model we combine all terms multiplied with ∇ϕ into one methodand the term multiplied with ϕ into a second:// mu(ū)vtemplatevoid linSource (const RangeType &uBar,

    const Entity &entity,const Point &x,const RangeType &value,RangeType &flux) const;

    // Du(ū)∇ūv + D(ū)∇vtemplatevoid linDiffusiveFlux (const RangeType &uBar,

    const JacobianRangeType &gradientBar,const Entity &entity,const Point &x,const RangeType &value,const JacobianRangeType &gradient,JacobianRangeType &flux) const;

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 34 / 44

  • DUNE-fem-howto

    Static vs. Dynamic PolymorphismDune: Crete 2013

    A possible implementation of this model for D(x) = I is given bytemplate< class FunctionSpace, class GridPart >struct DiffusionModel {

    typedef ProblemInterface< FunctionSpace > Problem;DiffusionModel ( const Problem &, const GridPart & );void source ( entity, x, value, flux ) const {const DomainType xGlobal = entity.geometry().global( x );RangeType m;problem_.m(xGlobal,m);for (unsigned int i=0;i

  • DUNE-fem-howto

    A Parabolic ProblemDune: Crete 2013

    We want to solve the time-dependent equation:

    ∂tu + L[u] = f , u(0) = u0

    with an elliptic operator L of the form

    L[u] = −∇ · D(u(x), x)∇u(x) + m(u(x), x).

    We split the operator into two parts and use Rothe’s method to obtain

    un+1 + ∆tL1[un+1] = f (tn) + un + ∆tL2[un].

    Note: higher order methods available based on method of lines idea.

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 36 / 44

  • DUNE-fem-howto

    Time DiscretizationDune: Crete 2013

    After time discretization we have

    un+1 + ∆tL1[un+1] = f (tn) + un + ∆tL2[un].

    which is of the following form

    LI [un+1] = LE [un]

    with two elliptic operators (D1 − D2 = ∆tD,m1 −m2 = ∆tm):

    LI [u] = −(∇ · D1(u(x), x)∇u(x) + m1(u(x), x)

    )+ u,

    LE [u] = −(∇ · D2(u(x), x)∇u(x) + m2(u(x), x) + f

    )+ u.

    We solve this non-linear equation with a simple one step method(ū = un):

    DLI [un+1; un] = LE [un].

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 37 / 44

  • DUNE-fem-howto

    Solution Strategy (Cont.)Dune: Crete 2013

    Example (θ-scheme): For a linear operator:

    L[u] = −∇ · D(x)∇u(x) + m(x)u(x).

    we get

    LI [u] = −∇ ·( =D1︷ ︸︸ ︷θ∆t D

    )∇u +

    ( =m1︷ ︸︸ ︷θ∆t m

    )u + u

    LE [u] = −∇ ·(

    (θ − 1)∆t D︸ ︷︷ ︸=D2

    )∇u +

    ((θ − 1)∆t m︸ ︷︷ ︸

    =m2

    )u + u + f

    We need two models with for example source functions:

    sI(x ,u) = (m1(x) + 1)u , sE (x ,u) = (m2(x) + 1)u + f (x).

    (or one model with a bool switch).Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 38 / 44

  • DUNE-fem-howto

    From scalar heat equation to diffusion-reaction systemDune: Crete 2013

    Changing the gridStart with 05.heat• source: C++ source files• data: parameter and grid files• output: vtk output for paraviewGo to src• make main• ./main• call paraview and look at

    results (in ../output)Change polynomial ordermake POLORDER=3 main

    Change grid manager , e.g.,• make GRIDTYPE=ALUGRID_SIMPLEX main,• make GRIDDIM=3 main, or• make GRIDDIM=2 WORLDDIM=3 main

    (here we need to change themacrogrid in ../data/parameter)

    implement a new dgf filetry parallel• make GRIDTYPE=YASPGRID main and

    call• mpiexec -np 2 ./main Note: need

    ALUGRID or YaspGrid(might be a problem on the system)

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 39 / 44

  • DUNE-fem-howto

    From scalar heat equation to diffusion-reaction systemDune: Crete 2013

    Changing the model (Barkley model (use c = 100)

    ∂tu0 −4u0 = 50u0(1− u0

    )(cu0 −

    43

    (cu1 + 1))

    ∂tu1 = c(u0 − u1)

    1 switch to a function space of range 2 (main.cc:22):typedef Dune::Fem::FunctionSpace< ..., 2 > FunctionSpaceType;

    2 modify the class DiscreteModel (heatmodel.hh):void source ( const Entity &entity,... ) {double uth = 4./3.*(100.*value[1]+1.);flux[ 0 ] = 50.*value[0]*(1.-value[0])*(100.*value[0]-uth);flux[ 1 ] = 100.*(value[0]-value[1]);flux *= timeProvider().deltaT();// time diescretizationflux += value;

    }

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 40 / 44

  • DUNE-fem-howto

    From scalar heat equation to diffusion-reaction systemDune: Crete 2013

    Changing the model (Barkley model)1 modify the initial data (heat.hh):

    virtual void u(const DomainType& x, ...) const {phi[ 0 ] = x[1]>.01?1:0;phi[ 1 ] = x[0]

  • DUNE-fem-howto

    From scalar heat equation to diffusion-reaction systemDune: Crete 2013

    Changing the model (Barkley model) Semi-implicit time stepping:{−∆t∆un+10 +

    (1− 50∆t

    (1− un0

    )(cun0 − u

    nth

    ))un+10 = u

    n0 u

    n0 ≤ u

    nth

    −∆t∆un+10 +(

    1 + 50∆tun0(

    cun0 − unth

    ))un+10 = u

    n0 + 50∆tu

    n0

    (cun0 − u

    nth

    )un0 > u

    nth

    with unth =43 (cu

    n1 + 1)

    void source ( const Entity &entity,... ) {double uth = 4./3.*(100.*value[1]+1.);if (value[0] > uth)flux[ 0 ] = 50.*value[0]*(100.*value[0]-uth);

    else flux[0] = 0.flux[ 1 ] = 100.*(value[0]-value[1]);flux *= timeProvider().deltaT();flux += value;

    }void linSource ( const RangeType& uBar,... ) {

    double uth = 4./3.*(100.*uBar[1]+1.);if (uBar[0] < uth)flux[ 0 ] = 50.*(1-uBar[0]*(100.*uBar[0]-uth);

    elseflux[ 0 ] = 50.*uBar[0]*(100.*uBar[0]-uth);

    flux[1] = 0.;flux *= timeProvider().deltaT() * value[0];flux += value;

    }

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 42 / 44

  • DUNE-fem-howto

    Spiral waves: ∂t~u − ε4~u = ~F (~u)Dune: Crete 2013

    Student project

    Spiral wave in 2D

    Spiral wave on moving surface

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 43 / 44

    ./movies/spiral.avi./movies/spiral_moving.avi

  • DUNE-fem-howto

    Other problems: ∂t~u +∇ ·~f (~u) = ~S(~u)Dune: Crete 2013

    1 start with 04b.fv/source1 make main2 ./main3 call paraview and look at results (in ../output)

    2 run adaptive: finitevolume.repeats: -1 in ..//data/parameter3 change from scalar liner transport to Euler equation (main.cc:34):

    typedef EulerModel< ... > ModelType;

    Andreas Dedner (University of Warwick) Introduction to the DUNE Software Library January 2013 44 / 44

    Introduction to [Fem] (Part 2)Measuring Efficiency-fem-howto