An (incomplete) Introduction to FreeFem++

Similar documents
Finite Elements for Fluids Prof. Sheu

phenomena during acupuncture Yannick Deleuze Yannick Deleuze

Lecture of FreeFEM Update:2015/7/29

Locally chosen grad-div stabilization parameters for finite element discretizations of incompressible flow problems

An Introduction to FreeFem++ UPMC, December 2011 O. Pantz. CMAP, Ecole Polytechnique.

2D Plotting with Matlab

Lab 2: Photon Counting with a Photomultiplier Tube

A short introduction to FreeFem++

Solving PDEs with freefem++

LEAST-SQUARES FINITE ELEMENT MODELS

Numerical simulations and mesh adaptation : exercise sessions Charles Dapogny 1, Pascal Frey 2, Yannick Privat 2

Finite Elements. Colin Cotter. January 15, Colin Cotter FEM

C++ For Science and Engineering Lecture 14

FEniCS, part IV: Generating a model

Homework 4 in 5C1212; Part A: Incompressible Navier- Stokes, Finite Volume Methods

Recent advances in HPC with FreeFem++

NUMERICAL SIMULATION OF INTERACTION BETWEEN INCOMPRESSIBLE FLOW AND AN ELASTIC WALL

JMBC Computational Fluid Dynamics I Exercises by A.E.P. Veldman


von Kàrmàn vortex street FEniCS, part IV: Generating a model Clouds Discussed in FEniCS book Fluid flow past an obstacle is not steady

Practical Exercises 7

Project One: C Bump functions

On the parameter choice in grad-div stabilization for the Stokes equations

(f P Ω hf) vdx = 0 for all v V h, if and only if Ω (f P hf) φ i dx = 0, for i = 0, 1,..., n, where {φ i } V h is set of hat functions.

6. How Functions Work and Are Accessed. Topics: Modules and Functions More on Importing Call Frames

Transient flow and heat equations - the Rayleigh-Benard instability

Project 4/5 - Molecular dynamics part II: advanced study of Lennard-Jones fluids, deadline December 1st (noon)

Introduction to FARGO3D

AMDiS Usergroup-Meeting

Discrete Projection Methods for Incompressible Fluid Flow Problems and Application to a Fluid-Structure Interaction

SIMULATION OF FLUID-STRUCTURAL INTERACTION

HOMEWORK 3: Phase portraits for Mathmatical Models, Analysis and Simulation, Fall 2010 Report due Mon Oct 4, Maximum score 7.0 pts.

A posteriori error estimates applied to flow in a channel with corners

Lecture 5b: Starting Matlab

Math Homework 3 Solutions. (1 y sin x) dx + (cos x) dy = 0. = sin x =

Graded Project #1. Part 1. Explicit Runge Kutta methods. Goals Differential Equations FMN130 Gustaf Söderlind and Carmen Arévalo

Lattice Boltzmann Method Solver Documentation

A First Course on Kinetics and Reaction Engineering Example S6.2

AMS subject classifications. Primary, 65N15, 65N30, 76D07; Secondary, 35B45, 35J50

LibMesh Experience and Usage

Some examples in domain decomposition

Homework Assignment 4 Root Finding Algorithms The Maximum Velocity of a Car Due: Friday, February 19, 2010 at 12noon

2.1 Identifying Patterns

Non-Newtonian Fluids and Finite Elements

FDM for parabolic equations

AP Calculus AB Summer Assignment. Due Date: First day of school.

Key exercise 1 INF5620

Dissection sparse direct solver for indefinite finite element matrices and application to a semi-conductor problem

Calculus BC: Section I

Preconditioners for the incompressible Navier Stokes equations

Numerical control and inverse problems and Carleman estimates

1 Introduction. J.-L. GUERMOND and L. QUARTAPELLE 1 On incremental projection methods

Homework 1 Solutions

The SIR Disease Model Trajectories and MatLab

Lab 5 - Capacitors and RC Circuits

arxiv: v1 [math.na] 28 Nov 2014

Study Resources For Algebra I. Unit 2A Graphs of Quadratic Functions

MATH 333: Partial Differential Equations

Lecture 26: Putting it all together #5... Galerkin Finite Element Methods for ODE BVPs

An Extended Finite Element Method for a Two-Phase Stokes problem

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

Numerical methods for the Navier- Stokes equations

MEI Casio Tasks for Mechanics


Vector and scalar penalty-projection methods

Study of Forced and Free convection in Lid driven cavity problem

Discretization of PDEs and Tools for the Parallel Solution of the Resulting Systems

Numerical simulation of the Gross-Pitaevskii equation by pseudo-spectral and finite element methods comparison of GPS code and FreeFem++

The Virtual Element Method: an introduction with focus on fluid flows

A First Course on Kinetics and Reaction Engineering Example S5.1

Finite Element Modules for Enhancing Undergraduate Transport Courses: Application to Fuel Cell Fundamentals

PDE Solvers for Fluid Flow

Matlab Section. November 8, 2005

Part 1. The diffusion equation

Example problem: Adaptive solution of the 2D advection diffusion equation

Octave. Tutorial. Daniel Lamprecht. March 26, Graz University of Technology. Slides based on previous work by Ingo Holzmann

EE3210 Lab 3: Periodic Signal Representation by Fourier Series

Some remarks on grad-div stabilization of incompressible flow simulations

MATERIAL MECHANICS, SE2126 COMPUTER LAB 2 PLASTICITY

Lab 5 CAPACITORS & RC CIRCUITS

Mixed Finite Element Methods. Douglas N. Arnold, University of Minnesota The 41st Woudschoten Conference 5 October 2016

Chapter 2. General concepts. 2.1 The Navier-Stokes equations

Proper Orthogonal Decomposition (POD) for Nonlinear Dynamical Systems. Stefan Volkwein

7-1A. Relationships Between Two Variables. Vocabulary. Using the Formula d = r t. Lesson

Due Tuesday, November 23 nd, 12:00 midnight

Maps and differential equations

Notater: INF3331. Veronika Heimsbakk December 4, Introduction 3

AP Physics 1 Summer Assignment

Vorticity and Dynamics

MAT 275 Laboratory 4 MATLAB solvers for First-Order IVP

WELL POSEDNESS OF PROBLEMS I

LibMesh Experience and Usage

Root Tutorial: Plotting the Z mass

FEM for Stokes Equations

MITOCW watch?v=dztkqqy2jn4

Continuum Mechanics Lecture 5 Ideal fluids

Numerical Optimization of Partial Differential Equations

WILEY. Differential Equations with MATLAB (Third Edition) Brian R. Hunt Ronald L. Lipsman John E. Osborn Jonathan M. Rosenberg

Riemann Sums. Outline. James K. Peterson. September 15, Riemann Sums. Riemann Sums In MatLab

Flow-Induced Vibration Analysis of Supported Pipes with a Crack

Transcription:

An (incomplete) Introduction to FreeFem++ Nathaniel Mays Wheeling Jesuit University November 12, 2011 Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 1 / 30

Model Problem - Stokes u + p = f u = 0 in Ω in Ω Models viscous fluid flow. u is the velocity vector < u 1, u 2 >. p is the pressure. u is the incompressibility condition Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 2 / 30

Stokes variationally Alternatively, if we multiply by a test function (v X and q Q) and integrate over the entire space, we end up with: find (u, p) in (X, Q) satisfying u : vdx + p vdx = f vdx v X Ω Ω Ω q u = 0 q Q Ω Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 3 / 30

Discretization Now the choice of discretization. Triangulate Ω into T h by some suitable method. Choose X h X that s finite dimensional. Choose Q h Q that s finite dimensional. X h and Q h need to satisfy the inf-sup condition (bad stuff happens otherwise). Typical elements are P2-P1 (Taylor-Hood) or P1bubble-P1 (Mini). Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 4 / 30

Stokes discretely If we look at discrete function spaces for our test functions (v h X h and q h Q h ) and integrate over the entire space, we end up with: find (u h, p h ) in (X h, Q h ) satisfying u h : v h dx + p h v h dx = f v h dx v h X h Ω Ω Ω q h u h = 0 q h Q h Ω Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 5 / 30

FreeFem++ Free Finite Element Method software Platforms: Unix, Windows, OS X Built on C++, so many commands have the same calls Wonderful for first calculations Outline: Declarations and structure Mesh generation and FE spaces Problem statements and programs Reports and output Future problems Parameter loops and post-processing Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 6 / 30

Declarations int i, j, maxit, one=1; real error, reynolds=1.0e4; string errorflag; string solution= testvariable ; func f1= 2.0 y; func real u1e (real t) { return cos(n pi x) sin(n pi y) exp( 2 n n pi pi t/tau); } real[int] isoval(10); Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 7 / 30

File Structure // Directories for saving data string plotdir = plots/ ; string datadir = data/ ; string reportdir = reports/ ; // Identifying prefix that will appear in filenames string problemprefix = channel ; //Create directories if they re not already here (FreeFem will NOT automatically do this) exec( mkdir p + plotdir); exec( mkdir p + datadir); exec( mkdir p + reportdir); Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 8 / 30

Meshing You can take advantage of FreeFem++ s meshing commands int meshpts=10; mesh Th = square(meshpts,meshpts); plot(th); Or stretch the square real xmin= 2.0,xmax = 5.0,ymin= 1.0,ymax=3.0; int meshpts=30; mesh Th = square(meshpts,meshpts,[xmin + (xmax xmin) x, ymin + (ymax ymin) y]); plot(th); Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 9 / 30

Borders and meshes // Label 1 will denote no slip boundary conditions. Label 2 will denote parabolic BC. border bottom(t=0,1) {x = ChLength t; y = 0.0; label = 1;} border right(t=0,1) {x = ChLength; y = t; label = 2;} border top(t=0,1) {x = ChLength (1.0 t); y = 1; label = 1;} border left(t=0,1) {x = 0.0; y = (1.0 t); label = 2;} border obstacle(t=0,2 pi) {x = ObsLength+ObsRadius cos(t); y = 0.5+ObsRadius sin(t); label = 1;} mesh Th = buildmesh( bottom(chlength meshperunit) + right( meshperunit) + top(chlength meshperunit) + left(meshperunit) + obstacle( 2 meshperunit) ); savemesh(th,datadir + problemprefix +.msh ); Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 10 / 30

Mesh Our wonderful mesh Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 11 / 30

Exercise 1 Create a domain with a rectangle with base 2 and height 3 and a semicircle (of radius 1) on the top. For the mesh, use approximately 10 mesh points per unit length (so 20 on the side of length 2, 31 for the semicircle). Plot the mesh. Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 12 / 30

Finite Element Spaces // Defining velocity and pressure spaces. fespace Xh(Th,P2); fespace Qh(Th,P1); // Initialize velocity and pressure functions Xh u1,u2; Qh p; // Initialize velocity and pressure test functions Xh v1,v2; Qh q; Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 13 / 30

Identifying your problems problem stokes([u1,u2,p],[v1,v2,q],solver=umfpack) = int2d(th)( ( dx(u1) dx(v1) + dx(u2) dx(v2) + dy(u1) dy(v1) + dy(u2) dy(v2) ) p ( dx(v1) + dy(v2) ) q ( dx(u1) + dy(u2) ) + PRESSURESTABILIZE p q ) int2d(th)( f1 v1 + f2 v2 ) + on( 1, u1=0.0, u2=0.0) + on( 2, u1=u1boundary, u2=0.0); Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 14 / 30

What about macros? Macros make your life a little less chaotic They only need to be debugged once. They clean up your code. Variables are just placeholders. They don t need to be initialized. They re slightly finicky (parenthesis) They don t end with ;, but // instead. macro div(u1,u2) ( dx(u1) + dy(u2) ) // macro H1norm(T,u1,u2) ( sqrt(int2d(t)( dx(u1)ˆ2 + dy(u1)ˆ2 + dx(u2 )ˆ2 + dy(u2)ˆ2 ) ) // Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 15 / 30

Useful! Macros! macro gradgrad(u1,u2,v1,v2) ( dx(u1) dx(v1) + dx(u2) dx(v2) + dy (u1) dy(v1) + dy(u2) dy(v2) ) // macro div(u1,u2) ( dx(u1) + dy(u2) ) // problem stokes([u1,u2,p],[v1,v2,q],solver=umfpack) = int2d(th)( gradgrad(u1, u2, v1, v2) p div(v1, v2) q div(u1, u2) + PRESSURESTABILIZE p q ) int2d(th)( f1 v1 + f2 v2 ) + on( 1, u1=0.0, u2=0.0) + on( 2, u1=u1boundary, u2=0.0); Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 16 / 30

Exercise 2 The Poisson equation is u = f. Create a problem statement in FreeFEM to solve this problem. Definite the solution space (use P1 functions) Definite the variables (u) Write out the variational form. Use Dirichlet boundary conditions (u = g on Ω) Using true solution u t = 1 + x 2 + y 2, we know f = 4 and g = 1 + x 2 + y 2. Calculate the error between your solution u and the true solution with: cout << Lˆ2 Error= <<sqrt( int2d(th)((u (1+xˆ2+yˆ2))ˆ2) ) << endl; Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 17 / 30

Programming starttime = clock(); stokes; comptime = clock(); plot(th,wait=1,cmm= Our wonderful mesh,ps=plotdir+ problemprefix + mesh.eps ); // Plots the mesh and then waits to continue. plot(p,value=1,fill=1,cmm= Contour of pressures,ps=plotdir+ problemprefix + pressure.eps,wait=1); plot([u1,u2],value=1,coef=0.1,cmm= Velocity vectors,ps=plotdir+ problemprefix + velocity.eps,wait=1); plot(p,[u1,u2],value=1,fill=1,coef=0.1,cmm= Pressure and velocity, wait=1); // Note the order of pressure and then velocity cout << Time = << comptime starttime << Solution norm = << int2d(th)(u1ˆ2 + u2ˆ2) << endl; Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 18 / 30

Pressure Contour of pressures IsoValue -37.3391-28.9141-23.2973-17.6806-12.0639-6.44722-0.830515 4.78619 10.4029 16.0196 21.6363 27.253 32.8697 38.4864 44.1031 49.7199 55.3366 60.9533 66.57 80.6117 Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 19 / 30

Velocity Velocity vectors Vec Value 0 0.0809877 0.161975 0.242963 0.323951 0.404939 0.485926 0.566914 0.647902 0.728889 0.809877 0.890865 0.971853 1.05284 1.13383 1.21482 1.2958 1.37679 1.45778 1.53877 Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 20 / 30

Together Pressure and velocity IsoValue -37.3391-28.9141-23.2973-17.6806-12.0639-6.44722-0.830515 4.78619 10.4029 16.0196 21.6363 27.253 32.8697 38.4864 44.1031 49.7199 55.3366 60.9533 66.57 80.6117 Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ Vec Value 0 0.0809877 0.161975 0.242963 0.323951 0.404939 0.485926 0.566914 0.647902 0.728889 0.809877 0.890865 0.971853 1.05284 1.13383 1.21482 1.2958 1.37679 1.45778 1.53877 November 12, 2011 21 / 30

Future Problems Hyperbolic problems, like the heat equation u t µ u = f, need another approach. One approach is Backward-Euler time stepping. Choose dt (time step size) Given u m Solve u m+1 u m µ u m+1 = f (t m+1 ) dt Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 22 / 30

Saving Data { ofstream file(datadir + problemprefix + mesh + meshperunit + u1.txt ); file << u1[] << endl; } { ofstream file(datadir + problemprefix + mesh + meshperunit + u2.txt ); file << u2[] << endl; } { ofstream file(datadir + problemprefix + mesh + meshperunit + p.txt ); file << p[] << endl; } Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 23 / 30

Reports Things that you probably should include in a report: Computation time (perhaps broken up into individual tasks/iterations as well as the entire run) clock() is the function to call system time. Mesh size (any parameters used to create the mesh, as well as number of triangles, min/max size of triangles, etc) Degrees of freedom (function of FEspaces, problem, mesh) List all relavent parameters. ANYTHING necessary to reproduce the experiment (and some things that aren t). Convergence criterion for nonlinear solvers, stabilization terms, everything. Directories where everything is saved. Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 24 / 30

Report Example ofstream report(reportdir + problemprefix + report.txt ); report << endl << << endl << endl; report << Problem Parameters << endl; report << ChLength = << ChLength << endl; report << ObsLength = << ObsLength << endl; report << ObsRadius = << ObsRadius << endl; report << meshperunit = << meshperunit << endl << endl; report << Boundary condition parameters << endl; report << maxspeed = << maxspeed << endl << endl; report << Numerical solver parameters << endl; report << PRESSURESTABILIZE = << PRESSURESTABILIZE << endl << endl; report << Results << endl; report << Time = << comptime starttime << endl; report << Solution norm = << int2d(th)(u1ˆ2 + u2ˆ2) << Nathaniel Mays (Wheeling endl; Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 25 / 30

Parameter Loops Easiest thing to change in code is parameters Multiple ways of changing for loop over parameters inside code cin command to make a call from commandline Types of things to check: Convergence properties with different Reynolds number (fluid flow) Solution properties with different borders Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 26 / 30

Parameter sweep example // Initialize meshperunit and all other variables needed mesh Th=square(2,2); // This is just to initialize the mesh, we re going to redefine it later. real meshperunitinitial = 4.0; int meshindexmax = 3; for(int meshindex = 0; meshindex < meshindexmax; meshindex++) { // Begin meshing loop meshperunit = meshperunitinitial 2.0ˆmeshIndex; Th = buildmesh(... ) // from earlier //Rest of the code to have problem statement and the program. //Report anything that you want reported from this mesh level. } // End meshing loop Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 27 / 30

Post Processing There are many reasons to set up your code for post processing. Running your code on a remote computer and don t have imaging abilities. You don t know what you need to know at the beginning of the experiment. Need new plots of old data. Need error reports with different norms. Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 28 / 30

Loading data mesh Th = readmesh(datadir + problemprefix +.msh ); fespace Xh(Th, P2); Xh u1, u2; { ifstream data(datadir + problemprefix + mesh + meshperunit + u1.txt ); data >> u1[]; } { ifstream data(datadir + problemprefix + mesh + meshperunit + u2.txt ); data >> u2[]; } Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 29 / 30

Batch Script Another way to do parameter sweeps: In linux: real reynolds, meshperunit, dt; cout << Please enter the reynolds number, mesh points per unit length, and time step << endl; cin >> reynolds >> meshperunit for reynolds in 1 1e2 1e4; do for meshing in 4 8 16; do echo $reynolds $meshing FreeFem++ stokes.edp > report${ reynolds} ${meshing}.txt&; done; done Also useful: for i in ls.eps ; do epstopdf $i; done Nathaniel Mays (Wheeling Jesuit University)An (incomplete) Introduction to FreeFem++ November 12, 2011 30 / 30