# Meaning of this minimal python and FEniCS based wave propagation code? [closed]

This is a question about understanding a piece of random code that does not necessarily require knowledge of it's theory. This is very specific and may not be of use to the community in general but some may find it interesting.

I have shown a snippet of code below. In this python code, created with FEniCS, I fail to understand a couple of lines which I request some expert to shed light on.

Question 1. I believe the expression $Ut$ and $Vt$ are implying that the input wave should carry on from time $t = 0$ to time $tl = 0.5$, but I am still not sure what is the meaning of '$?$' symbol and '$:1.0$' or '$:0.0$'.

Question 2. The use of derivative (x,y,z) in FEniCS implies the derivative of $x$ with respect to $y$, in the direction of $z$. Replace() and Action() are both defined in form language of FEniCS. What I don't understand theoretically and programming wise are the following two lines.

M, F_tot = lhs(replace(Dv_kinetic_energy, {v: du})), - Du_elastic_energy
M_lumped = action(M, Constant(1.0))  # Mass lumping


In the code, $v$ is the velocity, $a$ is the acceleration, $u$ is current displacement. $lhs$ in FEniCS is just the extraction of bilinear terms from the main equation. Ctrl+f $lhs$ on the below link:

The code snippet: (nothing else that was defined in the full code is required to solve this question)

from __future__ import division
from fenics import *
import numpy as np

# Function space and functions
V = FunctionSpace(mesh, "CG", 1)
u, u_pred, v, a = Function(V), Function(V), Function(V), Function(V)
du, w = TrialFunction(V), TestFunction(V)

tl = 0.5

Ut = Expression("t <= tl ? (1-cos(pi*t/tl))/2 : 1.0", t=0.0, tl=tl)
Vt = Expression("t <= tl ? pi/(2*tl)*sin(pi*t/tl) : 0.0", t=0.0, tl=tl)

bc_u = [DirichletBC(V, Constant(0.0), left), DirichletBC(V, Ut, right)]
bc_v = [DirichletBC(V, Constant(0.0), left), DirichletBC(V, Vt, right)]

# Energies
kinetic_energy = 0.5*inner(v, v)*dx

# Define the a-problem (solve for the acceleration M*a = F_ext - F_int = F_ext - K*u)
Du_elastic_energy = derivative(elastic_energy, u, w)
Dv_kinetic_energy = derivative(kinetic_energy, v, w)
M, F_tot = lhs(replace(Dv_kinetic_energy, {v: du})), - Du_elastic_energy
M_lumped = action(M, Constant(1.0))  # Mass lumping
M_lumped_ = PETScVector()
F_tot_ = PETScVector()
assemble(M_lumped, tensor=M_lumped_)
def solve_a():
assemble(F_tot, tensor=F_tot_)
a.vector().set_local(F_tot_.array() / M_lumped_.array())

• FEniCS-specific question have been moved to the Fenics Q&A. – Christian Clason Jul 8 '16 at 5:56
• I apologise, I won't post any more fenics based questions here again. – CRG Jul 8 '16 at 6:46
• No need to apologize; it just turned out to be better to not divide software-specific questions between this StackExchange and the official support channels (where the developer are guaranteed to see it). – Christian Clason Jul 8 '16 at 6:54

Question 1. I believe the expression Ut and Vt are implying that the input wave should carry on from time t=0 to time tl=0.5, but I am still not sure what is the meaning of '?' symbol and ':1.0:1.0' or ':0.0:0.0'.

Ans. The Expression function is defined here. Expression is passed a string which is a C++ statement. So the ?: is clearly ternary operator or more commonly known as inline-if. So, here the Vt and Ut are being evaluated by substituting the values of t and tl in the expression inside Expression().

Question 2. The use of derivative(x,y,z) in FEniCS implies the derivative of x with respect to y, in the direction of z. Replace() and action() are defined in form language of FEniCS. What I don't understand theoretically and programming wise are the following two lines.

M, F_tot = lhs(replace(Dv_kinetic_energy, {v: du})), - Du_elastic_energy
M_lumped = action(M, Constant(1.0))  # Mass lumping


Ans. I couldn't find the documentation for replace() in python. But I can guess whats happening in Line 1. {v: du} is a Python Dictionary -https://docs.python.org/2/tutorial/datastructures.html#dictionaries. So probably the variable v is being replaced with du in Dv_kinetic_energy. I don't know what it means theoretically

Not sure about Line 2 either.

• Based on the comments, the 'action' line is just formed the diagonal values of a lumped mass matrix by applying M to a constant vector. – origimbo Jul 8 '16 at 10:19