Toshi
June 11, 2022, 7:26pm
1
Hi all,
I have a question regarding the implementaiton of non-linear coefficient such as:
def q(u, alpha):
return 1 + alpha*u**2
I want to evaluate q in integrals using the values of u at the nodes. To achieve that (Treatment of non-linear coefficient - #2 by kamensky ), we can use Expression:
expr = dl.Expression(“alpha * u * u”,u=u, alpha = alpha, degree=1)
My question is how can we write the expression using the UserExpression. My code below does not work (i.e., computation never finishes somehow):
import dolfin as dl
import matplotlib.pyplot as plt
N = 100
X = 1.0
mesh = dl.IntervalMesh(N, 0, X)
degree = 1 # linear basis functions
Vh = dl.FunctionSpace(mesh, “Lagrange”, degree)
u = dl.interpolate(dl.Expression(‘x[0]’, degree = 1), Vh)
alpha = 2.0
class user_expr(dl.UserExpression):
def set_alpha(self, u, alpha): # in the user_expr class
self.u = u
self.alpha = alpha
def eval(self, value, x): # in the user_expr class
“set value[0] to value at point x”
value[0] = self.alpha * self.u* self.u
user_expr2 = user_expr(degree = 0)
user_expr2.set_alpha(u, alpha)
user_expr2.compute_vertex_values(mesh) # not working
I appreciate any help!
dokken
June 12, 2022, 1:14pm
2
In not sure what you mean by this. Do you want the quadrature rule for your integral to be the set of vertices in your mesh? Why would you want that?
Such a quadrature rule would underintegrate the polynomial.
Toshi
June 13, 2022, 5:53am
3
Thank you for your comment. The reason is I wanted to test the method used in existing papers.
dokken
June 13, 2022, 7:56am
4
Consider the following:
import dolfin as dl
N = 100
X = 1.0
mesh = dl.IntervalMesh(N, 0, X)
degree = 1 # linear basis functions
Vh = dl.FunctionSpace(mesh, "Lagrange", degree)
u = dl.interpolate(dl.Expression("x[0]", degree = 1), Vh)
alpha = 2.0
class user_expr(dl.UserExpression):
def set_alpha(self, u, alpha): # in the user_expr class
self.u = u
self.alpha = alpha
def eval(self, value, x): # in the user_expr class
"set value[0] to value at point x"
value[0] = self.alpha * self.u(x)* self.u(x)
user_expr2 = user_expr(degree = 0)
user_expr2.set_alpha(u, alpha)
q = dl.interpolate(user_expr2, dl.FunctionSpace(mesh, "Lagrange", 1))
d_to_v = dl.dof_to_vertex_map(q.function_space())
print(q.vector()[:][d_to_v])
Toshi
June 13, 2022, 10:17pm
5
Thank you! I worked very well. I appreciate your help.