fluid.py 1.93 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from ctypes import *
import numpy
import signal
import os

dir_path = os.path.dirname(os.path.realpath(__file__))
lib = CDLL(os.path.join(dir_path,"libmbfluid.so"))

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


class StrongBoundary(Structure):
    _fields_ = [("tag",c_char_p),("field",c_int),("v",c_double)]


class fluid_problem :

18
    def __init__(self, mesh_file_name, mu, rho, alpha, autoEpsilon, strong_boundaries):
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
19
20
        nsb = len(strong_boundaries)
        asb = (StrongBoundary*nsb)(*[StrongBoundary(i[0].encode(),i[1],i[2]) for i in strong_boundaries])
21
        lib.fluid_problem_new.restype = c_void_p
22
        self._fp = c_void_p(lib.fluid_problem_new(mesh_file_name.encode(), c_double(mu), c_double(rho), c_double(alpha), c_bool(autoEpsilon), nsb, asb))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
23
24
25
26
27
28
29
        if self._fp == None :
            raise NameError("cannot create fluid problem")

    def __del__(self):
        if(self._fp != None) :
            lib.fluid_problem_free(self._fp)

30
31
    def adapt_mesh(self, gradmin, gradmax, lcmin, autoEpsilon) :
        lib.fluid_problem_adapt_mesh(self._fp, c_double(gradmin), c_double(gradmax), c_double(lcmin), c_bool(autoEpsilon))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
32
33
34
35
36

    def export(self, output_dir, t, it) :
        lib.fluid_problem_export(self._fp, output_dir.encode(), c_double(t), c_int(it))

    def compute_node_force(self, dt) :
37
38
        def np2c(a) :
            return c_void_p(a.ctypes.data)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
39
        forces = numpy.ndarray([self.n_particles,2],"d")
40
        lib.fluid_problem_compute_node_particle_force(self._fp, c_double(dt), np2c(forces))
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
41
42
43
44
45
46
        return forces

    def implicit_euler(self, dt) :
        lib.fluid_problem_implicit_euler(self._fp, c_double(dt))

    def set_particles(self, mass, volume, position, velocity):
47
48
        def np2c(a) :
            return c_void_p(numpy.ascontiguousarray(a).ctypes.data)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
49
        self.n_particles = mass.shape[0]
50
        lib.fluid_problem_set_particles(self._fp,c_int(mass.shape[0]),np2c(mass),np2c(volume),np2c(position),np2c(velocity))