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