scontact3.py 5.19 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
2
from __future__ import division
from . import mesh
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
3
import shutil
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
4
5
6
7
8
9
10
11
import numpy
import os
import sys
from ctypes import *
import signal

signal.signal(signal.SIGINT, signal.SIG_DFL)

12
dir_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
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
lib = CDLL(os.path.join(dir_path,"libscontact3.so"))

coord_type = c_double*3
is_64bits = sys.maxsize > 2**32

class ParticleProblem :

    def _get_matrix(self, fName, ncol) :
        f = getattr(lib,"particleProblem"+fName)
        f.restype = c_void_p
        ptr = f(self._p)
        size = cast(ptr-(8 if is_64bits else 4), POINTER(c_size_t)).contents.value//8
        buf = (size*c_double).from_address(ptr)
        return numpy.ctypeslib.as_array(buf).reshape([-1,ncol])
    
    def _get_idx(self, fName) :
        f = getattr(lib,"particleProblem"+fName)
        f.restype = c_void_p
        ptr = f(self._p)
        size = cast(ptr-(8 if is_64bits else 4), POINTER(c_size_t)).contents.value//4
        buf = (size*c_int).from_address(ptr)
        return numpy.ctypeslib.as_array(buf)

    def __init__(self, inputfile=None) :
        lib.particleProblemNew.restype = c_void_p
        self._p = c_void_p(lib.particleProblemNew())
        if self._p == None :
            raise NameError("cannot create particle problem")
        if inputfile :
            lib.particleProblemLoad(self._p, inputfile.encode())
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
43
44

    def volume(self):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
45
46
47
48
        return 4./3.*numpy.pi * self._get_matrix("Particle", 2)[:, [0]]**3

    def r(self) :
        return self._get_matrix("Particle", 2)[:, 0]
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
49
50

    def mass(self):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
51
        return self._get_matrix("Particle", 2)[:, [1]]
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
52
53

    def position(self):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
54
        return self._get_matrix("Position", 3)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
55
56

    def velocity(self):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
57
        return self._get_matrix("Velocity", 3)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
58

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
59
60
61
    def triangles(self):
        return self._get_matrix("Triangle",12)

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
62
63
64
65
66
67
68
69
70
71
72
73
    def set_boundary_velocity(self, tag, v) :
        lib.particleProblemGetTagId.restype = c_size_t
        tag = lib.particleProblemGetTagId(self._p, tag.encode())
        disks = self._get_matrix("Disk",5)
        diskTag = self._get_idx("DiskTag")
        disks[diskTag == tag, 2:4] = v
        segments = self._get_matrix("Segment",6)
        segmentTag = self._get_idx("SegmentTag")
        segments[segmentTag == tag, 4:6] = v
        triangles = self._get_matrix("Triangle",12)
        triangleTag = self._get_idx("TriangleTag")
        triangles[triangleTag == tag, 9:12] = v
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
74
75

    def iterate(self, dt, forces) :
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
76
77
        self._get_matrix("ExternalForces",3)[:] = forces
        lib.particleProblemIterate(self._p, c_double(numpy.min(self.r())), c_double(dt), c_double(3e-8), c_int(-1))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
78
79

    def write_vtk(self, odir, i, t) :
80
        lib.particleProblemWriteVtk(self._p, odir.encode(), i)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
81

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
82
83
84
    def read_vtk(self,dirname,i) :
        lib.particleProblemReadVtk(self._p, dirname.encode(),i)

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
85
86
87
88
89
90
91
92
93
94
95
    def add_boundary_disk(self, x0, r, tag) :
        lib.particleProblemAddBoundaryDisk.restype = c_size_t
        return lib.particleProblemAddBoundaryDisk(self._p, coord_type(*x0), c_double(r), tag.encode())

    def add_boundary_segment(self, x0, x1, tag) :
        lib.particleProblemAddBoundarySegment.restype = c_size_t
        return lib.particleProblemAddBoundarySegment(self._p, coord_type(*x0), coord_type(*x1), tag.encode())

    def add_boundary_triangle(self, x0, x1, x2, tag) :
        lib.particleProblemAddBoundaryTriangle.restype = c_size_t
        return lib.particleProblemAddBoundaryTriangle(self._p, coord_type(*x0), coord_type(*x1),  coord_type(*x2), tag.encode())
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
96

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
97
98
    def add_particle(self, x, r, m):
        lib.particleProblemAddParticle(self._p, coord_type(*x), c_double(r), c_double(m))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
99

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
    def load_msh_boundaries(self, filename, tags, shift=[0,0,0]) :
        m = mesh.Mesh(filename)
        self._addv = set()
        self._adde = set()
        pnum = [m.getPhysicalNumber(1, i) for i in tags]
        for ent in m.entities :
            if ent.dimension !=2 or not [i for i in pnum if i in ent.physicals]:
                continue
            tag = 0 if not ent.physicals else ent.physicals[0]
            stag = m.getPhysicalName(ent.dimension, tag) or str(tag)
            for i, el in enumerate(ent.elements) :
                for j in range(3):
                    v0 = el.vertices[j]
                    if not v0[3] in self._addv :
                        self._addv.add(v0[3])
                        self.add_boundary_disk((v0[0]+shift[0],v0[1]+shift[1],v0[2]+shift[2]),0.,stag)
                    v1 = el.vertices[(j+1)%3]
                    if (not (v0[3],v1[3]) in self._adde) and (not (v1[3],v0[3]) in self._adde) :
                        self._adde.add((v0[3],v1[3]))
                        self.add_boundary_segment(
                            (v0[0]+shift[0],v0[1]+shift[1],v0[2]+shift[2]),
                            (v1[0]+shift[0],v1[1]+shift[1],v1[2]+shift[2]),
                            stag)
                        
                v0 = el.vertices[1]
                v1 = el.vertices[0]
                v2 = el.vertices[2]
                self.add_boundary_triangle(
                    (v0[0]+shift[0],v0[1]+shift[1],v0[2]+shift[2]),
                    (v1[0]+shift[0],v1[1]+shift[1],v1[2]+shift[2]),
                    (v2[0]+shift[0],v2[1]+shift[1],v2[2]+shift[2]),
                      stag)