Hello, I am trying to simulate a 2D wave equation on a sample square mesh but when I assemble the lhs, rhs or use the solve command I get syntax error:
Blockquote
File “”, line 11
solve(A==K, u_s, solver_parameters=dict(linear_solver=‘gmres’, preconditioner=‘ilu’))
^
SyntaxError: invalid syntax
I have 2 trial function (u_1 is the pressure and u_2 is the velocity vector field). I have 2 equations (first discretized with implicit euler and the second with explicit euler), which I summed them up. This is what I am trying to implement in FEniCS:
I tried different implementations but also assigning lefthand side and righthand side return me the same error of syntax.
F = v1*u1*dx - v1*u_n.split()[0]*dx + dot(v2, u2)*dx - dot(v2, u_n.split()[1]*dx -dt*dot(grad(v1), u2)*dx +dt*dot(v2, grad(u_n.split()[0]))*dx + dt*v1*dot(u_b.split()[1], n)*ds
a, L = lhs(F), rhs(F)
solve(a == L, u_s, bc)
File “”, line 15
a, L = lhs(F), rhs(F)
^
SyntaxError: invalid syntax
Here is my code:
from fenics import *
import numpy as np
# Constants
dt = 0.0005
FT = 0.39 # final time
t = 0
# Mesh
N = 20
M = 20
mesh = RectangleMesh(Point(0,0), Point(1,1), N, M, "left")
n = FacetNormal(mesh)
# Finite element spaces
P1 = FiniteElement("P-", triangle, 1, 0)
RT1 = FiniteElement("RTE", triangle, 1)
Lmix = FunctionSpace(mesh, P1*RT1)
# Trial functions
u_1, u_2 = TrialFunctions(Lmix)
# Test functions
v_1, v_2 = TestFunctions(Lmix)
# Functions - previous step - boundary inputs
u_n = Function(Lmix) # previous step (respect to n+1)
u_b = Function(Lmix) # boundary inputs
u_s = Function(Lmix) # solution (to be updated at each time step in the iteration loop)
# Forms
A = v_1*u_1*dx + dot(v_2, u_2)*dx - dt*dot(grad(v_1), u_2)*dx
# Compute the solution at each time step
while(t < 0.05):
# Update boundary conditions
g = Expression(("0", "sin(pi*t)*near(x[0],0)*(t<1)", "sin(pi*t)*near(x[1],1)*(t<1)"), degree = 2,t = t)
u_b = project(g, Lmix)
K = v_1*u_n.split()[0]*dx + dot(v_2, u_n.split()[1]*dx - dt*dot(v_2, grad(u_n.split()[0]))*dx - dt*v_1*dot(u_b.split()[1], n)*ds
#Solve linear system
solve(A==K, u_s, solver_parameters=dict(linear_solver='gmres', preconditioner='ilu'))
# Assign solution to previous step vector
u_n.assign(u_s)
print(t)
# Plot u_1
c = plot(u_s.split()[0])
plt.colorbar(c)
plt.show()
# Update time
t += dt
Sorry for my bad explanation, it ismy first post and I am beginning to learn how to use the forum. If you need more information, please ask and I will give more details.
Any help or suggestion will be very appreciated.
Thank you very much!
Niccolò