Runtime Issue with declaration of multiple boundary conditions

I’ve been having issues with declaring multiple boundary conditions for a solver. The code solves for one boundary condition, but when I try to initialize multiple boundary conditions and solve it gives a runtime error at the following line
solve(a_phi == L_phi, Phi, BCS)

I’m pretty sure that I am not defining domain “DX” or boundary conditions “BCS” correctly.

Any inputs would be greatly appreciated.

Code:

from fenics import * 
import matplotlib.pyplot as plt
import numpy as np

path = "/mnt/c/Files/modlab/code/"

"""
Dimensions of mesh
"""
mesh_side = 600e-6

"""
Creating a mesh
"""
cells_x, cells_y, cells_z = 20,20,20


outer_mesh = BoxMesh(Point(-mesh_side/2.0,-mesh_side/2.0,-mesh_side/2.0), 
                     Point(mesh_side/2.0, mesh_side/2.0, mesh_side/2.0), 
                     cells_x, cells_y, cells_z)

# Define function space
V = FunctionSpace(outer_mesh, 'P', 1)  # For electric potential Phi and electric field E

# Define trial and test functions
phi = TrialFunction(V)
q = TestFunction(V)

# Define the dimensions of the cuboid
bot_x, bot_y, bot_z = 300e-6, 200e-6, 10e-6

#Dimensions of pads
#length, breadth, thickness
pad_x, pad_y, pad_z = 50e-6, 50e-6, 10e-6 

# Define the center of the cuboid
center = Point(0, 0, 0)

# Define the center of pads
p1_center = Point(bot_x/4.0, bot_y/4.0, 0)

# Define separate subdomains for pads and cuboid
class Pad1(SubDomain):
    def inside(self, x, on_boundary):
        return (abs(x[0] - p1_center[0]) <= pad_x/2) and \
               (abs(x[1] - p1_center[1]) <= pad_y/2) and \
               (abs(x[2] - p1_center[2]) <= pad_z/2) and \
               (x[0] > 0) and (x[1] > 0)

class Cuboid(SubDomain):
    def inside(self, x, on_boundary):
        return (abs(x[0] - center[0]) <= bot_x/2) and \
               (abs(x[1] - center[1]) <= bot_y/2) and \
               (abs(x[2] - center[2]) <= bot_z/2) and \
               not any([Pad1().inside(x, on_boundary)])

# Initialize mesh function for the subdomains
markers = MeshFunction("size_t", outer_mesh, outer_mesh.topology().dim())
markers.set_all(0)

# Mark the subdomains for the pads
pad1 = Pad1()
pad1.mark(markers, 1)

# Mark the subdomain for the cuboid as 5
cuboid = Cuboid()
cuboid.mark(markers, 5)

# Define new measures for integration over each pad and rest of the bot
dx_pad1 = Measure("dx", domain=outer_mesh, subdomain_data=markers, subdomain_id=1)
dx_bot = Measure("dx", domain=outer_mesh, subdomain_data=markers, subdomain_id=5)


# Define boundary conditions for the pads and the bot
V_pad1 = 4
V_surface_value = 4


bc_pad1 = [DirichletBC(V, Constant(V_pad1), pad1)]
bc_bot = [DirichletBC(V, Constant(V_surface_value), cuboid)]

# Define weak forms
DX = dx_pad1 + dx_bot
a_phi = inner(grad(phi), grad(q)) * DX
L_phi = Constant(0) * q * DX  # Assuming no source term in this case

# Solve for Phi and Apply multiple boundary conditions
Phi = Function(V)
BCS = bc_pad1 + bc_bot
solve(a_phi == L_phi, Phi, BCS)
print("File Executed")

Error: (The code 165 lines)
~$ python3 “/home/ubuntu_wsl/modlab/code/oct19/tempCodeRunnerFile.py”
Solving linear variational problem.
Traceback (most recent call last):
File “/home/ubuntu_wsl/lab/code/oct19/tempCodeRunnerFile.py”, line 135, in
solve(a_phi == L_phi, Phi, BCS)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py”, line 233, in solve
_solve_varproblem(*args, **kwargs)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py”, line 273, in _solve_varproblem
solver.solve()
RuntimeError:
*** -------------------------------------------------------------------------
*** DOLFIN encountered an error. If you are not able to resolve this issue
*** using the information listed below, you can ask for help at


*** fenics-support@googlegroups.com


*** Remember to include the error message listed below and, if possible,
*** include a minimal running example to reproduce the error.


*** -------------------------------------------------------------------------
*** Error: Unable to set given (local) rows to identity matrix.
*** Reason: some diagonal elements not preallocated (try assembler option keep_diagonal).
*** Where: This error was encountered inside PETScMatrix.cpp.
*** Process: 0


*** DOLFIN version: 2019.2.0.dev0
*** Git changeset: ubuntu
*** -------------------------------------------------------------------------
Dolphin Version:
2019.2.0.dev0
Installed on Ubuntu distribution in WSL
Used commands in Ubuntu FEniCS on Ubuntu section
Link - https://fenicsproject.org/download/archive/

IDE - Visual Studio Code
Version: 1.83.1 (user setup)
Commit: ****************************
Date: 2023-10-10T23:48:05.904Z
Electron: 25.8.4
ElectronBuildId: 24154031
Chromium: 114.0.5735.289
Node.js: 18.15.0
V8: 11.4.183.29-electron.0
OS: Windows_NT x64 10.0.22621

Please create a minimal working example and paste it here between ``` tags. Right now we can’t really help you because we can’t see how you’ve defined things like V, dx_pad1, dx_pad2, etc.

from fenics import * 
import matplotlib.pyplot as plt
import numpy as np

path = "/mnt/c/Files/modlab/code/"

"""
Dimensions of mesh
"""
mesh_side = 600e-6

"""
Creating a mesh
"""
cells_x, cells_y, cells_z = 20,20,20


outer_mesh = BoxMesh(Point(-mesh_side/2.0,-mesh_side/2.0,-mesh_side/2.0), 
                     Point(mesh_side/2.0, mesh_side/2.0, mesh_side/2.0), 
                     cells_x, cells_y, cells_z)

# Define function space
V = FunctionSpace(outer_mesh, 'P', 1)  # For electric potential Phi and electric field E

# Define trial and test functions
phi = TrialFunction(V)
q = TestFunction(V)

# Define the dimensions of the cuboid
bot_x, bot_y, bot_z = 300e-6, 200e-6, 10e-6

#Dimensions of pads
#length, breadth, thickness
pad_x, pad_y, pad_z = 50e-6, 50e-6, 10e-6 

# Define the center of the cuboid
center = Point(0, 0, 0)

# Define the center of pads
p1_center = Point(bot_x/4.0, bot_y/4.0, 0)

# Define separate subdomains for pads and cuboid
class Pad1(SubDomain):
    def inside(self, x, on_boundary):
        return (abs(x[0] - p1_center[0]) <= pad_x/2) and \
               (abs(x[1] - p1_center[1]) <= pad_y/2) and \
               (abs(x[2] - p1_center[2]) <= pad_z/2) and \
               (x[0] > 0) and (x[1] > 0)

class Cuboid(SubDomain):
    def inside(self, x, on_boundary):
        return (abs(x[0] - center[0]) <= bot_x/2) and \
               (abs(x[1] - center[1]) <= bot_y/2) and \
               (abs(x[2] - center[2]) <= bot_z/2) and \
               not any([Pad1().inside(x, on_boundary)])

# Initialize mesh function for the subdomains
markers = MeshFunction("size_t", outer_mesh, outer_mesh.topology().dim())
markers.set_all(0)

# Mark the subdomains for the pads
pad1 = Pad1()
pad1.mark(markers, 1)

# Mark the subdomain for the cuboid as 5
cuboid = Cuboid()
cuboid.mark(markers, 5)

# Define new measures for integration over each pad and rest of the bot
dx_pad1 = Measure("dx", domain=outer_mesh, subdomain_data=markers, subdomain_id=1)
dx_bot = Measure("dx", domain=outer_mesh, subdomain_data=markers, subdomain_id=5)


# Define boundary conditions for the pads and the bot
V_pad1 = 4
V_surface_value = 4


bc_pad1 = [DirichletBC(V, Constant(V_pad1), pad1)]
bc_bot = [DirichletBC(V, Constant(V_surface_value), cuboid)]

# Define weak forms
DX = dx_pad1 + dx_bot
a_phi = inner(grad(phi), grad(q)) * DX
L_phi = Constant(0) * q * DX  # Assuming no source term in this case

# Solve for Phi and Apply multiple boundary conditions
Phi = Function(V)
BCS = bc_pad1 + bc_bot
solve(a_phi == L_phi, Phi, BCS)
print("File Executed")

Sorry if the formatting is wrong, new to the community.

If possible, please edit your comment to put your code between 3 backticks (```) instead of 3 apostrophes (‘’'), so it gets formatted into a code block. Also, in your opening post, what is the specific error that you get? You posted this:

Error: (The code 165 lines)
~$ python3 “/home/ubuntu_wsl/modlab/code/oct19/tempCodeRunnerFile.py”
Solving linear variational problem.
Traceback (most recent call last):
File “/home/ubuntu_wsl/lab/code/oct19/tempCodeRunnerFile.py”, line 135, in
solve(a_phi == L_phi, Phi, BCS)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py”, line 233, in solve
_solve_varproblem(*args, **kwargs)
File “/usr/lib/petsc/lib/python3/dist-packages/dolfin/fem/solving.py”, line 273, in _solve_varproblem
solver.solve()
RuntimeError:

Which doesn’t contain the actual RuntimeError.

Thank you for the correction. I’ve done that both in the comments and the post.

1 Like

However, we still don’t know what error you are getting exactly, because all you’ve posted so far is this:

Which cuts off the RuntimeError message.

1 Like

I removed “subdomain_id = some_value” from the defnition of custom “dx” and it seems to solve the issue.