from fenics import *
# Define mesh
nx, ny = 32, 16
mesh = RectangleMesh(Point(0, 0), Point(15, 3), nx, ny)
# Define function space
V = FunctionSpace(mesh, 'P', 1)
# Define subdomains for boundary conditions
class VeroWhitePlusBoundary(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and near(x[1], 0.0)
class TangoBlackPlusBoundary(SubDomain):
def inside(self, x, on_boundary):
return on_boundary and near(x[1], 3.0)
# Initialize subdomain markers
boundaries = MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
boundaries.set_all(0)
# Mark boundary regions
vero_white_plus_boundary = VeroWhitePlusBoundary()
vero_white_plus_boundary.mark(boundaries, 1)
tango_black_plus_boundary = TangoBlackPlusBoundary()
tango_black_plus_boundary.mark(boundaries, 2)
# Define boundary conditions
bc_VeroWhitePlus = DirichletBC(V, Constant(293.15), boundaries, 1)
bc_TangoBlackPlus = DirichletBC(V, Constant(293.15), boundaries, 2)
bcs = [bc_VeroWhitePlus, bc_TangoBlackPlus]
# Define material properties for VeroWhitePlus and TangoBlackPlus
rho_VeroWhitePlus = 1050.0 # Density (g/cm^3)
Cp_VeroWhitePlus = 2300.0 # Specific heat capacity (J/(kg*K))
k_VeroWhitePlus = 0.14 # Thermal conductivity (W/(m*K))
rho_TangoBlackPlus = 1200.0 # Density (g/cm^3)
Cp_TangoBlackPlus = 1300.0 # Specific heat capacity (J/(kg*K))
k_TangoBlackPlus = 1.4 # Thermal conductivity (W/(m*K))
# Define variational problem for transient heat conduction
T = TrialFunction(V)
v = TestFunction(V)
T_n = Function(V) # Temperature at previous time step
T_new = Function(V) # New temperature
dt = 0.1 # Time step size
T_target = 345.0 # Target temperature
# Define function space for each subdomain
V_VeroWhitePlus = FunctionSpace(mesh, 'P', 1, constrained_domain=boundaries, subdomain_id=1)
V_TangoBlackPlus = FunctionSpace(mesh, 'P', 1, constrained_domain=boundaries, subdomain_id=2)
# Define test functions for each subdomain
v_VeroWhitePlus = TestFunction(V_VeroWhitePlus)
v_TangoBlackPlus = TestFunction(V_TangoBlackPlus)
# Define variational problem
F = (rho_VeroWhitePlus * Cp_VeroWhitePlus * (T - T_n) / dt * v_VeroWhitePlus * dx(subdomain_id=1)
+ k_VeroWhitePlus * dot(grad(T), grad(v_VeroWhitePlus)) * dx(subdomain_id=1)
+ rho_TangoBlackPlus * Cp_TangoBlackPlus * (T - T_n) / dt * v_TangoBlackPlus * dx(subdomain_id=2)
+ k_TangoBlackPlus * dot(grad(T), grad(v_TangoBlackPlus)) * dx(subdomain_id=2))
# Time-stepping loop
t = 0.0
while t < T_target:
solve(F == 0, T_new, bcs)
T_n.assign(T_new)
t += dt
# Plot the solution
plot(T_new, title='Temperature')
plt.colorbar()
plt.show()
``
Cell In[13], line 53
50 T_target = 345.0 # Target temperature
52 # Define function space for each subdomain
---> 53 V_VeroWhitePlus = FunctionSpace(mesh, 'P', 1, constrained_domain=boundaries, subdomain_id=1)
54 V_TangoBlackPlus = FunctionSpace(mesh, 'P', 1, constrained_domain=boundaries, subdomain_id=2)
56 # Define test functions for each subdomain
File ~/miniconda3/envs/fenics_env/lib/python3.12/site-packages/dolfin/function/functionspace.py:33, in FunctionSpace.__init__(self, *args, **kwargs)
31 self._init_from_ufl(*args, **kwargs)
32 else:
---> 33 self._init_convenience(*args, **kwargs)
TypeError: FunctionSpace._init_convenience() got an unexpected keyword argument 'subdomain_id'
Can anyone help me i am new to fenics and i don't kno really what this error means
It is unclear what you are trying to do here. Are you trying to make a function space on a subset of facets of the initial mesh?
Hello the goal is to have one domain assembling 2 domains (rectangle 15 mm × 3 mm × 1 mm, every half is defined by a material)
is this way is better ?
Create mesh and function space for VeroWhitePlus
mesh_vero = BoxMesh(Point(0, 0, 0), Point(15, 3, 0.5), 10, 10, 10)
V_vero = VectorFunctionSpace(mesh_vero, ‘P’, 1)
Create mesh and function space for TangoBlackPlus
mesh_tango = BoxMesh(Point(0, 0, 0.5), Point(15, 3, 1), 10, 10, 10)
V_tango = VectorFunctionSpace(mesh_tango, ‘P’, 1)
Is the material known, or an unknown in your PDE?
the material properties such as density (rho), specific heat capacity (Cp), and thermal conductivity (k) are known and specified for both VeroWhitePlus and TangoBlackPlus materials.
i need to increase the temperature with time and when the temperature increase the thermal expansion increase
VeroWhitePlus :
E = 6 Mpa ; Conductivity = 0.14 k W (m K)−1 ; Density ρ (g cm−3) = 1050 ; Specific heatCp J (Kg K)−1 = 2300
TangoBlackPlus :
E = 3 Mpa ; Conductivity = 1.4 k W (m K)−1 ; Density ρ (g cm−3) = 1200 ; Specific heatCp J (Kg K)−1 = 1300
VeroWhitePlus :
The thermal expansion is not constant it vary with temperature
for each temperature =[273 ; 300.15 ; 310 ; 320 ; 343.15]
each Thermal expansion = [0 ; −1.3 ; −1.35 ; −1.4 ; −1.6]
TangoBlackPlus :
The thermal expansion is not constant it vary with temperature
for each temperature =[273 ; 300.15 ; 310 ; 320 ; 343.15]
each Thermal expansion = [0 ; −0.9 ; −1.2 ; −1.2 ; −1.3]
As you are only giving pseudocode and not the mathematical formulation of your problem, it is hard to see how the different subdomains are defined. For instance in the code above, you have 3 different test functions, one on the whole mesh and two that it Seems like you are trying to restrict to the boundaries of your domain.
If this is the case, it would be benificial if you can write a simplified mathematical problem as a starter. The people can help you step by step
Hello thank you very much for your helpful message
σ = −β∆Ta + DƐ
where ∆T, β, ε, D, σ are the gradient of the temperature profile, the thermal stress modulus, the strain, the matrix of elastic constants, thermal coefficient and the second term of equation points out to the Cauchy stress. a and β are determined as:
a^T = [1,1,1,0,0,0]
β = Eα/(1-2ν)
where E Young’s modulus, ν, Poisson’s ratio, and α is the thermal expansion coefficient.
For a Cartesian 3D model, we may write the transient heat conduction equation as:
∂/∂x (k_x (T) ∂T/∂x)+∂/∂y (k_y (T) ∂T/∂y)+∂/∂z (k_z (T) ∂T/∂z)+G=ρC_p ∂T/∂t+βTƐ_v
where the thermal conductivities in the x, y, and z axes as a function of temperature are denoted by kx (T), ky (T) and kz (T). In addition, G is the heat generation per unit volume, T, temperature, t, time, ρ, density, CP, specific heat, and εV, volume strain
The mathematics here is still far from a minimal example, and it is not clear how each coefficient k_x
, k_y
, k_z
are defined on subdomains.
It seems like you would like to a have a function
def k(T):
if T >=273 and T<= 300.15:
return 0
elif T<=300.15 and T<=310:
return =1.3
elif ...
in a subset of cells, and then with
in the other subdomain.
However, it is to me unclear if VeroWhitePlus
and TangoBlackPlus
is a subset of cells in your domain, or if they are a subset of facets of your 3D domain.