5@brief Demo 2d Langevin simulation with mixed grid topology and boundary conditions.
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,
27 grid_dimension=dplvn.D2,
30 grid_topologies=(dplvn.BOUNDED, dplvn.PERIODIC,),
35 dplvn.FIXED_FLUX, dplvn.FIXED_FLUX, dplvn.FLOATING, dplvn.FLOATING,
38 bc_values=(-1e+1, +1e+1, 0, 0,),
39 initial_condition=dplvn.RANDOM_UNIFORM,
40 integration_method=dplvn.RUNGE_KUTTA
43 if not sim.initialize():
44 raise Exception(
"Failed to initialize sim")
45 n_epochs: int = sim.get_n_epochs()
47 print(f
"Number of sim epochs = {n_epochs}")
51 n_segment_epochs: int = (n_epochs-1) // n_segments
52 if (n_segment_epochs*n_segments+1)!=n_epochs:
54 f
"Failed to segment sim with {n_epochs} epochs "
55 +
"into {n_segments} segment(s)"
58 print(bold(f
"Integrating: {n_epochs} epochs in {n_segments} segment(s)"))
61 density_dict: dict[float, NDArray] = {}
62 for i_segment
in range(0, n_segments+1, 1):
63 if i_segment>0
and not sim.run(n_segment_epochs):
64 raise Exception(
"Failed to run sim")
65 if not sim.postprocess():
66 raise Exception(
"Failed to process sim results")
67 i_epoch = sim.get_i_current_epoch()
68 t_epoch = np.round(sim.get_t_current_epoch())
69 density_dict[t_epoch] = sim.get_density()
71 f
"segment={i_segment}/{n_segments} "
72 + f
"i={i_epoch} t={t_epoch}"
76 print(
"cell density grid:")
77 print(np.round(density_dict[t_epoch].T, 2))
80if __name__ ==
"__main__":