Langevin C++ source

Loading...
Searching...
No Matches
dp_periodic.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3"""!
4@file dp_periodic.py
5@brief Demo 2d Langevin simulation with periodic grid topology.
6"""
7
8# import sys, os
9# sys.path.insert(0, os.path.join(os.path.pardir, "build"))
10 import numpy as np
11 from numpy.typing import NDArray
12 from lvn.dp import dplvn # type: ignore
13
14 def main() -> None:
15 bold = lambda str: ("\033[1m" + str + "\033[0m")
16
17 print()
18 print(bold(f"dplvn version: {dplvn.__version__}"))
19 print()
20
21 sim = dplvn.SimDP(
22 linear=1.0, quadratic=2.0, diffusion=0.1, noise=1.0,
23 t_final=20.0-1e-10,
24 # t_final=1e4-1e-10,
25 dx=0.5, dt=0.01,
26 random_seed=1,
27 # grid_dimension=dplvn.D1,
28 # grid_size=(4096,),
29 grid_dimension=dplvn.D2,
30 grid_size=(12,8,),
31 # grid_size=(40,20,),
32 grid_topologies=(dplvn.PERIODIC, dplvn.PERIODIC,),
33 boundary_conditions=(
34 dplvn.FLOATING, dplvn.FLOATING, dplvn.FLOATING, dplvn.FLOATING
35 ),
36 bc_values=(0, 0, 0, 0,),
37 initial_condition=dplvn.RANDOM_UNIFORM,
38 ic_values=(0, 1,),
39 integration_method=dplvn.RUNGE_KUTTA,
40 do_verbose=True,
41 )
42
43 if not sim.initialize(5):
44 raise Exception("Failed to initialize sim")
45 n_epochs: int = sim.get_n_epochs()
46 print()
47 print(f"Number of sim epochs = {n_epochs}")
48 print()
49
50 n_segments: int = 5
51 n_segment_epochs: int = (n_epochs-1) // n_segments
52 if (n_segment_epochs*n_segments+1)!=n_epochs:
53 raise Exception(
54 f"Failed to segment sim with {n_epochs} epochs "
55 + "into {n_segments} segment(s)"
56 )
57
58 print(bold(f"Integrating: {n_epochs} epochs in {n_segments} segment(s)"))
59 print()
60
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()
70 print(bold(
71 f"segment={i_segment}/{n_segments} "
72 + f"i={i_epoch} t={t_epoch}"
73 ))
74 # print(f"t epochs: {sim.get_t_epochs()}")
75 # print(f"mean densities: {sim.get_mean_densities()}")
76 print("cell density grid:")
77 print(np.round(density_dict[t_epoch].T, 2))
78 print(flush=True)
79
80 if __name__ == "__main__":
81 main()