NGsolve::Give me your element

Similar documents
NGsolve::Come to the edge

NETGEN/NGSolve Manual

TMA4220: Programming project - part 1

Computational Methods for Engineering Applications II. Homework Problem Sheet 4

An introduction to plotting data

Solution of Non-Homogeneous Dirichlet Problems with FEM

An Introduction to Netgen and NGSolve Session 1 Using Netgen/NGSolve

CMU CS 462/662 (INTRO TO COMPUTER GRAPHICS) HOMEWORK 0.0 MATH REVIEW/PREVIEW LINEAR ALGEBRA

Star Cluster Photometry and the H-R Diagram

Exercise 3: GIS data on the World Wide Web

Integration using Gaussian Quadrature

ngsxfem: Geometrically unfitted discretizations with Netgen/NGSolve (

Advanced Higher Order Finite Elements From Theory to Application with Netgen/NGSolve

CityGML XFM Application Template Documentation. Bentley Map V8i (SELECTseries 2)

August 7, 2007 NUMERICAL SOLUTION OF LAPLACE'S EQUATION

Using Microsoft Excel

Life Cycle of Stars. Photometry of star clusters with SalsaJ. Authors: Daniel Duggan & Sarah Roberts

Leaf Spring (Material, Contact, geometric nonlinearity)

M E R C E R W I N WA L K T H R O U G H

VPython Class 2: Functions, Fields, and the dreaded ˆr

Lesson 3: Advanced Factoring Strategies for Quadratic Expressions

Introduction. Name: Basic Features of Sunspots. The Solar Rotational Period. Sunspot Numbers

Task 1: Open ArcMap and activate the Spatial Analyst extension.

This format is the result of tinkering with a mixed lecture format for 3 terms. As such, it is still a work in progress and we will discuss

LAB Exercise #4 - Answers The Traction Vector and Stress Tensor. Introduction. Format of lab. Preparation reading

Department of Chemical Engineering University of California, Santa Barbara Spring Exercise 2. Due: Thursday, 4/19/09

Comp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33

Task 1: Start ArcMap and add the county boundary data from your downloaded dataset to the data frame.

Distributed Memory Parallelization in NGSolve

CS 7B - Fall Final Exam (take-home portion). Due Wednesday, 12/13 at 2 pm

2. Polynomial interpolation

EXPERIMENT 7: ANGULAR KINEMATICS AND TORQUE (V_3)

Lab 1: Numerical Solution of Laplace s Equation

Multiphysics Modeling

MATH 333: Partial Differential Equations

Unit 15 Section 3 : Symmetry

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

progeny. Observe the phenotypes of the F1 progeny flies resulting from this reciprocal cross.

Foundations of Computation

Calculating NMR Chemical Shifts for beta-ionone O

Applied C Fri

Studying Topography, Orographic Rainfall, and Ecosystems (STORE)

LESSON 9.1 ROOTS AND RADICALS

Instructor Notes for Chapters 3 & 4

LivePhoto Physics Activity 3. Velocity Change. Motion Detector. Sample

Exercise in gas chromatography

Chemistry 14CL. Worksheet for the Molecular Modeling Workshop. (Revised FULL Version 2012 J.W. Pang) (Modified A. A. Russell)

10.1.1Theorem: Module 4 : Local / Global Maximum / Minimum and Curve Sketching

Lesson 5b Solving Quadratic Equations

Section 2.1 Differential Equation and Solutions

Core Mathematics 2 Algebra

LAB 3: WORK AND ENERGY

Name Date Class. Figure 1. The Google Earth Pro drop-down menu.

Conformational Analysis of n-butane

C++ For Science and Engineering Lecture 17

Machine Learning: Homework 5

IOP2601. Some notes on basic mathematical calculations

Lab 15 Taylor Polynomials

Creating Axisymmetric Models in FEMAP

Signal Processing First Lab 11: PeZ - The z, n, and ˆω Domains

LAB 2 - ONE DIMENSIONAL MOTION

Numerical solution of the time-independent 1-D Schrödinger equation. Matthias E. Möbius. September 24, 2010

Proofs, Strings, and Finite Automata. CS154 Chris Pollett Feb 5, 2007.

Date: Summer Stem Section:

Solving Differential Equations on 2-D Geometries with Matlab

Assignment 1 Physics/ECE 176

Module 10: Free Vibration of an Undampened 1D Cantilever Beam

Modeling a Composite Slot Cross-Section for Torsional Analysis

through any three given points if and only if these points are not collinear.

Introduction to lambda calculus Part 6

Lab 1: Handout GULP: an Empirical energy code

Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

Synteny Portal Documentation

DOC SOLVING SYSTEMS OF EQUATIONS BY SUBSTITUTION CALCULATOR

Handling Raster Data for Hydrologic Applications

Project IV Fourier Series

Does Air Have Mass? 1.3 Investigate

A short introduction to FreeFem++

Photometry of a Globular Cluster

University of TN Chattanooga Physics 1040L 8/18/2012 PHYSICS 1040L LAB LAB 4: R.C. TIME CONSTANT LAB

Lab Partner(s) TA Initials (on completion) EXPERIMENT 7: ANGULAR KINEMATICS AND TORQUE

Data Structures & Database Queries in GIS

Relative Photometry with data from the Peter van de Kamp Observatory D. Cohen and E. Jensen (v.1.0 October 19, 2014)

The Discontinuous Galerkin Finite Element Method

Chapter 1, Section 1.2, Example 9 (page 13) and Exercise 29 (page 15). Use the Uniqueness Tool. Select the option ẋ = x

Temperature measurement

University of Illinois at Urbana-Champaign College of Engineering

1 Solution of Electrostatics Problems with COM- SOL

Connect the Vernier spectrometer to your lap top computer and power the spectrometer if necessary. Start LoggerPro on your computer.

Using the Stock Hydrology Tools in ArcGIS

Algebra III and Trigonometry Summer Assignment

DSP First Lab 11: PeZ - The z, n, and ωdomains

WMS 10.1 Tutorial GSSHA Applications Precipitation Methods in GSSHA Learn how to use different precipitation sources in GSSHA models

AMS 132: Discussion Section 2

EOSC 110 Reading Week Activity, February Visible Geology: Building structural geology skills by exploring 3D models online

Newton's Laws and Atwood's Machine

The Quantizing functions

LED Lighting Facts: Product Submission Guide

LAB 5 INSTRUCTIONS LINEAR REGRESSION AND CORRELATION

Scientific Computing Software Concepts for Solving Partial Differential Equations

Transcription:

NGsolve::Give me your element And let your eyes delight in my ways Jay Gopalakrishnan Portland State University Winter 2015 Download code (works only on version 6.0) for these notes from here. Give me your heart / And let your eyes delight in my ways. The Bible Jay Gopalakrishnan 1/18

Contents 1 Automatic differentiation 2 Shape functions 3 Finite elements and spaces 4 Orientation 5 Bicubic qualdrilateral element Jay Gopalakrishnan 2/18

Automatic differentiation Goal: Create variables that know how to (exactly) differentiate themselves. Idea: Differentiation obeys some rules (product rule, quotient rule etc) that we can implement by overloading operators, e.g., overload * to implement i (f g) = f ( i g) + g( i f ). Suppose an object representing x i (the ith coordinate) knows its value and the value of its derivatives, at any given point. Then, we can compute both the value and the value of derivatives of x i x j by overloading * as above. Jay Gopalakrishnan 3/18

A minimalist class for differentiation template<i n t D> c l a s s MyDiffVar {// My d i f f e r e n t i a b l e v a r i a b l e s // F i l e : d i f f e r e n t i a b l e s. hpp double Value ; double D e r i v a t i v e s [D ] ; p u b l i c : MyDiffVar ( ) { } ; MyDiffVar ( double x i, i n t i ) { // i th c o o r d i n a t e x i has Value = x i ; // grad = i th u n i t v e c t o r f o r ( auto & d : D e r i v a t i v e s ) d = 0. 0 ; D e r i v a t i v e s [ i ] = 1. 0 ; } double GetValue ( ) const { r e t u r n Value ; } double& SetValue ( ) { r e t u r n Value ; } double G e t D e r i v a t i v e ( i n t i ) const { r e t u r n D e r i v a t i v e s [ i ] ; } double& S e t D e r i v a t i v e ( i n t i ) { r e t u r n D e r i v a t i v e s [ i ] ; } } ; Jay Gopalakrishnan 4/18

Overload * for the differentiables Template implementation of implement i (f g) = f ( i g) + g( i f ): // implement p r o d u c t r u l e template<i n t D> MyDiffVar<D> operator ( const MyDiffVar<D> & f, const MyDiffVar<D> & g ) { } MyDiffVar<D> f g ; f g. S etvalue ( ) = f. GetValue ( ) g. GetValue ( ) ; f o r ( i n t i =0; i <D; i ++) f g. S e t D e r i v a t i v e ( i ) = f. GetValue ( ) g. G e t D e r i v a t i v e ( i ) + g. GetValue ( ) f. G e t D e r i v a t i v e ( i ) ; r e t u r n f g ; Quiz: Open the file and provide operators +,, and /. Jay Gopalakrishnan 5/18

Using your class #i n c l u d e d i f f e r e n t i a b l e s. hpp // F i l e d0. cpp using namespace s t d ; i n t main ( ) { MyDiffVar <2> x ( 0. 5, 0 ), y ( 2. 0, 1 ) ; } cout << x : << x << e n d l << y : << y << e n d l << x y : << x y << e n d l << x y y+y : << x y y+y << e n d l ; Using your simple class, you can now differentiate polynomial expressions built using x and y coordinates (or x i, i = 1,..., N, in N-dimensions). Jay Gopalakrishnan 6/18

Exercise! How would you modify differentiables.hpp so that you can also differentiate expressions like sin(xy)? Make sure your modified file compiles and runs correctly with this driver: #i n c l u d e d i f f e r e n t i a b l e s. hpp // F i l e d0x. cpp using namespace s t d ; i n t main ( ) { MyDiffVar <2> x ( 0. 5, 0 ), y ( 2. 0, 1 ) ; } cout << x : << x << e n d l << y : << y << e n d l << s i n ( x y )/ y : << s i n ( x y )/ y << e n d l ; Jay Gopalakrishnan 7/18

Netgen s AutoDiff class An implementation of these ideas is available in $NGSRC/netgen/libsrc/general/autodiff.hpp. Here is an example showing how to use it: #i n c l u d e <fem. hpp> // F i l e d1. cpp using namespace s t d ; i n t main ( ) { } AutoDiff <2> x ( 0. 5, 0 ), y ( 2. 0, 1 ) ; // x and y c o o r d s AutoDiff <2> b [ 3 ] = { x, y, 1 x y } ; // b a r y c e n t r i c c o o r d s cout << x : << x << e n d l << y : << y << e n d l << x y : << x y << e n d l << x y y+y : << x y y+y << e n d l << ( b0 b1 b2 1)/y : << ( b [ 0 ] b [ 1 ] b [ 2 ] 1)/ y << e n d l ; We will use AutoDiff and the following classes to program finite elements. Jay Gopalakrishnan 8/18

FlatVector, SliceVector, etc. #i n c l u d e <b l a. hpp> // F i l e : f l a t v e c. cpp using namespace s t d ; using namespace n g b l a ; i n t main ( ) { double mem [ ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1 0 } ; F l a t V e c t o r <double> f 1 ( 2,mem) ; // A v e c t o r c l a s s t h a t s t e a l s F l a t V e c t o r <double> f 2 ( 2,mem+3); // memory from e l s e w h e r e. cout << f 1 : \ n << f 1 << e n d l ; // This p r i n t s 1, 2. cout << f 2 : \ n << f 2 << e n d l ; // This p r i n t s 5, 6. S l i c e V e c t o r <> s1 ( 4, 2,mem) ; // A l s o s t e a l s memory. cout << s1 : \ n << s1 << e n d l ; // This p r i n t s 1, 3, 5, 7. S l i c e V e c t o r <> s2 ( 5, 1,mem+4); // What i s t h i s? // : SliceVector class does not allocate or delete memory. Their constructors just create/copy pointers. Jay Gopalakrishnan 9/18

class ScalarFiniteElement template <i n t D> c l a s s S c a l a r F i n i t e E l e m e n t : p u b l i c F i n i t e E l e m e n t { v i r t u a l void CalcShape ( const I n t e g r a t i o n P o i n t & ip, S l i c e V e c t o r <> shape ) const = 0 ; v i r t u a l void CalcDShape ( const I n t e g r a t i o n P o i n t & ip, S l i c e M a t r i x <> dshape ) const = 0 ; //... } ; shape and dshape are cheap to pass by value as function arguments even when they contain many elements. Any derived finite element class must provide shape functions and their derivatives. Jay Gopalakrishnan 10/18

Visualizing finite element shape functions # FILE: shapes.pde geometry = square.in2d mesh = squaretrg.vol fespace v -type=h1ho -order=2 gridfunction u -fespace=v numproc shapetester nptest -gridfunction=u The numproc shapetester is an NGsolve tool to visualize global basis functions (called global shape functions) of an FESpace. Load this PDE file. Click Solve button before doing anything else. Look for a tiny window called Shape Tester that pops up. The number (0,1,... ) that you input in Shape Tester window determines which basis function will be set in gridfunction u. Got to Visual menu and pick gridfunction u to visualize. Jay Gopalakrishnan 11/18

Visualizing finite element shape functions # FILE: shapes.pde geometry = square.in2d mesh = squaretrg.vol fespace v -type=h1ho -order=2 gridfunction u -fespace=v numproc shapetester nptest -gridfunction=u 1, 2,... 25... etc. Global shape functions Jay Gopalakrishnan 11/18

Prepare to write your own finite element Study these files in the folder my_little_ngsolve: myelement.hpp, myelement.cpp, myhoelement.hpp, myhoelement.cpp All elements in a mesh are mapped from a fixed reference element. Pay particular attention to CalcShape(..) and CalcDshape(..). They give the values and derivatives of all local shape functions on the reference element. myfespace.hpp, myfespace.cpp, myhofespace.hpp, myhofespace.cpp Each global degree of freedom ( dof ) gives a global basis function and is associated to a geometrical object of the mesh (like a vertex, edge, or element). Pay particular attention to GetDofNrs(...), which return global dof-numbers connected to an element. Jay Gopalakrishnan 12/18

Homework Your assignment is to code the bicubic finite element Q 3 in bicubicelem.cpp. On the reference element, the unit square, this element consists of the space of functions Q 3 = span{x i y j : 0 i 3, 0 j 3}. Also code a bicubic finite element space (derived from FESpace), for any mesh of quadrilateral elements, in file bicubicspace.cpp Then, use your space to approximate the operator + I and solve a Neumann boundary value problem. Tabulate errors. The ensuing slides give you hints to complete this homework and suggest separating the work into smaller separate tasks. Jay Gopalakrishnan 13/18

Bicubic shape functions on unit square Task 1: In bicubicelem.cpp, provide shape functions. E.g., here is a valid basis set of shape functions (you may use others) : Vertex basis Edge basis φ 0 = (1 x)(1 y) φ 1 = (1 x)y φ 2 = x(1 y) φ 3 = xy φ 4 = (φ 0φ 1)φ 0 φ 5 = (φ 0φ 1)φ 1 φ 6 = (φ 1φ 3)φ 3 φ 7 = (φ 1φ 3)φ 1 :...... Interior basis φ 4 = (1 x)(1 y)xyφ 0 φ 4 = (1 x)(1 y)xyφ 1 :... Jay Gopalakrishnan 14/18

Bicubic shape functions on unit square Task 1: In bicubicelem.cpp, provide shape functions. Your basis expressions should go into the CalcShape member function. For the CalcDShape member function, you can use AutoDiff variables and the same expressions you need in CalcShape. Consider simplifying your code so that you only type the basis expressions once. Jay Gopalakrishnan 14/18

Orientation Task 2: In bicubicspace.cpp, write your finite element space. Remember to keep track of matching local and global orientation (go back and revise your bicubicelem.cpp if necessary). / What i s the l o c a l o r i e n t a t i o n? I s the o r d e r i n g o f v e r t i c e s and edges w i t h i n the r e f e r e n c e element v2 e1 v3 v3 e1 v2 o o o o e2 e3 e2 e3 o o o o or something v0 e0 v1, v0 e0 v1, e l s e? / What is the global orientation? NGsolve s mesh edges are directed/oriented. If edge shape functions from adjacent elements are not given in that orientation, then you may lose continuity! Jay Gopalakrishnan 15/18

Check your basis Task 3: Compile the code you wrote and make a shared library make libmyquad.so and check your basis functions on the three given quadrilateral mesh files. # FILE : bicubicshapes.pde geometry = square.in2d #mesh = squarequad1.vol.gz #mesh = squarequad2.vol.gz mesh = squarequad3.vol.gz shared = libmyquad define fespace v -type=myquadspace define gridfunction u -fespace=v numproc shapetester nptest -gridfunction=u Jay Gopalakrishnan 16/18

Solve a PDE Task 4: Using your finite element space, solve this boundary value problem: u + u = f on Ω u/ n = 0 on Ω. Hints: Do you know the variational formulation for this problem? You want to write a PDE file that mixes your finite element space with the NGSolve integrators. E.g., the NGSolve integrator laplace, can work with any finite element which provides CalcDShape, by dynamic polymorphism. Jay Gopalakrishnan 17/18

Solve a PDE Task 4: Using your finite element space, solve this boundary value problem: u + u = f on Ω This task includes these steps: u/ n = 0 on Ω. 1 Set f so that your exact solution is u = sin(πx) 2 sin(πy) 2. 2 Compute the L 2 (Ω) error (code this either in your own C++ numproc like we did before or find facilities to directly do it in the pde file). 3 Solve on mesh = squarequad3.vol.gz by loading your pde file and pressing the Solve button. Compute the L 2 (Ω)-error. Note it down. 4 Pressing the Solve button again to solve and compute the L 2 -error on a uniformly refined mesh. Note the L 2 -error. Repeat (until you can t). 5 What is the rate of convergence of L 2 -error with meshsize? Jay Gopalakrishnan 17/18

Project Student Team Project: Learn about the DPG method and download an implementation of it in GitHub. Your job is to extend it to quadrilateral elements. You will need to code a new finite element space that will serve as the test space for the DPG method. Details will be progressively made clear as you proceed with the project. Jay Gopalakrishnan 18/18