Editing Fem-fenics
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 2: | Line 2: | ||
== Introduction == | == Introduction == | ||
Fem-Fenics Octave is a package for solving partial differential equations. Obviously, Fem-fenics is not the only extra package for Octave with this purpose. For example, [[Bim_package]] uses finite volumes to solve diffusion-advection-reaction equations, while secs1d/2d/3d [http://octave.sourceforge.net/secs1d/index.html] are suited for the resolution of the drift-diffusion system. Furthermore, to use profitably the software, you can integrate it with msh [http://octave.sourceforge.net/msh/index.html] for the generation of the mesh and with fpl [http://octave.sourceforge.net/fpl/index.html] for the post-processing of data. The objective of Fem-fenics is to be a generic library of finite elements, thereby allowing the user to resolve any type of pde, choosing also the most appropriate Finite Element space for any specific problem. | |||
As the name suggests, the Fem-fenics pkg is a wrapper for FEniCS [http://fenicsproject.org/] functions and classes. Thus, ideally the Fem-fenics final goal would be to be able to reproduce all the features available in FEniCS, | As the name suggests, the Fem-fenics pkg is a wrapper for FEniCS [http://fenicsproject.org/] functions and classes. Thus, ideally the Fem-fenics final goal would be to be able to reproduce all the features available in FEniCS, simplifying them where it is possible or using the Octave function whenever available (like the "\" for the resolution of a linear system or the odepkg [http://octave.sourceforge.net/odepkg/index.html] for the resolution of a time dependent problem). | ||
== Tutorials == | == Tutorials == | ||
A generic problem has to be solved in two steps: | |||
* | * a file where the '''abstract problem''' is described: this file has to be written in Unified Form Language (UFL), which is ''a domain specific language for defining discrete variational forms and functionals in a notation close to pen-and-paper formulation.'' UFL is easy to learn, and in any case the User manual provides explanations and examples. [http://fenicsproject.org/documentation/ufl/1.2.0/user/user_manual.html#ufl-user-manual] | ||
* a script file where the abstract problem is imported and a '''specific problem''' is implemented and solved: this is the script file where the fem-fenics functions are needed. Their sintax is as close as possible to the python interface, so that Fenics users are comfortable with that, but it is alos quite intuitive for beginners. The examples below show the equivalence between the different programming languages. | |||
Line 25: | Line 25: | ||
A complete description of the problem is avilable on the [http://fenicsproject.org/documentation/dolfin/1.2.0/python/demo/pde/poisson/python/documentation.html Fenics website.] | A complete description of the problem is avilable on the [http://fenicsproject.org/documentation/dolfin/1.2.0/python/demo/pde/poisson/python/documentation.html Fenics website.] | ||
<div style="width: 100%;"> | <div style="width: 100%;"> | ||
<div style="float:left; width: 48%"> | <div style="float:left; width: 48%"> | ||
{{Code|Define Poisson problem with fem-fenics|<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code|Define Poisson problem with fem-fenics|<syntaxhighlight lang="octave" style="font-size:13px"> | ||
pkg load fem-fenics msh | pkg load fem-fenics msh | ||
import_ufl_Problem ('Poisson') | |||
# Create mesh and define function space | # Create mesh and define function space | ||
Line 50: | Line 35: | ||
mesh = Mesh(msh2m_structured_mesh (x, y, 1, 1:4)); | mesh = Mesh(msh2m_structured_mesh (x, y, 1, 1:4)); | ||
# File Poisson.ufl | |||
# element = FiniteElement("Lagrange", triangle, 1) | |||
V = FunctionSpace('Poisson', mesh); | V = FunctionSpace('Poisson', mesh); | ||
Line 61: | Line 48: | ||
# Define variational problem | # Define variational problem | ||
# | # File Poisson.ufl | ||
# u = TrialFunction(element) | # u = TrialFunction(element) | ||
# v = TestFunction(element) | # v = TestFunction(element) | ||
Line 71: | Line 58: | ||
g = Expression ('g', @(x,y) sin (5.0 * x)); | g = Expression ('g', @(x,y) sin (5.0 * x)); | ||
# | # File Poisson.ufl | ||
# a = inner(grad(u), grad(v))*dx | # a = inner(grad(u), grad(v))*dx | ||
# L = f*v*dx + g*v*ds | # L = f*v*dx + g*v*ds | ||
a = BilinearForm ('Poisson' | a = BilinearForm ('Poisson', V); | ||
L = LinearForm ('Poisson', V, f, g); | L = LinearForm ('Poisson', V, f, g); | ||
Line 99: | Line 86: | ||
# Create mesh and define function space | |||
mesh = UnitSquareMesh(32, 32) | |||
V = FunctionSpace(mesh, "Lagrange", 1) | V = FunctionSpace(mesh, "Lagrange", 1) | ||
Line 162: | Line 140: | ||
=== Mixed Formulation for Poisson Equation === | === Mixed Formulation for Poisson Equation === | ||
In this example the | In this example the poisson equation is solved with a mixed approach: the stable FE space obtained using Brezzi-Douglas-Marini polynomial of order 1 and Dicontinuos element of order 0 is used. | ||
<math>-\mathrm{div}\ ( \mathbf{\sigma} (x, y) ) ) = f (x, y) \qquad \mbox{ in } \Omega</math> | <math>-\mathrm{div}\ ( \mathbf{\sigma} (x, y) ) ) = f (x, y) \qquad \mbox{ in } \Omega</math> | ||
Line 172: | Line 150: | ||
<math>(\sigma (x, y) ) \cdot \mathbf{n} = \sin (5x) \qquad \mbox{ on } \Gamma_N</math> | <math>(\sigma (x, y) ) \cdot \mathbf{n} = \sin (5x) \qquad \mbox{ on } \Gamma_N</math> | ||
A complete description of the problem is | A complete description of the problem is avilable on the [http://fenicsproject.org/documentation/dolfin/1.2.0/python/demo/pde/mixed-poisson/python/documentation.html Fenics website.] | ||
<div style="width: 100%;"> | <div style="width: 100%;"> | ||
<div style="float:left; width: 48%"> | <div style="float:left; width: 48%"> | ||
{{Code|Define MixedPoisson problem with fem-fenics|<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code|Define MixedPoisson problem with fem-fenics|<syntaxhighlight lang="octave" style="font-size:13px"> | ||
pkg load fem-fenics msh | pkg load fem-fenics msh | ||
import_ufl_Problem ('MixedPoisson') | |||
# Create mesh | # Create mesh | ||
Line 198: | Line 161: | ||
mesh = Mesh(msh2m_structured_mesh (x, y, 1, 1:4)); | mesh = Mesh(msh2m_structured_mesh (x, y, 1, 1:4)); | ||
# ufl | # File MixedPoisson.ufl | ||
# BDM = FiniteElement("BDM", triangle, 1) | # BDM = FiniteElement("BDM", triangle, 1) | ||
# DG = FiniteElement("DG", triangle, 0) | # DG = FiniteElement("DG", triangle, 0) | ||
Line 205: | Line 168: | ||
# Define trial and test function | # Define trial and test function | ||
# ufl | # File MixedPoisson.ufl | ||
# (sigma, u) = TrialFunctions(W) | # (sigma, u) = TrialFunctions(W) | ||
# (tau, v) = TestFunctions(W) | # (tau, v) = TestFunctions(W) | ||
Line 214: | Line 177: | ||
# Define variational form | # Define variational form | ||
# ufl | # File MixedPoisson.ufl | ||
# a = (dot(sigma, tau) + div(tau)*u + div(sigma)*v)*dx | # a = (dot(sigma, tau) + div(tau)*u + div(sigma)*v)*dx | ||
# L = - f*v*dx | # L = - f*v*dx | ||
a = BilinearForm ('MixedPoisson' | a = BilinearForm ('MixedPoisson', V); | ||
L = LinearForm ('MixedPoisson', V, f); | L = LinearForm ('MixedPoisson', V, f); | ||
Line 260: | Line 223: | ||
{{Code|Define MixedPoisson problem with fenics python|<syntaxhighlight lang="python" style="font-size:13px"> | {{Code|Define MixedPoisson problem with fenics python|<syntaxhighlight lang="python" style="font-size:13px"> | ||
from dolfin import * | from dolfin import * | ||
# Create mesh | # Create mesh | ||
mesh = UnitSquareMesh(32, 32) | mesh = UnitSquareMesh(32, 32) | ||
Line 291: | Line 238: | ||
(sigma, u) = TrialFunctions(W) | (sigma, u) = TrialFunctions(W) | ||
(tau, v) = TestFunctions(W) | (tau, v) = TestFunctions(W) | ||
Line 344: | Line 292: | ||
=== Hyperelasticity === | === Hyperelasticity === | ||
This time we compare the code with the | This time we compare the code with the c++ version of DOLFIN. | ||
A complete description of the problem is avilable on the [http://fenicsproject.org/documentation/dolfin/1.2.0/cpp/demo/pde/hyperelasticity/cpp/documentation.html Fenics website.] | |||
The final solution will look like this | |||
[[File:HyperElasticity.png]] | |||
[[File:HyperElasticity.png | |||
{{Code|HyperElasticity Problem: the ufl file|<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code|HyperElasticity Problem: the ufl file|<syntaxhighlight lang="octave" style="font-size:13px"> | ||
Line 402: | Line 345: | ||
{{Code|Define HyperElasticity problem with fem-fenics|<syntaxhighlight lang="octave" style="font-size:13px"> | {{Code|Define HyperElasticity problem with fem-fenics|<syntaxhighlight lang="octave" style="font-size:13px"> | ||
pkg load fem-fenics msh | pkg load fem-fenics msh | ||
problem = 'HyperElasticity'; | |||
import_ufl_Problem (problem); | |||
Line 586: | Line 490: | ||
using namespace dolfin; | using namespace dolfin; | ||
// Sub domain for clamp at left end | // Sub domain for clamp at left end | ||
Line 734: | Line 599: | ||
// Solve nonlinear variational problem F(u; v) = 0 | // Solve nonlinear variational problem F(u; v) = 0 | ||
solve(F == 0, u, bcs, J); | solve(F == 0, u, bcs, J); | ||
Line 765: | Line 629: | ||
</div> | </div> | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
=== Incompressible Navier-Stokes equations === | === Incompressible Navier-Stokes equations === | ||
A complete description of the problem is avilable on the [http://fenicsproject.org/documentation/dolfin/1.2.0/python/demo/pde/navier-stokes/python/documentation.html Fenics website.] | |||
A complete description of the | |||
<div style="width: 100%;"> | <div style="width: 100%;"> | ||
Line 846: | Line 710: | ||
# eq = (1/k)*inner(u - u0, v)*dx + inner(grad(u0)*u0, v)*dx \ | # eq = (1/k)*inner(u - u0, v)*dx + inner(grad(u0)*u0, v)*dx \ | ||
# + nu*inner(grad(u), grad(v))*dx - inner(f, v)*dx | # + nu*inner(grad(u), grad(v))*dx - inner(f, v)*dx | ||
a1 = BilinearForm ('TentativeVelocity' | a1 = BilinearForm ('TentativeVelocity', V, k); | ||
Line 852: | Line 716: | ||
# a = inner(grad(p), grad(q))*dx | # a = inner(grad(p), grad(q))*dx | ||
# L = -(1/k)*div(u1)*q*dx | # L = -(1/k)*div(u1)*q*dx | ||
a2 = BilinearForm ('PressureUpdate' | a2 = BilinearForm ('PressureUpdate', Q); | ||
# Velocity update | # Velocity update | ||
# a = inner(u, v)*dx | # a = inner(u, v)*dx | ||
# L = inner(u1, v)*dx - k*inner(grad(p1), v)*dx | # L = inner(u1, v)*dx - k*inner(grad(p1), v)*dx | ||
a3 = BilinearForm ('VelocityUpdate' | a3 = BilinearForm ('VelocityUpdate', V); | ||
# Assemble matrices | # Assemble matrices | ||
Line 1,065: | Line 929: | ||
<div style="clear:both"></div> | <div style="clear:both"></div> | ||
[[Category: | [[Category:OctaveForge]] | ||
[[Category:Packages]] |