Hello everyone.
I just started to study fenics.
I have a question. I want to set the boundary conditions as a class if the boundaries == 2 and boundaries == 3.
I tried to do it but it did not work.
Thanks in advance!
from dolfin import *
import matplotlib.pyplot as plt
mesh = Mesh("cube.xml")
mesh.init()
subdomains = MeshFunction("size_t", mesh, "cube_physical_region.xml")
boundaries = MeshFunction("size_t", mesh, "cube_facet_region.xml")
dx = Measure('dx', domain=mesh, subdomain_data = subdomains)
ds = Measure('ds', domain=mesh, subdomain_data = boundaries)
order = 1
BDM = FiniteElement("BDM", triangle, order)
DG = FiniteElement("DG", triangle, order - 1)
mixed = MixedElement(BDM, DG, DG)
W = FunctionSpace(mesh, mixed)
tdim = mesh.topology().dim()
mesh.init(tdim-1, tdim)
dw = TrialFunction(W)
wt = TestFunction(W)
v, q, r = split(wt)
w = Function(W)
w0 = Function(W)
u , p, s = split(w)
u0, p0, s0 = split(w0)
sm = 0.5 * (s0 + s)
class PressureBC(UserExpression):
def eval_cell(self, values, x, cell):
for facet in facets(mesh):
if facet.index() == 2:
values[0] = 1
class SaturationBC(UserExpression):
def eval(self, values, x):
for facet in facets(mesh):
if facet.index() == 3:
values[0] = 1
pbar = PressureBC()
sbar = SaturationBC()
dt = Constant(0.01)
kinv = Expression("1 / max (exp(-pow(10 * (x[1] - 0.5 - 0.1 * sin(10 * x[0])), 2)), 0.01)", degree=2)
zero = Constant(0.0)
Kinv = as_matrix(((kinv, zero), (zero, kinv)))
mu_rel = Constant(0.2)
def linv(s):
return 1.0 / ((1.0 / mu_rel)*s**2 + (1.0 - s)**2)
def F(s):
return s**2 / (s**2 + mu_rel * (1.0 - s)**2)
n = FacetNormal(mesh)
un = 0.5 *( inner(u0, n) + sqrt(inner(u0, n) * inner(u0, n)))
un_h = 0.5 * (inner(u0, n) - sqrt(inner(u0, n) * inner(u0, n)))
stab = inner(jump(r), un('+')*F(sm)('+')-un('-')*F(sm)('-'))*dS+r*un_h*sbar*ds
#stab = inner(jump(r), un('+')*F(sm)('+')-un('-')*F(sm)('-'))*ds+r*(un_h)*sbar*ds
F1 = inner(v, linv(sm)*Kinv*u)*dx - div(v)*p*dx + inner(v, pbar*n)*ds
F2 = q * div(u) * dx
F3 = r * (s - s0) * dx - dt * inner(grad(r), F(sm) * u) * dx + dt * r * F(sm) * un * ds + dt * stab
F = F1 + F2 + F3
J = derivative(F, w, dw)
info("Hello")
print(" ")
newton_solver_parameters = {"nonlinear_solver": "newton",
"newton_solver": {"linear_solver": "bicgstab", "preconditioner": "default", "absolute_tolerance" : 1e-6, "relative_tolerance" :1e-5, "maximum_iterations": 10}}
problem = NonlinearVariationalProblem(F, w, J=J)
solver = NonlinearVariationalSolver(problem)
solver.parameters.update(newton_solver_parameters)
#info(solver.parameters, True)
# Solve the problem
(iter, converged) = solver.solve()
# Check for convergence
if not converged:
warning("This demo is a complex nonlinear problem. Convergence is not guaranteed when modifying some parameters or using PETSC 3.2.")
folder = "res/examp/"
u_file = File(folder + "velocity.pvd")
p_file = File(folder + "pressure.pvd")
s_file = File(folder + "saturation.pvd")
max_time_iters = 100
for iter in range(max_time_iters + 1):
print("Time iteration = %d" % iter)
w0.assign(w)
solver.solve()
u, p, s = w.split()
u_file << u
p_file << p
s_file << s