Python wrapper for running instances of trisurf-ng
Samo Penic
2019-07-15 7f64589f9f172b7965a086bcceed8c47a51daca7
commit | author | age
7f6458 1 from trisurf.trisurf import Directory, Tape
SP 2 from trisurf import trisurf as ts
3 #from hashlib import md5
4 import os
5 import django
6 import sys
7 os.chdir('/home/samo/trisurf-server')
8 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web_trisurf.settings")
9 django.setup()
10 from database import models
11
12 import numpy as np
13 import ipyvolume as ipv
14 import meshio
15 import matplotlib.pyplot as plt
16 import uuid
17
18 def read_mesh(filename):
19     tmpfilename='/tmp/{}.vtu'.format(str(uuid.uuid4()))
20     with open (filename) as f:
21         contents=f.read()
22     valid_vtu=contents[0:contents.find("<trisurfversion>")]+contents[contents.find("<UnstructuredGrid>"):]
23     with open (tmpfilename, 'w') as f:
24         f.write(valid_vtu)
25     mesh=meshio.read(tmpfilename)
26     os.unlink(tmpfilename)
27     return mesh
28
29 def display_vesicle(run, iteration=None, colormap='bwr', point_data='vertices_idx'):
30     cmap=plt.get_cmap(colormap)
31     if iteration is not None:
32         filename=os.path.join(run.Dir.fullpath(),'timestep_{:06d}.vtu'.format(int(iteration)))
33     else:
34         filename=os.path.join(run.Dir.fullpath(),run.getLastVTU())
35     mesh=read_mesh(filename)
36     fig=ipv.figure()
37     color_data=mesh.point_data[point_data]/mesh.point_data[point_data].max()
38     points=mesh.points.transpose()
39     trimesh=ipv.plot_trisurf(*points, triangles=mesh.cells['triangle'], color=[c[0:3] for c in cmap(color_data)])
40     ipv.xlim(points[0,:].min(),points[0,:].max())
41     ipv.ylim(points[1,:].min(),points[1,:].max())
42     ipv.zlim(points[2,:].min(),points[2,:].max())
43     ipv.squarelim()
44     #ipv.style.use('nobox')
45     #ipv.pylab.style.use('nobox')
46     return fig #ipv.show()
47     
48     
49 def database2runs(objs):
50     runs=[]
51     for obj in objs:
52         r=ts.Runner(tape='/home/samo/trisurf-tapes/{}/tape'.format(obj.id))
53         r.maindir=obj.simulation.directory
54         r.subdir=obj.subdir
55         runs.append(r)
56     return runs