Here is a MWE
from dolfin import *
import numpy as np
mesh = UnitSquareMesh(10, 10)
class Left(SubDomain):
def inside(self, x, on_boundary):
return near(x[0], 0.0)
class Right(SubDomain):
def inside(self, x, on_boundary):
return near(x[0], 1.0)
class Bottom(SubDomain):
def inside(self, x, on_boundary):
return near(x[1], 0.0)
class Top(SubDomain):
def inside(self, x, on_boundary):
return near(x[1], 1.0)
# Initialize sub-domain instances
left = Left()
top = Top()
right = Right()
bottom = Bottom()
# Initialize mesh function for boundary domains
boundary_subdomains = MeshFunction("size_t", mesh, mesh.topology().dim()-1, 0)
boundary_subdomains.set_all(4)
left.mark(boundary_subdomains, 1)
top.mark(boundary_subdomains, 2)
right.mark(boundary_subdomains, 3)
bottom.mark(boundary_subdomains, 4)
degree = 1
dss=Measure('ds', domain=mesh, subdomain_data=boundary_subdomains)
area_gamma1 = assemble(1*dss(4))
# specific electric conductivity of carbon steel at 20 C
leitfaehigkeit = 1.43e-7
V_phi = FunctionSpace(mesh, "CG", degree)
# Dirichlet boundary condition for the equation of potential
c = Constant(0.0)
bc2 = DirichletBC(V_phi, c, boundary_subdomains, 2)
dx = Measure('dx', mesh)
# Test and trial functions for equation of potential
phi = TrialFunction(V_phi)
v = TestFunction(V_phi)
u_h1 = Function(V_phi) # for equation of potential
f = Constant(0.0)
# bilinearform for the equation of potentialĂź
a = dot(grad(phi), grad(v))*dx
# time parameters
T = 2.0 # final time
num_steps = 10 # number of time steps
dt = T / num_steps # time step size
t = 0.0
alpha = 3 # parameter alpha # those parameters are for now for the L2-projection and need to be changed
beta = 1.2 # parameter beta
# repleasment functions for the equation of potential in the discrete space
u = Function(V_phi)
u_n = Function(V_phi)
# second problem -------------------------------------------------------------------------------------------
V_stress = VectorFunctionSpace(mesh, "CG", degree)
# Test and trial functions for second problem
u2 = TrialFunction(V_stress)
v2 = TestFunction(V_stress)
# Lame coefficients
# 2D case for the creep strain-----------------------
E = Constant(1e5) # Youngs module
nu = Constant(0.3) #
model = "plane_stress"
mu = E/2/(1+nu)
lmbda = E*nu/(1+nu)/(1-2*nu)
if model == "plane_stress":
lmbda = 2*mu*lmbda/(lmbda+2*mu)
def eps(v):
return sym(grad(v))
# stress tensor
def sigma(v):
return lmbda*tr(eps(v))*Identity(2) + 2.0*mu*eps(v)
faktor = Constant(0.5) # in 2D ist der Vorfaktor 1/n = 0.5
f2 = Constant((0, 0)) # no pressure were supplied
#T = Constant((1, 1))
T = np.random.rand(num_steps, 2)
print("Random stress on the boundary = " + str(T))
a2 = inner(sigma(u2), eps(v2))*dx
# Needed to avoid error in the linear form (see ufl datatypes)
T_ufl = as_matrix(T.tolist())
c2 = Constant((0.0, 0.0))
bc2_2 = DirichletBC(V_stress, c2, boundary_subdomains, 2)
u_h2 = Function(V_stress) # for second problem
u_h2n = Function(V_stress)
d = u_h2.geometric_dimension() # space dimension
# Problem heat equation-----------------------------------------------
V_heat = VectorFunctionSpace(mesh, "CG", 1)
# Define test and trial function
u_heat = TrialFunction(V_heat)
v_heat = TestFunction(V_heat)
# define \gamma(\theta), \eta(\theta)
gamma_theta = 0.2
eta = Constant(0.24) # for \eta(\theta) heat conductivity
c3 = Constant((0.0, 0.0))
bc2_3 = DirichletBC(V_heat, c2, boundary_subdomains, 2)
u_h3 = Function(V_heat) # for second problem
u_h3n = Function(V_heat)
sol = Constant(0.2345) # just for testing; it is the solution from another class
for i in range(num_steps-1):
# potential of equation-------------------------
L = f*v*dx + (leitfaehigkeit*(1/area_gamma1)*(-sol))*v*dss(4)
# Compute solution
solve(a == L, u, bc2)
u_n.assign(u)
# stress stuff ---------------------------------------------------
L2 = dot(f2, v2)*dx + dot(T_ufl[i,:], v2)*dss(4)
solve(a2 == L2, u_h2, bc2_2)
# stress
s = sigma(u_h2) - (1./2)*tr(sigma(u_h2))*Identity(d) # deviatoric stress in 2D
von_Mises = sqrt(3./2*inner(s, s))
V = FunctionSpace(mesh, 'P', 1)
von_Mises = project(von_Mises, V)
# Compute magnitude of displacement
u_magnitude = sqrt(dot(u_h2, u_h2))
u_magnitude = project(u_magnitude, V)
# non-stationary heat equation -----------------------------------------
f = Expression("eta*pow(abs(S))+gamma_theta*pow(abs(grad(elektrisches_Pot)))", eta=eta,S=s,gamma_theta=gamma_theta,elektrisches_Pot=u)
t += dt