This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Linear elasticity problems on domains with different materials

0 votes

Dear all,

I am trying to solve a linear elasticity problem on domains with different materials in 2D. The whole domain is a half disk divided into a smaller half disk and its complement. The model has both Dirichlet and Neumann boundary conditions. The Dirichlet bc corresponds to "Bottom" and Newmann "Top". Here is my code:

from __future__ import print_function
from dolfin import *
from mshr import *
import sys, math, numpy

# Create classes for defining parts of the boundaries and the interior
# of the domain
class Bottom(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and near(x[1], 0.0)

class Top(SubDomain):
    def inside(self, x, on_boundary):
        return on_boundary and (between(x[1], (0.5, 1.1)))

class Obstacle(SubDomain):
    def inside(self, x, on_boundary):
        return (x[0]*x[0] + x[1]*x[1] <= 0.25) 

# Define parameters
r = 1.0
MeshDensity = 20

# Initialize sub-domain instances
top = Top()
bottom = Bottom()
obstacle = Obstacle()

# Define the domain and generate the mesh
domain = Circle(Point(0, 0), r) - Rectangle(Point(-1.0, -2.0), Point(1.0, 0.0))
mesh = generate_mesh(domain,MeshDensity,"cgal")
#plot(mesh, 'Mesh')

# Initialize mesh function for interior domains
domains = CellFunction("size_t", mesh)
domains.set_all(0)
obstacle.mark(domains, 1)
#print(domains.array())

# Initialize mesh function for boundary domains
boundaries = FacetFunction("size_t", mesh)
boundaries.set_all(0)
bottom.mark(boundaries, 0)
top.mark(boundaries, 1)
#print(boundaries.array())

# Define input data
a1 = Constant(1.0)
a0 = Constant(0.01)
g = Constant((0.0, -1.0))
f = Constant((0.0, 1.0))

# Define function space and basis functions
V = VectorFunctionSpace(mesh, "Lagrange", 1)
u = TrialFunction(V)
v = TestFunction(V)

# Define Dirichlet boundary ( x=0, y=0 )
c = Expression(("0.0", "0.0"))

# Define Dirichlet boundary conditions at top and bottom boundaries
bcs = DirichletBC(V, c, boundaries, 0)

# Define new measures associated with the interior domains and
# exterior boundaries
dx = Measure('dx', domain=mesh, subdomain_data=domains)
ds = Measure('ds', domain=mesh, subdomain_data=boundaries)

# Define variational form
F = (dot(a0*grad(u), grad(v))*dx(0) + (dot(a1*grad(u), grad(v)))*dx(1)
     - inner(g, v)*ds(1)
     - inner(f, v)*dx(0) - inner(f, v)*dx(1))

# Separate left and right hand sides of equation
a, L = lhs(F), rhs(F)

# Solve problem
u_h = Function(V)
solve(a == L, u_h.vector(), bcs)

# Plot solution
plot(u, title="u")
interactive()

The error message goes as follows:

Can only integrate scalar expressions. The integrand is a tensor expression with value rank 2 and free indices ().

Traceback (most recent call last):
File "/home/jason/Programs/Python/Questions/08-Feb-2016.py", line 72, in
- inner(f, v)dx(0) - inner(f, v)dx(1))
File "/usr/lib/python2.7/dist-packages/ufl/measure.py", line 374, in rmul
error(msg % (integrand.rank(), integrand.ufl_free_indices))
File "/usr/lib/python2.7/dist-packages/ufl/log.py", line 151, in error
raise self._exception_type(self._format_raw(*message))
UFLException: Can only integrate scalar expressions. The integrand is a tensor expression with value rank 2 and free indices ().

The way I see it, after taking the dot product (inner product), all of my integrands are scalars. Is it because the way I define the expression or the function space is wrong, maybe both? It would be really appreciated if you could tell me how to modify it to make it work.

closed with the note: This question has been wonderfully answered.
asked Feb 9, 2016 by Riemannstein FEniCS Novice (210 points)
closed Feb 10, 2016 by Riemannstein

1 Answer

+1 vote
 
Best answer

Maybe use the inner operation instead of dot product:

F = (inner(a0*grad(u), grad(v))*dx(0) + (inner(a1*grad(u), grad(v)))*dx(1)
     - inner(g, v)*ds(1)
     - inner(f, v)*dx(0) - inner(f, v)*dx(1))
answered Feb 9, 2016 by hernan_mella FEniCS Expert (19,460 points)
selected Feb 9, 2016 by Riemannstein

Thank you very much for the reply. I have tried your expression and it seems that the compiler no longer complains about the integrand. Instead, there goes the following error message saying that the equations cannot be solved:

Traceback (most recent call last):
File "/home/jason/Programs/Python/Questions/08-Feb-2016-ans1.py", line 80, in
solve(a == L, u_h.vector(), bcs)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 297, in solve
_solve_varproblem(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 314, in _solve_varproblem
= _extract_args(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 421, in _extract_args
u = _extract_u(args[1])
File "/usr/lib/python2.7/dist-packages/dolfin/fem/solving.py", line 471, in _extract_u
"Expecting second argument to be a Function")
File "/usr/lib/python2.7/dist-packages/dolfin/cpp/common.py", line 2611, in dolfin_error
return _common.dolfin_error(location, task, reason)
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@fenicsproject.org


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


*** -------------------------------------------------------------------------
*** Error: Unable to solve variational problem.
*** Reason: Expecting second argument to be a Function.
*** Where: This error was encountered inside solving.py.
*** Process: 0


*** DOLFIN version: 1.6.0
*** Git changeset: unknown
*** -------------------------------------------------------------------------

May I ask further for the reason? Thanks again!

The solve function expects a Function object as second argument (u_h.vector() is a GenericVector).

Consider this:

solve(a == L, u_h, bcs)

I changed my second argument into a function. Now the "Reason" of the error message says:

*** Error: Unable to assemble system.
*** Reason: expected a linear form for L.
*** Where: This error was encountered inside SystemAssembler.cpp.

According to my expression, "L" should be a linear form right? What is the reason for this? Thanks again!

Change

plot(u, title="u")

to

plot(u_h, title="u")

Thank you so much! It worked just as expected and the results are very nice! Yet, the error message "Unable to assemble system" doesn't seem to point me to the right direction. I mean plotting the solution and computing it are more or less separated tasks. That's exactly what makes your advice so valuable.

Glad to hear! (i think the same about the error message...)

I am novice in Fenics. I have followed this code and got solution i got null, can any one help, thanks in advance

...