Random field actor

Demonstrates a custom actor class that sets a field to random values

random field actor
  0%|          | 0/10.0 [00:00<?, ?it/s]
Initializing:   0%|          | 0/10.0 [00:00<?, ?it/s]
  0%|          | 0/10.0 [00:00<?, ?it/s]
 10%|█         | 1.0/10.0 [00:00<00:03,  2.87it/s]
 20%|██        | 2.0/10.0 [00:00<00:01,  5.73it/s]
 70%|███████   | 7.0/10.0 [00:00<00:00, 20.04it/s]
 70%|███████   | 7.0/10.0 [00:00<00:00, 20.04it/s]
100%|██████████| 10.0/10.0 [00:00<00:00, 28.61it/s]
100%|██████████| 10.0/10.0 [00:00<00:00, 28.60it/s]

import numba as nb
import numpy as np

from pde import UnitGrid

import emulsim


class RandomFieldActor(emulsim.ActorBase):
    """Actor that sets a new random field each time step."""

    def evolve(self, elements, t, dt):
        # mandatory python implementation of the background evolution
        (field,) = elements
        field.data = np.random.uniform(0, 0.1, field.data.shape)

    def make_evolver_numba(self, elements):
        """Implementing the compiled version is optional."""

        # this function is optional and can be used to speed up calculations
        @nb.jit
        def evolver(elements_state, t: float, dt: float):
            """Evolve the diffusion equation explicitly."""
            (field_state,) = elements_state
            for i in range(field_state.size):
                field_state.flat[i] = np.random.uniform(0, 0.1)

        return evolver  # type: ignore


# set up state
element = emulsim.ScalarFieldElement(parameters={"grid": UnitGrid([32, 32])})
state = emulsim.State({"field": element})

# set up simulation
simulation = emulsim.Simulation(state)
simulation.add_actor("field", RandomFieldActor())

# run simulation
result = simulation.run(t_range=10)

result.plot()

Total running time of the script: (0 minutes 0.436 seconds)