A First Step Towards Automatic PDE Code Ver ... - FEniCS Project
A First Step Towards Automatic PDE Code Ver ... - FEniCS Project
A First Step Towards Automatic PDE Code Ver ... - FEniCS Project
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
A <strong>First</strong> <strong>Step</strong> <strong>Towards</strong> <strong>Automatic</strong> <strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification<br />
Ola Skavhaug<br />
Kent-Andre Mardal<br />
Hans Petter Langtangen<br />
Simula Research Laboratory, Norway<br />
University of Oslo, Norway<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 1
Validation and <strong>Ver</strong>ification<br />
Validation:<br />
Is the <strong>PDE</strong> model appropriate?<br />
Or: Do we solve the right equations?<br />
Core interest among scientists and engineers<br />
<strong>Ver</strong>ification:<br />
Are the numerical methods correctly implemented?<br />
Or: Do we solve the equations right?<br />
Attracts much less interest than validation<br />
Validation requires successful verification<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 2
Generation of Analytical Solutions<br />
Given a <strong>PDE</strong> or system of <strong>PDE</strong>s:<br />
F(u) = 0 in Ω<br />
(F and u may be scalar or vector)<br />
Pick any v as analytical solution<br />
v solves the problem<br />
F(u) = b in Ω,<br />
b = F(v)<br />
e.g. u = v on ∂Ω<br />
i.e., add a source term in the equation such that v is a<br />
solution<br />
Known as the method of manufactured solutions<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 3
<strong>Ver</strong>ification Procedure<br />
Make a sequence of refinements in space and time<br />
Compute errors<br />
Fit a convergence estimate<br />
error = A∆x p + B∆y q + C∆z r + D∆t s<br />
(by nonlinear least squares)<br />
Does the method converge? With expected rate?<br />
This is easy, but not a standard or required scientific<br />
procedure in Computational Science<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 4
Computing F(v) (Source Term)<br />
Computing the source term F(v) is a matter of<br />
differentiation<br />
Calculating F(v) by hand is tedious and error-prone<br />
This is critical for coupled systems of <strong>PDE</strong>s<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 5
Example<br />
∇ · [µ∇w] = −β<br />
∇ 2 T = −κ −1 µ˙γ 2<br />
µ = e −α(T−T 0) ˙γ n−1<br />
√<br />
˙γ = (w ,x ) 2 + (w ,y ) 2<br />
w = cos(sin(x + y)) + tanx<br />
T = x + y + e xy<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 6
½ ÜÔ´Ýܾܵ·Ý¾ÜÔ´Ýܵ·´½¼º¼µ´´½¹×Ò´×ҴݷܵµÓ״ݷܵ·ØҴܵ¾µ¾·<br />
´¹×Ò´×ҴݷܵµÓ״ݷܵµ¾µ´¼º½·¿´´½¹×Ò´×ҴݷܵµÓ״ݷܵ·ØҴܵ¾µ¾·<br />
´¹×Ò´×ҴݷܵµÓ״ݷܵµ¾µ´¹¼º¾µµÜÔ´¼º¼¹¼ºÝ¹¼ºÜÔ´Ýܵ¹¼ºÜµ<br />
¾ ¼º½¹Ó×´×ҴݷܵµÓ״ݷܵ¾´¼º½·¿´´½¹×Ò´×ҴݷܵµÓ״ݷܵ· ØҴܵ¾µ¾·´¹×Ò´×ҴݷܵµÓ״ݷܵµ¾µ´¹¼º¾µµÜÔ´¼º¼¹¼ºÝ¹<br />
¼ºÜÔ´Ýܵ¹¼ºÜµ¹´¼º¾µ´´½¹×Ò´×ҴݷܵµÓ״ݷܵ·ØҴܵ¾µ¾·<br />
´¹×Ò´×ҴݷܵµÓ״ݷܵµ¾µ´¹½º¾µ´¹´¾º¼µ×Ò´×ҴݷܵµÓ״ݷܵ<br />
´¹Ó×´×ҴݷܵµÓ״ݷܵ¾·×Ò´×Ҵݷܵµ×Ҵݷܵµ·´¾º¼µ´½¹×Ò´×Ҵݷܵµ<br />
Ó״ݷܵ·ØҴܵ¾µ´´¾º¼µØҴܵ´½·ØҴܵ¾µ¹Ó×´×ҴݷܵµÓ״ݷܵ¾·<br />
×Ò´×Ҵݷܵµ×Ҵݷܵµµ´½¹×Ò´×ҴݷܵµÓ״ݷܵ·ØҴܵ¾µÜÔ´¼º¼¹¼ºÝ¹<br />
¼ºÜÔ´Ýܵ¹¼ºÜµ·´¼º¾µ´´½¹×Ò´×ҴݷܵµÓ״ݷܵ·ØҴܵ¾µ¾·<br />
´¹×Ò´×ҴݷܵµÓ״ݷܵµ¾µ´¹½º¾µ×Ò´×ҴݷܵµÓ״ݷܵ´´¾º¼µ<br />
´½¹×Ò´×ҴݷܵµÓ״ݷܵ·ØҴܵ¾µ´¹Ó×´×ҴݷܵµÓ״ݷܵ¾· ×Ò´×Ҵݷܵµ×Ҵݷܵµ¹¾×Ò´×ҴݷܵµÓ״ݷܵ´¹Ó×´×ҴݷܵµÓ״ݷܵ¾·<br />
×Ò´×Ҵݷܵµ×ҴݷܵµµÜÔ´¼º¼¹¼ºÝ¹¼ºÜÔ´Ýܵ¹¼ºÜµ¹ ´¹¼º¹¼ºÜÔ´Ýܵܵ×Ò´×ҴݷܵµÓ״ݷܵ´¼º½·¿´´½¹×Ò´×Ҵݷܵµ<br />
Ó״ݷܵ·ØҴܵ¾µ¾·´¹×Ò´×ҴݷܵµÓ״ݷܵµ¾µ´¹¼º¾µµ ÜÔ´¼º¼¹¼ºÝ¹¼ºÜÔ´Ýܵ¹¼ºÜµ·´¹¼º¹¼ºÝÜÔ´Ýܵµ´½¹×Ò´×Ҵݷܵµ<br />
Example cont.<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 7
Automation Idea 1<br />
Use Maple (or similar) to define F(u) and v. Then<br />
compute F(v) and generate C/F77 code<br />
Incorporate this C/F77 code in the simulator.<br />
Easy in principle, but still somewhat tedious;<br />
requires some manual work<br />
The process must be simpler and safer if we want to make<br />
extensive use of manufactured solutions!<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 8
Automation Idea 2<br />
Build a fully automatic “problem solving environment”<br />
for this type of code verification<br />
Need to glue symbolic package, source term F(v) code<br />
generation, and <strong>PDE</strong> solver<br />
Use a “scripting language” for gluing!<br />
– we have chosen Python<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 9
Our Approach<br />
GiNaC: symbolic math engine (in C++)<br />
Generate Python interface to GiNaC (using SWIG)<br />
Extend Python-GiNaC with (e.g.)ÖandÚ<br />
Diffpack: <strong>PDE</strong> solver library (in C++)<br />
Generate Python interface to Diffpack <strong>PDE</strong> solver<br />
(using SWIG/SIP)<br />
Famms: home-made Python module to glue GiNaC<br />
and solver; specify F(u) and v in Python, the rest is<br />
automatic<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 10
Example<br />
ÐÐ×ØØݽŴµ ÖÓÑÐ×ØØݽÅÑÔÓÖØ ÔÈ×ÓÐÚÖ<br />
∇ ((λ + µ)∇ · u) + ∇<br />
È×ÓÐÚÖ×··»ÈÝØÓÒÓØ<br />
· (µ∇u) = 0<br />
ÖÓÑÑÑ×ÑÔÓÖØ ÖÓÑ×ÝÑÓÐÑÔÓÖØ<br />
Ü¸ÝºÜ ÑÑ×´Ò×ÔѾµ Ú½×ҴܵھÓ״ݵ ÚÎØÓÖ´´Ü¸Ýµ¸´Ú½¸Ú¾µµ Ð××ÓÖÒÔÒÒØÚÖÐ× ÔÙÒØÓÒ×ÓÖÓÑÔÓÒÒØ<br />
Äѽ¾¼ÑÙ¿ Ð×ØØÝÔÖÑØÖ× ×ÔÝÑÒÙØÙÖ×ÓÐÙØÓÒ<br />
´Ùµ<br />
º××Ò´ÕÙØÓÒ¸×ÓÐÙØÓÒÚ¸×ÑÙÐØÓÖе ÖØÙÖÒÖ´´ÄÑ·ÑÙµÚ´Ùµµ·Ú´ÑÙÖ´Ùµµ<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 11
Example cont.<br />
××ÒÒÔÙØØ ÑÅÒÙËÝ×ØÑ´µ<br />
∇ ((λ + µ)∇ · u) + ∇<br />
ÑÒÙÒØÖØÓÈ×ÓÐÚÖ<br />
· (µ∇u) = 0<br />
Ѻ×Ø´ÑÙÐØÐÚÐÑØÓ¸ÅÙÐØÖµ Ѻ×Ø´×ÑÓÓØÖ×ÑØÓ¸ËËÇʵ кҴѵ ÒØ×ØÖÙØ×ÓÖÒÔÙØ<br />
Ѻ×Ø´ÓÖ×Ö×ÑØÓ¸Ù××Ðѵ Ѻ×Ø´ÄѸÄѵѺ×Ø´ÑÙ¸ÑÙµ ººº<br />
к×ÓÐÚÈÖÓÐÑ´µ к×Ò´µ ×ÒÒÔÙØØÓ×ÓÐÚÖ<br />
к×ÚÊ×ÙÐØ×´µ<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 12
Another Example<br />
∇ · [µ∇w] = −β<br />
∇ 2 T = −κ −1 µ˙γ 2<br />
µ = e −α(T−T 0) ˙γ n−1<br />
ÛÓ×´×Ҵܷݵµ·ØҴܵ<br />
√<br />
ÌÜ·Ý·ÜÔ´Üݵ<br />
˙γ = (w ,x ) 2 + (w ,y ) 2<br />
ÑÑ´Ûµ ÑÝ̴̵ ÑÝÛ´Ñѵ ÖØÙÖÒÜÔ´¹ÐԴ̹̼µµ ÖØÙÖÒÑÑ´Ò¹½µ ÖØÙÖÒ×ÕÖØ´Ûº´Ü¸½µ¾·Ûº´Ý¸½µ¾µ<br />
½´Û¸ÌµÖØÙÖÒÐÔд̵·ÔÔ´¹½µÑݴ̸ÑÑ´ÛµµÑÑ´Ûµ¾<br />
Ñݴ̸Ñѵ ÖØÙÖÒÑÝ̴̵ÑÝÛ´Ñѵ<br />
¾´Û¸ÌµÖØÙÖÒÚ´Ñݴ̸ÑÑ´ÛµµÖ´Ûµµ·Ø<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 13
What Have We Done?<br />
We have<br />
“created” an engine for symbolic <strong>PDE</strong> math, with nice<br />
input syntax<br />
made a seamless integration of our native <strong>PDE</strong> solver<br />
and this math engine such that the <strong>PDE</strong> solver solves a<br />
new symbolically computed problem – no solver<br />
modification required<br />
incorporated empirical convergence estimation<br />
incorporated visualization (Vtk/MayaVi)<br />
...and this works for any Diffpack <strong>PDE</strong> solver<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 14
Beyond Diffpack<br />
How much of this is actually tied to Diffpack?<br />
Famms is meant to be independent of the <strong>PDE</strong> package<br />
Key mechanism: determine functions run–time<br />
Functors (function objects) in C++<br />
Function pointers in plain C<br />
Should be “easy” to incorporate in other software<br />
packages; we only need Python callback mechanisms<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 15
The Glue<br />
Ú×Ò´Üݵ ܸݸÚÆÓØ×<br />
Easy to evaluate v pointwise in GiNaC<br />
Ð××ÅÝÊÀËÔÙÐ×Ð××ÙÒØÓÖ<br />
v and F(v) are expressions with GiNaC objects<br />
Bind<br />
ßÔÙÐ<br />
generic Python pointers in the <strong>PDE</strong> solver to<br />
Python functions for evaluating v and source term F(v)<br />
ÈÝÇØ ÔÝÙÒ »»ÈÝØÓÒÙÒØÓÒØÓÐÐ<br />
ÓÙÐÓÔÖØÓÖ´µ´ÓÒ×Ø×ÔÔÓÒزܸØÑص »»ÙÐÖ×ÖÓÑÜÒØ ÈÝÇØ Ö× »»ÖÙÑÒØ×ØÓÔÝÙÒ ß<br />
ÖØÙÖÒÓÙÐÖ×ÙÐØ ÓÙÐÖ×ÙÐØÈÝÐÓØ×ÓÙдÔÝÖ×ÙÐص ÔÝÖ×ÙÐØÈÝÚÐÐÐÇØ´ÔÝÙÒ¸Ö×µ<br />
Ð<br />
<strong>PDE</strong> <strong>Code</strong> <strong>Ver</strong>ification 16