Unable to Define nonlinear variational problem

#1

Here is the error I am getting:

Error: Unable to define nonlinear variational problem F(u; v) = 0 for all v.
*** Reason: Expecting the residual F to be a linear form (not rank 2).
*** Where: This error was encountered inside NonlinearVariationalProblem.cpp.
*** Process: 0

My code (I can try get to get a shorter version but I don’t know what’s causing it at the moment):

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
H = 1.0
mesh = BoxMesh(Point(0., 0., 0.), Point(length, length, width), 10, 10, 2)
V = VectorFunctionSpace(mesh, 'P', 1)

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

#-----define boundary-----#
# 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-----#
#boundary expression
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): #check if 0<x<loop_side
        if near(x[1], 0., tol) or near(x[1], loop_side, tol): #check if 0<y<loop_side
            return near(x[2], loop_height_z, tol) #check if z ~= loop_height_z
    return False

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

#defining variational problem
B = TrialFunction(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, vector = F)

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

You should not use TrialFunction in a non-linear problem, see for instance:
https://bitbucket.org/fenics-project/dolfin/src/master/python/demo/documented/nonlinear-poisson/demo_nonlinear-poisson.py.rst

2 Likes
#3

Thank you! That fixed the issue!