5@brief Demo 2d Langevin simulation with periodic grid topology.
11from numpy.typing
import NDArray
15 bold =
lambda str: (
"\033[1m" + str +
"\033[0m")
18 print(bold(f
"dplvn version: {dplvn.__version__}"))
22 linear=1.0, quadratic=2.0, diffusion=0.1, noise=1.0,
29 grid_dimension=dplvn.D2,
32 grid_topologies=(dplvn.PERIODIC, dplvn.PERIODIC,),
34 dplvn.FLOATING, dplvn.FLOATING, dplvn.FLOATING, dplvn.FLOATING
36 bc_values=(0, 0, 0, 0,),
37 initial_condition=dplvn.RANDOM_UNIFORM,
38 integration_method=dplvn.RUNGE_KUTTA
41 if not sim.initialize():
42 raise Exception(
"Failed to initialize sim")
43 n_epochs: int = sim.get_n_epochs()
45 print(f
"Number of sim epochs = {n_epochs}")
49 n_segment_epochs: int = (n_epochs-1) // n_segments
50 if (n_segment_epochs*n_segments+1)!=n_epochs:
52 f
"Failed to segment sim with {n_epochs} epochs "
53 +
"into {n_segments} segment(s)"
56 print(bold(f
"Integrating: {n_epochs} epochs in {n_segments} segment(s)"))
59 density_dict: dict[float, NDArray] = {}
60 for i_segment
in range(0, n_segments+1, 1):
61 if i_segment>0
and not sim.run(n_segment_epochs):
62 raise Exception(
"Failed to run sim")
63 if not sim.postprocess():
64 raise Exception(
"Failed to process sim results")
65 i_epoch = sim.get_i_current_epoch()
66 t_epoch = np.round(sim.get_t_current_epoch())
67 density_dict[t_epoch] = sim.get_density()
69 f
"segment={i_segment}/{n_segments} "
70 + f
"i={i_epoch} t={t_epoch}"
74 print(
"cell density grid:")
75 print(np.round(density_dict[t_epoch].T, 2))
78if __name__ ==
"__main__":