Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 6a8b8afe authored by GACON Florian's avatar GACON Florian
Browse files

Test RK4

parent 2af22ebe
No related branches found
No related tags found
No related merge requests found
from fenics import * from fenics import *
import ufl
import time import time
T = 2.0 # final time # T = 0.01 # final time
num_steps = 50 # number of time steps # number of time steps
dt = T / num_steps # time step size # dt = T / num_steps # time step size
# Create mesh and define function space # Create mesh and define function space
nx = ny = 30 nx = ny = 100
mesh = RectangleMesh(Point(-2, -2), Point(2, 2), nx, ny) mesh = RectangleMesh(Point(-2, -2), Point(2, 2), nx, ny)
V = FunctionSpace(mesh, 'P', 1) V = FunctionSpace(mesh, 'P', 1)
...@@ -26,30 +27,100 @@ u = TrialFunction(V) ...@@ -26,30 +27,100 @@ u = TrialFunction(V)
v = TestFunction(V) v = TestFunction(V)
f = Constant(0) f = Constant(0)
def func(t, state): # Create VTK file for saving solutio
F = dot(grad(state), grad(v))*dx - f*v*dx # Time-stepping
return F
# Create VTK file for saving solution sol = Function(V)
vtkfile = File('heat_gaussian/solution.pvd') sol.assign(u_n)
k1 = Function(V)
k2 = Function(V)
k3 = Function(V)
k4 = Function(V)
# Time-stepping F = (dot(grad(sol), grad(v)))*dx
#Midpoint method
# sol.assign(u_n)
# scheme = BackwardEuler(F, sol, t=fe.Constant(0), bcs=[bc])
# solver = RKSolver(scheme)
# solver.step_interval(0,2,0.04)
# with XDMFFile('solution.xdmf') as f:
# f.write(scheme.solution())
t = 0 t = 0
form = (u-u_n)/dt*dx - func(t+0.5*dt, u_n + dt*func(t, u_n)) vtkfile = XDMFFile('solution.xdmf')
a, L = lhs(F), rhs(F) # vtkfile.write_checkpoint(sol, "heat", t)
u = Function(V) # vtkfile << (sol, t)
dt = 0.04
num_steps = 50
for n in range(num_steps): for n in range(num_steps):
print(t, n)
# Update current time # Update current time
t += dt vtkfile.write(sol, t)
evalargs = {u: u_n}
F1 = ufl.replace(F, evalargs)
# print(F1)
assemble(F1, tensor=k1.vector())
bc.apply(k1.vector())
evalargs = {u: u_n + dt*0.5*k1}
F2 = ufl.replace(F, evalargs)
# print(F2)
assemble(F2, tensor=k2.vector())
bc.apply(k2.vector())
# Compute solution evalargs = {u: u_n + dt*0.5*k2}
solve(a == L, u, bc) F3 = ufl.replace(F, evalargs)
# print(F3)
assemble(F3, tensor=k3.vector())
bc.apply(k3.vector())
evalargs = {u: u_n + dt*k3}
F4 = ufl.replace(F, evalargs)
# print(F4)
assemble(F4, tensor=k4.vector())
bc.apply(k4.vector())
form = (u-u_n)/dt*v*dx - ((1/6)*k1+(1/3)*k2+(1/3)*k3+(1/6)*k4)*v*dx
# form = (u-u_n)/dt*v*dx - F1
a = lhs(form)
L = rhs(form)
solve(a==L, sol, bc)
# Save to file and plot solution
vtkfile << (u, t)
# Update previous solution # Update previous solution
u_n.assign(u) u_n.assign(sol)
t += dt
vtkfile.write(sol,t)
vtkfile.close()
# for n in range(num_steps):
# print(t, n)
# # Update current time
#
# evalargs = {u: u_n}
# F1 = ufl.replace(F, evalargs)
# assemble(F1, tensor=k1.vector())
#
# evalargs = {u: u_n + dt*0.5*k1}
# F2 = ufl.replace(F, evalargs)
# assemble(F2, tensor=k2.vector())
#
# form = (u-u_n)/dt*v*dx - k2*v*dx
# a = lhs(form)
# L = rhs(form)
#
# solve(a==L, sol, bc)
#
# vtkfile << (sol, t)
#
# # Update previous solution
# u_n.assign(sol)
# t += dt
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment