PETSc error code is: 63 (Argument out of range)

Something is probably wrong with my PETScVector, don’t quite know what to fix. I maybe need to do something like F = PETScVector(comm, 3). 3 is for 3 components of the vector. I don’t know how to use the MPICommonwrapper input at all.

*** Error: Unable to successfully call PETSc function ‘MatSetValuesLocal’.
*** Reason: PETSc error code is: 63 (Argument out of range).
*** Where: This error was encountered inside /build/dolfin-V0jMTP/dolfin-2018.1.0.post1/dolfin/la/PETScMatrix.cpp.
*** Process: 0


*** DOLFIN version: 2018.1.0

from fenics import *

"""
    Simulating Magnetic field from a square loop of wire.
    Box is 5x5x1. The wire with lower right hand corner on the origin and at
    height z = 0.5.
    Equation:

    curl (curl A/mu) = 0
    curl A = B (what we're looking for)
    At the boundary (on the wire), B is defined.
"""

#create mesh and define function space
# box is 5x5x1
length=width= 5.0
height = 0.1
mesh = BoxMesh(Point(0., 0., 0.), Point(length, width, height), 10, 10, 2)
V = VectorFunctionSpace(mesh, 'P', 1)

loop_side = 5.0 #length of square loop
C = 0.1 #constant (~Remanance/4pi) for boundary eqn component
tol = 1E-2 #tolerance for boundary definition
loop_height_z = height/2.0 #the sq loop will be at this height
mu = 1.32E-6; #material mu for neodymium magnet

#-----define boundary components-----#
# x component at boundary
b_D_x_str = "2*K*x[2]*(1/pow(pow(l-x[0], 2) + pow(x[2], 2), 2) - 1/pow(pow(x[0], 2) + pow(x[2], 2), 2))"
# y component at boundary
b_D_y_str = "2*K*x[2]*(1/pow(pow(l-x[1], 2) + pow(x[2], 2), 2) - 1/pow(pow(x[1], 2) + pow(x[2], 2), 2))"
# z component at boundary
b_D_z_str = """-2*K*((x[0]-l)/pow(pow(l-x[0],2) + pow(x[2], 2), 2) + x[1]/(pow(pow(x[1], 2) + pow(x[2], 2), 2)) +
x[0]/(pow(pow(x[0], 2) + pow(x[2], 2), 2)) -
(x[1]-l)/pow(pow(l-x[1],2) + pow(x[2], 2), 2))"""
#-----END define boundary components-----#

b_D = Expression((b_D_x_str, b_D_y_str, b_D_z_str), degree=3, l=loop_side, K=C)

""" Checks if vector x is on the boundary: a square loop with lower left hand
    corner on the origin. Then shifted up by loop_height_z in the z direction"""
def on_boundary(x):
    if (near(x[0], 0., tol) or near(x[0], loop_side, tol)) or \
       (near(x[1], 0., tol) or near(x[1], loop_side, tol)) or \
       (near(x[2], loop_height_z, tol)):
        return True
    else:
        return False

#boundary condition
bc = DirichletBC(V, b_D, on_boundary)

#defining variational problem
# integral over domain (B/mu) * curl(s) = 0
B = Function(V); #B field
s = TestFunction(V); #test function
f = Constant((0., 0., 0.)) # zero on RHS
a = (1/mu)*dot(B, curl(s))*dx # Left hand side
F = PETScVector() #right hand side
assemble(inner(f,s)*dx, tensor = F)

#compute solution
B = Function(V)
solve(a==F, B, bc)

You actually don’t need to create the PETScVector(). You can solve the problem by just specifying a variational form for the right-hand side:

#defining variational problem
# integral over domain (B/mu) * curl(s) = 0
B = TrialFunction(V); #B field  ####### CHANGED
s = TestFunction(V); #test function
f = Constant((0., 0., 0.)) # zero on RHS
a = (1/mu)*dot(B, curl(s))*dx # Left hand side
F = inner(f,s)*dx ####### CHANGED

#compute solution
B = Function(V)
solve(a==F, B, bc)

You can alternatively assemble both the matrix and vector, then solve a linear algebra system, which is sometimes useful if you want to do something special with the assembly process (e.g., modify the assembled matrix and/or vector in some custom way), but the above is simpler for most cases.