How about:
from fenics import *
import matplotlib.pyplot as plt
import random
from dolfin import *
import numpy as np
import pickle
import time
# import index_handler
import sys
from mpi4py import MPI
#Define periodic boundary conditions.
class PeriodicBoundary(SubDomain):
#Left edge and bottom edge are target domain
def inside(self, x, on_boundary):
return ((near(x[0], 0) or near(x[1], 0.)) and not (near(x[0], 1.) or near(x[1], 1.))) and on_boundary
def map(self, x, y):
#Map top right corner to origin.
if near(x[0], 1.) and near(x[1], 1.):
y[0] = x[0] - 1.
y[1] = x[1] - 1.
#Map right edge of domain to left edge.
elif near(x[0], 1.):
y[0] = x[0] - 1.
y[1] = x[1]
#Map top edge of domain to bottom edge.
elif near(x[1], 1.):
y[0] = x[0]
y[1] = x[1] - 1.
#Need else statement for some unknown reason.
else:
y[0] = -1.
y[1] = -1.
# Define mesh, function space, and BC
N = 100
deg_fe = 1
mesh = UnitSquareMesh(MPI.COMM_SELF, N, N)
pbc = PeriodicBoundary()
V = FunctionSpace(mesh, 'CG', deg_fe, constrained_domain=pbc) #Function space for FE solution
directorypath = "results/"
comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
if (rank == 0):
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", mpi_comm=MPI.COMM_SELF, degree=1)
q = interpolate(f, V)
with XDMFFile(MPI.COMM_SELF,directorypath+"potential.xdmf") as fFile:
fFile.write_checkpoint(q, "q", 0.0)
W = Function(V)
if (rank == 0):
with XDMFFile(MPI.COMM_SELF,directorypath+"potential.xdmf") as fFile:
fFile.read_checkpoint(W, "q", 0)
comm.barrier()
def print_rank(rank):
print(rank)
print_rank(rank)