Noobie help - Expecting second argument to be a Function

Hello ! I’m running the legacy 2019 fenics code, and am trying to get a code snipped I found to work. I’ll be hones, I have very little experience with fenics. I’m trying to do the calculations on two volumes (a cube inside a cube) with a single boundary between them, which I made and exported from gmsh. I would appreciate any help you could give me … thank you kindly!

Code:

from __future__ import print_function
from fenics import *
import matplotlib.pyplot as plt
from dolfin import *
from mshr import *

#Import geometry

xml_file="text.xml"

mesh=Mesh(xml_file)
cd=MeshFunction("size_t",mesh,"text_physical_region.xml")
fd=MeshFunction("size_t",mesh,"text_facet_region.xml")


# Function space of the mesh
V = FunctionSpace(mesh, 'CG', 1)

# Set up test and trail functions
v = TestFunction(V)
u = TrialFunction(V)

# Bilinear form

mu_r=1.0
mu_m=2.0
dx = Measure("dx", subdomain_data=cd)

a = inner(grad(u), grad(v))*dx(62)  + mu_m*inner(grad(u), grad(v))*dx(63)
# Air domain +Hard magnetic domain

#magnetization vector
M_0=Constant((0.,0.,1.))

# Linear form
L = inner(M_0, grad(v))*dx(63)
# Create Dirichlet boundary condition
bc = DirichletBC(V, Constant(0.0), fd,61)
# Compute solution
solve(a == L, u, bc, solver_parameters={'linear_solver': 'gmres'})

# Save solution to file in VTK format
vtkfile = File('solution.pvd')
vtkfile << u

Hi @Hirian_Razvan,

Welcome to the FEniCS discourse.

A note for further posts:

  1. Make sure that the code is formatted with 3x` encapsulation, i.e.
```python
def f(x):
   return x
```

This makes sure that the code can be copy-pasted by others.

  1. Supply the error message, including the traceback, i.e.:
fenics@c6fbe02d4e9a:/root/shared$ python3 mwe2.py 
Traceback (most recent call last):
  File "mwe2.py", line 40, in <module>
    solve(a == L, u, bc, solver_parameters={'linear_solver': 'gmres'})
  File "/usr/local/lib/python3.6/dist-packages/dolfin/fem/solving.py", line 233, in solve
    _solve_varproblem(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/dolfin/fem/solving.py", line 247, in _solve_varproblem
    = _extract_args(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/dolfin/fem/solving.py", line 383, in _extract_args
    u = _extract_u(args[1])
  File "/usr/local/lib/python3.6/dist-packages/dolfin/fem/solving.py", line 436, in _extract_u
    raise RuntimeError("Expecting second argument to be a Function.")
RuntimeError: Expecting second argument to be a Function.
  1. Make sure that the example can be reproduced by others. A good idea is to use one of the built in meshes for this, as we do not have access to the following xml files:

To your question;
Note that you have defined your variational form with a TrialFunction

This indicates to DOLFIN that you want to assemble a matrix.
As the output of

the second argument should be a function, i.e.

uh = Function(V)
solve(a == L, uh, bc, solver_parameters={'linear_solver': 'gmres'})

Thank you very much, that seems to have solved it ! and thank you for the explanations on how to post better questions, I will try to do a better job in future.