Source

enzo-3.0 / src / enzo / io / python_bridge / message_passing.pyx

The active_particles branch has multiple heads

Full commit
"""
Author: Matthew Turk <matthewturk@gmail.com>
Affiliation: Columbia University
Homepage: http://enzo-project.org/
License: This file is covered under the Enzo license
"""

import numpy as np
cimport numpy as np
cimport cython
cimport libc.stdlib
from libcpp.string cimport string
from cython.operator cimport dereference as deref

from enzo_includes cimport *

cdef extern from "EventHooks.h":
    void RunEventHooks(string,
                       c_HierarchyEntry **Grids,
                       c_TopGridData &MetaData,
                       EventDataContainer *LocalData)

cdef extern from "EventDataContainers.h":
    cdef cppclass EventDataContainer:
        pass

    cdef cppclass TestEventDataContainer:
        int SomethingToPrint

    cdef cppclass PreGalaxyParticleDataContainer:
        int num_to_insert
        double *center[3]
        double *velocity[3]
        double *dens
        double *radius

cdef class MessageCoordinator:
    cdef c_HierarchyEntry **Grids
    cdef c_TopGridData MetaData

    def issue_test_event(self, value_to_print):
        cdef TestEventDataContainer *pedc = new TestEventDataContainer()
        pedc.SomethingToPrint = value_to_print
        cdef EventDataContainer *data = <EventDataContainer *> pedc
        RunEventHooks("Python Event".encode("UTF-8"), self.Grids, self.MetaData, data)
        del pedc

    def get_galaxy_particles(self, num_to_insert, input_cen, input_vel,
            input_radius, input_dens):
        cdef PreGalaxyParticleDataContainer *pgpdc = new PreGalaxyParticleDataContainer()
        # Make some buffers.
        cdef np.ndarray cen0 = np.empty(num_to_insert, dtype=np.float64)
        cdef np.ndarray cen1 = np.empty(num_to_insert, dtype=np.float64)
        cdef np.ndarray cen2 = np.empty(num_to_insert, dtype=np.float64)
        
        cdef np.ndarray vel0 = np.empty(num_to_insert, dtype=np.float64)
        cdef np.ndarray vel1 = np.empty(num_to_insert, dtype=np.float64)
        cdef np.ndarray vel2 = np.empty(num_to_insert, dtype=np.float64)
        
        cdef np.ndarray dens = np.empty(num_to_insert, dtype=np.float64)
        cdef np.ndarray radius = np.empty(num_to_insert, dtype=np.float64)
        # Fill the buffers.
        cen0[:] = input_cen[:, 0]
        cen1[:] = input_cen[:, 1]
        cen2[:] = input_cen[:, 2]
        
        vel0[:] = input_vel[:, 0]
        vel1[:] = input_vel[:, 1]
        vel2[:] = input_vel[:, 2]
        
        radius[:] = input_radius[:]
        dens[:] = input_dens[:]
        # Fill the class.
        pgpdc.num_to_insert = num_to_insert
        
        pgpdc.center[0] = <double *> cen0.data
        pgpdc.center[1] = <double *> cen1.data
        pgpdc.center[2] = <double *> cen2.data
        
        pgpdc.velocity[0] = <double *> vel0.data
        pgpdc.velocity[1] = <double *> vel1.data
        pgpdc.velocity[2] = <double *> vel2.data
        
        pgpdc.dens = <double *> dens.data
        pgpdc.radius = <double *> radius.data
        
        cdef EventDataContainer *data = <EventDataContainer *> pgpdc
        RunEventHooks("Get GP List".encode("UTF-8"), self.Grids, self.MetaData, data)
        del pgpdc

cdef MessageCoordinator mc = MessageCoordinator()
test_event = mc.issue_test_event
get_galaxy_particles = mc.get_galaxy_particles

cdef public void create_coordinator(c_HierarchyEntry **Grids,
                                    c_TopGridData &MetaData):
    mc.Grids = Grids
    mc.MetaData = MetaData

cdef extern from "fix_enzo_defs.h":
    pass