Hi
My question is in the following of my previous question here
I want to solve an elasticity problem in parallel when the Young’s modulus and the Poisson ratio change in different steps (e.g. Parametric study). Lets say the Young’s modulus changes in 3 steps and the Poisson ratio changes in 2 steps (There are 3x2=6 combinations). In my previous question I learned how to assign each combination to a specific core (Using 6 cores). Now, lets say I have limited number of cores for example only 2 cores and I have to distribute these 6 different combinations equally between these 2 cores (e.g. 3 combinations on each). This is what I came up with:
from dolfin import *
import time
import itertools
start_time = time.time()
L = 25.
H = 1.
Nx = 250
Ny = 10
rho_g = 1e-3
f = Constant((0,-rho_g))
def elasticity(E,nu,naming1,naming2, comm=mpi_comm_world()):
mesh = RectangleMesh(comm, Point(0., 0.), Point(L, H), Nx, Ny, "crossed")
mu = E/2/(1+nu)
lmbda = E*nu/(1+nu)/(1-2*nu)
def eps(v):
return sym(grad(v))
def sigma(v):
return lmbda * tr(eps(v)) * Identity(2) + 2.0 * mu * eps(v)
V = VectorFunctionSpace(mesh, 'Lagrange', degree=2)
du = TrialFunction(V)
u_ = TestFunction(V)
a = inner(sigma(du), eps(u_)) * dx
l = inner(f, u_) * dx
def left(x, on_boundary):
return near(x[0], 0.)
bc = DirichletBC(V, Constant((0., 0.)), left)
u = Function(V, name="Displacement")
solve(a == l, u, bc)
info('E %g nu %g -> |uh|=%g' % (E,nu, u.vector().norm('l2')))
File(comm, 'foo%g %g .pvd' % (naming1.index(E),naming2.index(nu))) << u
E = [3e5 , 2e5 ,1e5]
nu =[0.15 , 0.3]
A = E
B = nu
#All 6 combinations
E_nu_combos = list(itertools.product(E, nu))
#Storing combinations in an array (This array includes 3 lists)
E_nu_combos1 = []
E_nu_combos2 = []
E_nu_combos3 = []
for i in range(2):
ss = E_nu_combos[i]
E_nu_combos1.append(ss)
for j in range(2,4):
bb = E_nu_combos[j]
E_nu_combos2.append(bb)
for m in range(4,6):
cc = E_nu_combos[m]
E_nu_combos3.append(cc)
Young1 = [E_nu_combos1]
Young2 = [E_nu_combos2]
Young3 = [E_nu_combos3]
Young1.extend(Young2)
Young1.extend(Young3)
#Here is how I solve it in a loop
for k in range(len(Young1)):
assert len(Young1[k]) >= MPI.size(mpi_comm_world())
my_E, my_nu = Young1[k][MPI.rank(mpi_comm_world())]
elasticity(my_E,my_nu,A,B, mpi_comm_self()) # Do everything on THIS process
print("--- %s seconds ---" % (time.time() - start_time))
And running the script by:
mpirun -n 2 python test.py
In this case the above code works fine and distributes the 6 different combinations of the 2 parameters on 2 cores equally (3 combinations on each core).
The problem is that my method is not an efficient way for this purpose. I have used a loop at the bottom of the above code to do it. I was wondering if there is a better way. For example something like this strategy:
If the Poisson ratio is equal to 0.15 then use the first core (e.g. rank==0) and if it is equal to 0.3 then use the second core (e.g. rank==1).
I have been trying such a strategy but I could not figure out how I can implement it.
Thanks in advance for your help.