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

How can I solve two problems parallelly with fenics! It seems that fenics can't do it by default!

+2 votes

My code is like this! I just want to solve two problems parallelly!

from dolfin import *
import numpy as np
from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()


mesh = UnitSquareMesh(32, 32)
V = FunctionSpace(mesh, "Lagrange", 1)
u = TrialFunction(V)
v = TestFunction(V)

if(rank==0):
    g = Expression('sin(pi*x[0])*sin(pi*x[1])',pi = np.pi )
    f = Expression('2*pi*pi*sin(pi*x[0])*sin(pi*x[1])',pi = np.pi)
elif(rank==1):
    g = Expression('exp(-ll*(x[0]-0.5) )+exp(ll*(x[0]-0.5))',ll=20.0 )
    f = Expression('-ll*ll*exp(-ll*(x[0]-0.5))-ll*ll*exp(ll*(x[0]-0.5))',ll=20.0)
class Boundary(SubDomain):
    def inside(self,x,on_boundary):
        return on_boundary      

bc = DirichletBC(V, g, Boundary())
a = inner(grad(u), grad(v))*dx 
L = f*v*dx 
A = assemble( a )
b = assemble( L )
bc.apply(A,b)
u = Function(V)
solve(A,u.vector(),b)
plot(u)
interactive()

And I run it like this: mpirun -np 2 python myprogram.py
Thanks!

asked Nov 21, 2014 by motianlunfenics FEniCS Novice (310 points)

1 Answer

+1 vote

It is not entirely clear what you are trying to do. If you want to solve two different Poisson problems simultaneously on two processors, then you cannot use the default Fenics mesh, because the default is to distribute it. You can, however, let Fenics know that you want the same non-distributed mesh on both processors

mesh = UnitSquareMesh(mpi_comm_self(), 32, 32)

Now you should get two different solutions on the two processors.

answered Nov 21, 2014 by mikael-mortensen FEniCS Expert (29,340 points)

Thank you very much, you solve my problem perfectly!

I also have a related question :

from dolfin import *
import numpy as np
from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

mesh = UnitSquareMesh(mpi_comm_self(),16, 16)
V = FunctionSpace(mesh, "DG", 1)

# Define test and trial functions
v = TestFunction(V)
u = TrialFunction(V)

# Define normal component, mesh size and right-hand side
n = FacetNormal(mesh)
h = CellSize(mesh)
h_avg = (h('+') + h('-'))/2


if(rank==0):
    g = Expression('sin(pi*x[0])*sin(pi*x[1])',pi = np.pi )
    f = Expression('2*pi*pi*sin(pi*x[0])*sin(pi*x[1])',pi = np.pi)
if(rank==1):
    g = Expression('exp(-ll*(x[0]-0.5) )+exp(ll*(x[0]-0.5))',ll=20.0 )
    f = Expression('-ll*ll*exp(-ll*(x[0]-0.5))-ll*ll*exp(ll*(x[0]-0.5))',ll=20.0)

alpha = 5.0
gamma = alpha*2


# Define variational problem

a = dot(grad(v), grad(u))*dx  \
    - dot(avg(grad(v)), jump(u, n))*dS \
    - dot(jump(v, n), avg(grad(u)))*dS \
    + alpha/h_avg*dot(jump(v, n), jump(u, n))*dS \
    - dot(grad(v), u*n)*ds \
    - dot(v*n, grad(u))*ds \
    + gamma/h*v*u*ds
L = v*f*dx + gamma/h*v*g*ds - dot(grad(v), g*n)*ds 

# Compute solution

A = assemble(a)
b = assemble(L)

but when I run : mpirun -np 2 python myprogram.py
there was wrong ? I don't know why ? Thanks!

...