This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

UFL file independet of Space Dimension

0 votes

Hi,
my goal is to solve the stationary Navier-Stokes Equations.
Therefore i have written an ufl file and described the Weak Formulation:

#kind of element
cell = tetrahedron

# Function Space for Velocity U and Pressure V 
U = VectorElement("CG", cell, 2)
V = FiniteElement("CG", cell, 1)

# Mixed Function Space 
W = U*V

# (initial guess) Function for solution
w = Coefficient(W)

# (right hand side) Function
f = Coefficient(U);

# split into parts, w_u is desired solution
(w_u,w_p) = (as_vector((w[0],w[1],w[2])),w[3])

# TestFunction
(psi_u,psi_p) = TestFunctions(W)

# user defined viscosity
# NU = Constant(cell)
# Weak Formulation
# nonlinear termin (+) 
# laplace (+)
# pressure (+)  
# solenoidal (+) 
# rhs
 F =  inner(psi_u,dot(grad(w_u),w_u))*dx + \
     inner(grad(psi_u),grad(w_u))*dx    + \
     -1*div(psi_u)*w_p*dx               + \
     -1*psi_p*div(w_u)*dx               + \
     dot(psi_u,f)*dx                          

 # Derivative
 dw = TrialFunction(W)
 J = derivative(F, w, dw)

This Formulation is for the 3 dimensional case.
Is it possible to "generalize" the code, that it is also working for 2 dimensional case?
The main problems are:

 cell = tetrahedron
 (w_u,w_p) = (as_vector((w[0],w[1],w[2])),w[3])

Thanks in advance

asked Mar 31, 2016 by maxb FEniCS Novice (170 points)
edited Mar 31, 2016 by maxb

Hi, the preferred way is to generate separate ufl files for each dimension and then delegate to function from appropriate header based on user-given value, see here and here. If you are only concerned with UFL consider (remember UFL is implemented in Python)

gdim = 2
cell = tetrahedron if gdim == 3 else triangle
# ... same as before
(w_u,w_p) = (as_vector(tuple([w[i] for i in range(gdim)])), w[gdim])
# ... same as before

Ah ok thats a nice workaround/simplification. Maybe i can easily write a code generator,
which generates the desired ufl files for different dimensions and FE Spaces, thanks.

...