Pass variables to the expression for the initial condition

I’m new to Fenics and I’m trying to solve the 1D Burgers equation using this script

I’m having problems passing variables to the expression for the initial condition. For instance, if I try to set the initial condition u(x,0) = np.pi * x as follows,

#  Define the initial condition.
#
  # u_init = Expression ( "x[0]", degree = 1 ) # <- the original IC, which works
  PI = np.pi  
  u_init = Expression ( "PI * x[0])", degree = 1 )  # <- my IC, which doesn't work

then the code doesn’t work and I get this error message:

------------------- Start compiler output ------------------------
/tmp/tmpi4_ubx8m/dolfin_expression_e1b233f340eedca27ef2c4df85830884.cpp: In member function ‘virtual void dolfin::dolfin_expression_e1b233f340eedca27ef2c4df85830884::eval(Eigen::Ref<Eigen::Matrix<double, -1, 1> >, Eigen::Ref<const Eigen::Matrix<double, -1, 1> >) const’:
/tmp/tmpi4_ubx8m/dolfin_expression_e1b233f340eedca27ef2c4df85830884.cpp:61:23: error: ‘PI’ was not declared in this scope; did you mean ‘pi’?
   61 |           values[0] = PI * x[0]);
      |                       ^~
      |                       pi

And I have the same problem if I try to give the viscosity, nu, as an argument.

Why does this happen? How could it be fixed?

Should be:

u_init = Expression ( "PI * x[0])", PI=PI, degree = 1 ) 
2 Likes

Ok, thank you very much, this way it works perfectly!