Hello, I have a problem right now, which is I want to derive the value of a point in 1D that changes over time, and then apply that value to an edge in 2D as a first kind of boundary condition, how do I do that?
Here is my 1D code:
# 1D---The value at 0.2 changed over time and was saved in a CSV file
from fenics import *
import numpy as np
import csv
mesh = IntervalMesh(100,0,1)
L = FunctionSpace(mesh, 'DG', 1)
def k(T):
return 100 + 0.5 * T
def mu(T):
return 1000 + 0.7 * T
def c(T):
return 50 + 0.05 * T
q = Expression('100000', degree=0)
T_ini = Expression('-20', degree=0)
f = Constant(0.0)
t_total = 5
num_steps = 50
dt = Constant(t_total / num_steps)
class BoundaryX0(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and abs(x[0] - 0.0) < DOLFIN_EPS
class BoundaryX1(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and abs(x[0] - 1) < DOLFIN_EPS
bx0 = BoundaryX0()
bx1 = BoundaryX1()
boundary_markers = MeshFunction('size_t', mesh, mesh.topology().dim()-1)
bx0.mark(boundary_markers, 0)
bx1.mark(boundary_markers, 1)
boundary_conditions = {0: {'Neumann': q}, 1: {'Dirichlet': T_ini}}
ds = Measure('ds', domain=mesh, subdomain_data=boundary_markers)
bcs = []
for i in boundary_conditions:
if 'Dirichlet' in boundary_conditions[i]:
bc = DirichletBC(L, boundary_conditions[i]['Dirichlet'], boundary_markers, i)
bcs.append(bc)
T_n = interpolate(T_ini, L)
T = Function(L)
v = TestFunction(L)
F = mu(T)*c(T)*T*v*dx + dt*k(T)*dot(grad(T), grad(v))*dx - mu(T)*c(T)*T_n*v*dx - dt*f*v*dx - dt*q*v*ds(0)
J = derivative(F, T)
problem = NonlinearVariationalProblem(F, T, bcs, J)
solver = NonlinearVariationalSolver(problem)
prm = solver.parameters
prm['nonlinear_solver'] = 'newton'
prm['newton_solver']['absolute_tolerance'] = 1e-1
prm['newton_solver']['relative_tolerance'] = 1e-2
prm['newton_solver']['maximum_iterations'] = 100
prm['newton_solver']['relaxation_parameter'] = 1.0
Ts = []
times = []
t = 0
while t < t_total:
t += float(dt)
solver.solve()
Ts.append(T(0.2))
times.append(t)
T_n.assign(T)
with open('T(0.2).csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['times', 'T(0.2)'])
for i, j in zip(times, Ts):
writer.writerow([i, j])
This code mainly calculates the T value and saves the value at 0.2 and the corresponding time change in a csv file. Next, I need to apply the value of this point to an edge of the 2D model as the first type of boundary condition. At this time, I encounter a problem because the value is changing and I don’t know how to deal with it.
Here is my 2D code:
from fenics import *
mesh = RectangleMesh(Point(0, 0), Point(2, 2), 50, 50)
L = FunctionSpace(mesh, 'DG', 1)
def k(T):
return 100 + 0.5 * T
def mu(T):
return 1000 + 0.7 * T
def c(T):
return 50 + 0.05 * T
t_total = 5
num_steps = 50
dt = Constant(t_total / num_steps)
class BoundaryX0(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and abs(x[0] - 0.0) < DOLFIN_EPS
class BoundaryX1(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and abs(x[0] - 1) < DOLFIN_EPS
bx0 = BoundaryX0()
bx1 = BoundaryX1()
boundary_markers = MeshFunction('size_t', mesh, mesh.topology().dim()-1)
bx0.mark(boundary_markers, 0)
bx1.mark(boundary_markers, 1)
boundary_conditions = {0: {'Dirichlet': ?}, 1: {'Dirichlet': T_ini}}
When I wrote this, I didn’t know how to write down (1D and 2D have the same solution time and step size). Thank you all!