Closed Thorinwasher closed 1 year ago
That probably means that you trial functions in the block form are not in the right column. Feel free to send an example to reproduce this (as minimal as you can).
I understand your point about not having error message in asserts, but I would give such a task such a low priority that I'd rather tell you now that I won't have the time to fix that.
I see where you're coming from, here's my try on a minimally reproducible code. Probably something stupid I missed to do. Yes, I haven't defined any bcs yet, but that should not be an issue when talking about functionspaces (although I bet the solution would not converge or similar things). Seems like I missed to do something with the definition of a, now that I'm looking at it again
What I'm trying to achieve: solve anything over multiple domains.
from mpi4py import MPI
from dolfinx import mesh
import dolfinx.fem
import multiphenicsx.fem
import ufl
import gmsh
from dolfinx.io import gmshio
kA = 1.0
kC = 2.0
#Creating the mesh with gmsh
if not gmsh.isInitialized():
gmsh.initialize()
gmsh.model.add("battery2d")
model = gmsh.model()
model.occ.addRectangle(0, 0, 0, 1, 0.5, tag = 1)
model.occ.addRectangle(0, 0.5, 0, 1, 0.5, tag = 2)
model.occ.synchronize()
model.addPhysicalGroup(2, [1], 1)
model.addPhysicalGroup(2, [2], 2)
model.mesh.generate(dim=2)
(domain, cellTags, facetTags) = gmshio.model_to_mesh(model=model,comm=MPI.COMM_WORLD,rank=0,gdim=2)
gmsh.finalize()
anodeCells = cellTags.find(1)
cathodeCells = cellTags.find(2)
V = dolfinx.fem.FunctionSpace(domain, ("CG", 1))
VA = V.clone()
VC = V.clone()
dofs_anode = dolfinx.fem.locate_dofs_topological(VA, cellTags.dim, anodeCells)
dofs_cathode = dolfinx.fem.locate_dofs_topological(VC, cellTags.dim, cathodeCells)
restriction_anode = multiphenicsx.fem.DofMapRestriction(VA.dofmap, dofs_anode)
restriction_cathode = multiphenicsx.fem.DofMapRestriction(VC.dofmap, dofs_cathode)
restriction = [restriction_anode, restriction_cathode]
(uA, uC) = (ufl.TrialFunction(VA), ufl.TrialFunction(VC))
(vA, vC) = (ufl.TestFunction(VA), ufl.TestFunction(VC))
def poissonEquation_a(u : ufl.TrialFunction,v : ufl.TestFunction,k : float):
return ufl.dot(ufl.grad(u), ufl.grad(v)) * ufl.dx
def poissonEquation_L(u : ufl.TrialFunction,v : ufl.TestFunction):
return ufl.inner(300 * ufl.sin(20), v) * ufl.dx
a = [[poissonEquation_a(u=uA,v=vA,k=kA)],
[poissonEquation_a(u=uC,v=vC,k=kC)]]
L = [poissonEquation_L(u=uA,v=vA),
poissonEquation_L(u=uC,v=vC)]
a_cpp = dolfinx.fem.form(a)
L_cpp = dolfinx.fem.form(L)
A = multiphenicsx.fem.petsc.assemble_matrix_block(a_cpp, bcs=[],restriction=(restriction, restriction))
F = multiphenicsx.fem.petsc.assemble_vector_block(L_cpp, a_cpp, bcs=[], restriction=restriction)
Yep, I solved it: this was just a stupid definition of a, it should be:
a = [[poissonEquation_a(u=uA,v=vA,k=kA),None],
[None,poissonEquation_a(u=uC,v=vC,k=kC)]]
I probably have some reading to do, as I don't understand why a would need to be defined in a matrix form here
You are right that you do not need to use the block form here, since the equations for uA and for uC are fully decoupled, thus you could solve them separately. However, as soon as one of the off-diagonal terms is non-None
, that will couple the A
and the C
problems.
Okay, I see. I still don't understand how uA and uC would couple if I add off diagonal terms. What equation is being solved when there's a matrix on the left side and a vector on the right side?
a * q = f
what is the vector q?
Sorry for asking too many questions : )
For instance, something like
- \Delta u_A - k_{AC} \Delta u_C = f_A
- k_{CA} \Delta u_A - \Delta u_C = f_C
for k_{AC}
and k_{CA}
non zero.
In that scenario, would 'a' be defined as:
$$
\left(\begin{array}{cc}
- \nabla u_A * \nabla v_A dx & -k_{AC} \nabla u_C * \nabla v_C dx\\
- k_{CA} \nabla u_A * \nabla v_A dx & - \nabla u_C * \nabla v_C dx
\end{array}\right)
$$
?
There are two errors in your formulation:
$$
\left(\begin{array}{cc}
- \nabla u_A * \nabla v_A dx & -k_{AC} \nabla u_C * \nabla v_{!!!! A !!!!} dx\\
- k_{CA} \nabla u_A * \nabla v_{!!!! C !!!!} dx & - \nabla u_C * \nabla v_C dx
\end{array}\right)
$$
Aha. Thanks for the help <3
What does this mean?
I can send the code as well if you want, but my main issue with this is that there is no text saying the issue.
Maybe it should be something more like this?
I'm happy to contribute, but I'm a beginner at using dolfinx (any fem solver really), so I doubt I could help that much with fem related stuff.
There's many other spots where this is the scenario, just highlighting one I ran into.