Langevin C++ source

Loading...
Searching...
No Matches
dp_mixed.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2
3"""!
4@file dp_mixed.py
5@brief Demo 2d Langevin simulation with mixed grid topology and boundary conditions.
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=30.0-1e-10,
24 # t_final=300.0-1e-8,
25 dx=0.5, dt=0.01,
26 random_seed=2,
27 grid_dimension=dplvn.D2,
28 grid_size=(12,8,),
29 # grid_size=(100,60,),
30 grid_topologies=(dplvn.BOUNDED, dplvn.PERIODIC,),
31 # boundary_condition=dplvn.FLOATING,
32 boundary_conditions=(
33 # dplvn.FLOATING, dplvn.FLOATING, dplvn.FLOATING, dplvn.FLOATING,
34 # dplvn.FIXED_VALUE, dplvn.FIXED_VALUE, dplvn.FLOATING, dplvn.FLOATING,
35 dplvn.FIXED_FLUX, dplvn.FIXED_FLUX, dplvn.FLOATING, dplvn.FLOATING,
36 ),
37 # bc_values=(-0, +2, 0, 0,),
38 bc_values=(-1e+1, +1e+1, 0, 0,),
39 initial_condition=dplvn.RANDOM_UNIFORM,
40 ic_values=(0, 1,),
41 integration_method=dplvn.RUNGE_KUTTA,
42 do_verbose=True,
43 )
44
45 if not sim.initialize(5):
46 raise Exception("Failed to initialize sim")
47 n_epochs: int = sim.get_n_epochs()
48 print()
49 print(f"Number of sim epochs = {n_epochs}")
50 print()
51
52 n_segments: int = 5
53 n_segment_epochs: int = (n_epochs-1) // n_segments
54 if (n_segment_epochs*n_segments+1)!=n_epochs:
55 raise Exception(
56 f"Failed to segment sim with {n_epochs} epochs "
57 + "into {n_segments} segment(s)"
58 )
59
60 print(bold(f"Integrating: {n_epochs} epochs in {n_segments} segment(s)"))
61 print()
62
63 density_dict: dict[float, NDArray] = {}
64 for i_segment in range(0, n_segments+1, 1):
65 if i_segment>0 and not sim.run(n_segment_epochs):
66 raise Exception("Failed to run sim")
67 if not sim.postprocess():
68 raise Exception("Failed to process sim results")
69 i_epoch = sim.get_i_current_epoch()
70 t_epoch = np.round(sim.get_t_current_epoch())
71 density_dict[t_epoch] = sim.get_density()
72 print(bold(
73 f"segment={i_segment}/{n_segments} "
74 + f"i={i_epoch} t={t_epoch}"
75 ))
76 # print(f"t epochs: {sim.get_t_epochs()}")
77 # print(f"mean densities: {sim.get_mean_densities()}")
78 print("cell density grid:")
79 print(np.round(density_dict[t_epoch].T, 2))
80 print(flush=True)
81
82 if __name__ == "__main__":
83 main()