Exemplo n.º 1
0
def main():
    '''Cortix run file for a criminal justice network.

    Attributes
    ----------
    n_groups : int
        Number of population groups being followed. This must be the same for all
        modules.
    end_time: float
        End of the flow time in SI unit.
    time_step: float
        Size of the time step between port communications in SI unit.
    use_mpi: bool
        If set to `True` use MPI otherwise use Python multiprocessing.

    '''

    # Configuration Parameters
    n_groups = 10  # number of population groups
    end_time = 30 * unit.year
    time_step = 1.0 * unit.day

    use_mpi = False  # True for MPI; False for Python multiprocessing

    city = Cortix(use_mpi=use_mpi, splash=True)

    city.network = Network()

    community = Community(n_groups=n_groups,
                          non_offender_adult_population=100000,
                          offender_pool_size=0)
    city.network.module(community)
    community.end_time = end_time
    community.time_step = time_step
    community.show_time = (True, 30 * unit.day)
    community.save = True

    prison = Prison(n_groups=n_groups, pool_size=0.0)
    city.network.module(prison)
    prison.end_time = end_time
    prison.time_step = time_step
    prison.save = True

    parole = Parole(n_groups=n_groups)
    city.network.module(parole)
    parole.end_time = end_time
    parole.time_step = time_step
    parole.save = True

    adjudication = Adjudication(n_groups=n_groups)
    city.network.module(adjudication)
    adjudication.end_time = end_time
    adjudication.time_step = time_step
    adjudication.save = True

    jail = Jail(n_groups=n_groups)
    city.network.module(jail)
    jail.end_time = end_time
    jail.time_step = time_step
    jail.save = True

    arrested = Arrested(n_groups=n_groups)
    city.network.module(arrested)
    arrested.end_time = end_time
    arrested.time_step = time_step
    arrested.save = True

    probation = Probation(n_groups=n_groups)
    city.network.module(probation)
    probation.end_time = end_time
    probation.time_step = time_step
    probation.save = True

    city.network.connect(prison, parole, 'bidirectional')
    city.network.connect(adjudication, prison)
    city.network.connect(jail, prison)
    city.network.connect(adjudication, jail)
    city.network.connect(arrested, jail)
    city.network.connect(arrested, adjudication)
    city.network.connect(arrested, probation)
    city.network.connect(probation, jail)
    city.network.connect(adjudication, probation)

    city.network.connect(arrested, community, 'bidirectional')
    city.network.connect(jail, community)
    city.network.connect(probation, community)
    city.network.connect(adjudication, community)
    city.network.connect(prison, community)
    city.network.connect(parole, community)

    city.network.draw()

    city.run()

    if city.use_multiprocessing or city.rank == 0:

        total_num_unknowns = n_groups * len(city.network.modules)
        total_num_params = 0

        # Inspect Data Function
        def inspect_module_data(module, quant_name):
            population_phase = module.population_phase
            (fxg_quant,
             time_unit) = population_phase.get_quantity_history(quant_name)

            fxg_quant.plot(x_scaling=1 / unit.year,
                           x_label='Time [y]',
                           y_label=fxg_quant.latex_name + ' [' +
                           fxg_quant.unit + ']')

            # Number of parameters in the prison model
            n_params = (len(population_phase.GetActors()) - 1) * n_groups
            return n_params

        quant_names = {
            'Prison': 'npg',
            'Parole': 'feg',
            'Adjudication': 'f*g',
            'Jail': 'fjg',
            'Arrested': 'frg',
            'Probation': 'fbg',
            'Community': 'f0g'
        }

        for m in city.network.modules:
            quant_name = quant_names[m.name]
            total_num_params += inspect_module_data(m, quant_name)
            plt.grid()
            plt.savefig(m.name + '.png', dpi=300)
            if m.name == 'Community':
                inspect_module_data(m, 'n0')
                plt.grid()
                plt.savefig(m.name + '-n0.png', dpi=300)

        # Total number of unknowns and parameters

        city.log.info('total number of unknowns   =' + str(total_num_unknowns))
        city.log.info('total number of parameters =' + str(total_num_params))

    # Properly shutdow city
    city.close()
Exemplo n.º 2
0
        print("Finished sending!")

if __name__ == "__main__":

    # Cortix built-in DataPlot module
    d = DataPlot()
    #d.set_title("Test Plot")
    #d.set_xlabel("Time")
    #d.set_ylabel("Position")

    d.title = 'Test Plot'
    d.xlabel = 'Time'
    d.ylabel = 'Position'

    # Custom class to send dummy data
    p = PlotData()

    p1 = Port("plot-in")
    p2 = Port("plot-out")
    p1.connect(p2)

    d.ports.append(p1)
    p.ports.append(p2)

    c = Cortix()
    c.network = Network()
    c.network.add_module(d)
    c.network.add_module(p)
    c.use_mpi = False
    c.run()
Exemplo n.º 3
0
def main():
    '''Cortix run file for a `Droplet`-`Vortex` network.

    Attributes
    ----------
    n_droplets: int
        Number of droplets to use (one per process).
    end_time: float
        End of the flow time in SI unit.
    time_step: float
        Size of the time step between port communications in SI unit.
    create_plots: bool
        Create various plots and save to files. (all data collected in the
        parent process; it may run out of memory).
    plot_vortex_profile: bool
        Whether to plot (to a file) the vortex function used.
    use_mpi: bool
        If set to `True` use MPI otherwise use Python multiprocessing.

    '''

    # Configuration Parameters
    n_droplets = 5
    end_time   = 3*const.minute
    time_step  = 0.2

    create_plots = True

    if n_droplets >= 2000:
        create_plots = False

    plot_vortex_profile = False # True may crash the X server.

    use_mpi = False # True for MPI; False for Python multiprocessing

    swirl = Cortix(use_mpi=use_mpi, splash=True)

    swirl.network = Network()

    # Vortex module (single).
    vortex = Vortex()
    swirl.network.module(vortex)
    vortex.show_time = (True,1*const.minute)
    vortex.end_time = end_time
    vortex.time_step = time_step
    if plot_vortex_profile:
        vortex.plot_velocity()

    for i in range(n_droplets):

        # Droplet modules (multiple).
        droplet = Droplet()
        swirl.network.module(droplet)
        droplet.end_time = end_time
        droplet.time_step = time_step
        droplet.bounce = False
        droplet.slip = False
        droplet.save = True

        # Network port connectivity (connect modules through their ports)
        swirl.network.connect( [droplet,'external-flow'],
                               [vortex,vortex.get_port('fluid-flow:{}'.format(i))],
                               'bidirectional' )

    swirl.network.draw()

    swirl.run()

    # Plot all droplet trajectories
    if create_plots:

        modules = swirl.network.modules

        if swirl.use_multiprocessing or swirl.rank == 0:

            # All droplets' trajectory

            from mpl_toolkits.mplot3d import Axes3D
            import matplotlib.pyplot as plt

            positions = list()
            for m in swirl.network.modules[1:]:
                positions.append(m.liquid_phase.get_quantity_history('position')[0].value)

            fig = plt.figure(1)
            ax = fig.add_subplot(111,projection='3d')
            ax.set_title('Droplet Trajectories')
            ax.set_xlabel('x')
            ax.set_ylabel('y')
            ax.set_zlabel('z')
            for pos in positions:
                x = [i[0] for i in pos]
                y = [i[1] for i in pos]
                z = [i[2] for i in pos]
                ax.plot(x, y, z)
            fig.savefig('trajectories.png', dpi=300)

            # All droplets' speed
            fig = plt.figure(2)
            plt.xlabel('Time [min]')
            plt.ylabel('Speed [m/s]')
            plt.title('All Droplets')

            for m in modules[1:]:
                speed = m.liquid_phase.get_quantity_history('speed')[0].value
                plt.plot(list(speed.index/60), speed.tolist())

            plt.grid()
            fig.savefig('speeds.png', dpi=300)

            # All droplets' radial position
            fig = plt.figure(3)
            plt.xlabel('Time [min]')
            plt.ylabel('Radial Position [m]')
            plt.title('All Droplets')

            for m in modules[1:]:
                radial_pos = m.liquid_phase.get_quantity_history('radial-position')[0].value
                plt.plot(list(radial_pos.index/60)[1:], radial_pos.tolist()[1:])

            plt.grid()
            fig.savefig('radialpos.png', dpi=300)

    # This properly ends the program
    swirl.close()
Exemplo n.º 4
0
def main():
    um = False
    sim_time = 365 * 24 * 3600  #157788000.0 * 10
    time_step = 24 * 3600  #25000.0

    universe_rad = 2.5e11

    cortix = Cortix(use_mpi=um)
    cortix.network = Network()

    earth_mass = 5.9740e+24
    earth_pos = np.array([1.4960e+11, 0.0, 0.0])
    earth_vel = np.array([(0.0, 2.9800e+04, 0.0)])
    earth = Body(earth_mass, earth_pos, earth_vel)
    earth.name = "earth"
    cortix.network.module(earth)

    mars_mass = 6.4190e+23
    mars_pos = np.array([2.2790e+11, 0.0, 0.0])
    mars_vel = np.array([0.0, 2.4100e+04, 0.0])
    mars = Body(mars_mass, mars_pos, mars_vel)
    mars.name = "mars"
    cortix.network.module(mars)

    mercury_mass = 3.3020e+23
    mercury_pos = np.array([5.7900e+10, 0.0, 0.0])
    mercury_vel = np.array([0.0, 4.7900e+04, 0.0])
    mercury = Body(mercury_mass, mercury_pos, mercury_vel)
    mercury.name = "mercury"
    cortix.network.module(mercury)

    venus_mass = 4.8690e+24
    venus_pos = np.array([1.0820e+11, 0.0, 0.0])
    venus_vel = np.array([0.0, 3.5000e+04, 0.0])
    venus = Body(venus_mass, venus_pos, venus_vel)
    venus.name = "venus"
    cortix.network.module(venus)

    sun_mass = 1.9890e+30
    sun_pos = np.array([0.0, 0.0, 0.0])
    sun_vel = np.array([0.0, 0.0, 0.0])
    sun = Body(sun_mass, sun_pos, sun_vel)
    sun.name = "sun"
    cortix.network.module(sun)

    for x, i in enumerate(cortix.network.modules):
        i.save = True
        i.dt = time_step
        i.time = sim_time
        for y, j in enumerate(cortix.network.modules):
            pi = Port("body_{}".format(y), um)

            if x != y and pi not in i.ports:
                i.ports.append(pi)

                pj = Port("body_{}".format(x), um)
                j.ports.append(pj)

                pj.connect(pi)
                cortix.network.gv_edges.append((str(x), str(y)))

    cortix.run()
    cortix.network.draw()

    return [(b.name, b.trajectory) for b in cortix.network.modules]