Skip to content

Instantly share code, notes, and snippets.

@julesghub
Created June 18, 2019 05:44
Show Gist options
  • Save julesghub/b72ebc21c8ae6146bb4e6a5c7346c088 to your computer and use it in GitHub Desktop.
Save julesghub/b72ebc21c8ae6146bb4e6a5c7346c088 to your computer and use it in GitHub Desktop.
nn parallel test. Can run with 1-4 procs.
#!/usr/bin/env python
# coding: utf-8
import underworld as uw
import numpy as np
import glucifer
import time
# In[11]:
mesh = uw.mesh.FeMesh_Cartesian(elementRes=(500,500))
mvar = mesh.add_variable(nodeDofCount=1, dataType="double")
rvar = mesh.add_variable(nodeDofCount=1, dataType="double")
rvar.data[:] = uw.mpi.rank
# In[12]:
swarm = uw.swarm.Swarm(mesh)
svar = swarm.add_variable(count=1, dataType="int")
# In[14]:
# coord = np.random.random(size=(4,2))
coord = np.array([
[ 0.60912071, 0.19463198],
[ 0.45136018, 0.8336581 ],
[ 0.12331671, 0.40281476],
[ 0.83136761, 0.87640524] ])
# In[15]:
swarm.add_particles_with_coordinates(coord)
swarm.shadow_particles_fetch()
# In[16]:
rank = uw.mpi.rank
comm = uw.mpi.comm
# get particle populations on procs to assign global id
counts = comm.allgather(swarm.particleLocalCount)
prev = sum(counts[:rank])
svar.data[:,0] = np.arange(prev, prev+counts[rank])
# In[18]:
start = time.clock()
mvar.data[:] = svar.evaluate(mesh)
if uw.mpi.rank == 0:
print("Time taken: {:.5f} sec".format(time.clock()-start))
# In[19]:
store = glucifer.Store("viz")
fig = glucifer.Figure(store)#figsize=(800,600))
point = glucifer.objects.Points(swarm, fn_size=10., colourBar=False)
# fig.append(glucifer.objects.Mesh(mesh))
fig.append(point)
surf = glucifer.objects.Surface(mesh, mvar, onMesh=True)
# surf = glucifer.objects.Surface(mesh, svar, onMesh=True, resolution=500)
point.colourBar["align"] = "right"
point.colourBar["position"] = 0
point.colourBar["size"] = [0.75,0.02]
fig.append(surf)
fig.show()
# In[ ]:
mH = mesh.save("mesh.h5")
rH = rvar.save("rank.h5")
fH = mvar.save("field.h5")
mvar.xdmf("field.xdmf", fH, 'field', mH, 'mesh')
rvar.xdmf("rank.xdmf", rH, 'rank', mH, 'mesh')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment