Skip to content

Commit

Permalink
added 2D version of schrodinger equation simulation
Browse files Browse the repository at this point in the history
fixes #3
  • Loading branch information
azimonti committed Aug 12, 2024
1 parent 9e50650 commit ffc9bd9
Show file tree
Hide file tree
Showing 7 changed files with 623 additions and 43 deletions.
73 changes: 53 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,6 @@ def create_potential(x):
f"Potential {p.potential} not implemented")
```

## Getting Started

To get started with these simulations:
1. Clone the repository:
```
git clone https://github.com/azimonti/schrodinger-equation-simulation.git
```
2. Navigate to the repository directory:
```
cd schrodinger-equation-simulation
```
3. Install required dependencies:
```
pip install -r requirements.txt
```
4. Run the simulation scripts:
```
python schrodinger_1d.py
```

Some example configurations are available in the `examples_1d` directory.

## Visualization Options
Expand Down Expand Up @@ -114,6 +94,59 @@ Harmonic oscillator single eigensolution for `n=3` showing no change in the wave

https://github.com/user-attachments/assets/a62f3526-ee19-4d3e-bbe9-223830be8f4f

# Schrödinger Equation Simulation - 2D

Numerical simulation of the Schrödinger equation in 2D.

## Features

- **Potential Types**:
- Free space
- Particle in a box
- Particle in a box with barrier

- **Wavefunction Initialization**:
- Wavepackets

- **Visualization**:
- Probability density
- Modulus and phase (color phase)

- **Time Evolution Methods**:
- Implicit Crank-Nicolson (second-order)

- **Units**:
- Normalized units

Some example configurations are available in the `examples_2d` directory.

## Example Results

Particle in a box a wavefunction:

Particle in a box with barrier probability density:

# Getting Started

To get started with these simulations:
1. Clone the repository:
```
git clone https://github.com/azimonti/schrodinger-equation-simulation.git
```
2. Navigate to the repository directory:
```
cd schrodinger-equation-simulation
```
3. Install required dependencies:
```
pip install -r requirements.txt
```
4. Run the simulation scripts:
```
python schrodinger_1d.py
python schrodinger_2d.py
```

# Contributing

Contributions to the simulation of Schrödinger project are welcome. Whether it's through submitting bug reports, proposing new features, or contributing to the code, your help is appreciated. For major changes, please open an issue first to discuss what you would like to change.
Expand Down
46 changes: 46 additions & 0 deletions examples_2d/wavepackets/fully_reflecting_barrier.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
p2 = SimpleNamespace(
# low resolution
Nx_lr=200, # number of grid points in x direction
Ny_lr=200, # number of grid points in y direction
dt_lr=0.01, # time step for the simulation
# high resolution
Nx_hr=400, # number of grid points in x direction
Ny_hr=400, # number of grid points in y direction
dt_hr=0.005, # time step for the simulation
x_min=-20, # minimum x value
x_max=20, # maximum x value
y_min=-15, # minimum y value
y_max=15, # maximum y value
x0=-15, # initial position of the wave packet in x
y0=0, # initial position of the wave packet in y
sigma_x=2.0, # width of the wave packet in x direction
sigma_y=2.0, # width of the wave packet in y direction
kx=50.0, # initial wave vector in x direction
ky=0.0, # initial wave vector in y direction
t_max=10, # maximum simulation time
barrier_height=1e5, # height of the finite barrier in the middle
barrier_width=2, #half width in the x direction
total_duration=13, # total duration of the animation
fps=30 # frames per second for the animation
)


cfg = SimpleNamespace(
plot_prob=True, # plot probability or wavefunction
dev_simul=False, # align the number of steps to number of frames
infinite_barrier=True, # reflecting or absorbing boundary
middle_barrier=True, # set a finite barrier at x/2
periodic_boundary=False, # periodic boundary
high_res_grid=False, # enable high resolution simulation grid
high_res_dt=False, # enable high resolution simulation timestep
high_res_plot=True, # enable high resolution simulation plot
plot=True, # enable plotting
compute=True, # enable computations
animate=True, # enable animations
save_anim=True, # save animations
load_data=False, # load data from a file
save_data=True, # save data to a file
data_folder='data/simul', # folder for data files
animation_format='mp4', # animation format (mp4 or gif)
verbose=True
)
48 changes: 48 additions & 0 deletions examples_2d/wavepackets/no_barrier.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
p2 = SimpleNamespace(
# low resolution
Nx_lr=200, # number of grid points in x direction
Ny_lr=200, # number of grid points in y direction
dt_lr=0.01, # time step for the simulation
# high resolution
Nx_hr=400, # number of grid points in x direction
Ny_hr=400, # number of grid points in y direction
dt_hr=0.005, # time step for the simulation
x_min=-20, # minimum x value
x_max=20, # maximum x value
y_min=-15, # minimum y value
y_max=15, # maximum y value
x0=-15, # initial position of the wave packet in x
y0=0, # initial position of the wave packet in y
sigma_x=2.0, # width of the wave packet in x direction
sigma_y=2.0, # width of the wave packet in y direction
kx=-50.0, # initial wave vector in x direction
ky=0.0, # initial wave vector in y direction
t_max=10, # maximum simulation time
barrier_height=1e2, # height of the finite barrier in the middle
barrier_width=0.1, #half width in the x direction
total_duration=10, # total duration of the animation
fps=30 # frames per second for the animation
)


cfg = SimpleNamespace(
plot_prob=False, # plot probability or wavefunction
dev_simul=False, # align the number of steps to number of frames
infinite_barrier=True, # reflecting or absorbing boundary
middle_barrier= False, # set a finite barrier at x/2
periodic_boundary=False, # periodic boundary
high_res_grid=False, # enable high resolution simulation grid
high_res_dt=False, # enable high resolution simulation timestep
high_res_plot=True, # enable high resolution simulation plot
plot=True, # enable plotting
compute=True, # enable computations
animate=True, # enable animations
save_anim=True, # save animations
load_data=False, # load data from a file
save_data=True, # save data to a file
data_folder='data/simul', # folder for data files
animation_format='mp4', # animation format (mp4 or gif)
verbose=True
)


46 changes: 46 additions & 0 deletions examples_2d/wavepackets/semi_reflecting_barrier.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
p2 = SimpleNamespace(
# low resolution
Nx_lr=200, # number of grid points in x direction
Ny_lr=200, # number of grid points in y direction
dt_lr=0.01, # time step for the simulation
# high resolution
Nx_hr=400, # number of grid points in x direction
Ny_hr=400, # number of grid points in y direction
dt_hr=0.005, # time step for the simulation
x_min=-20, # minimum x value
x_max=20, # maximum x value
y_min=-15, # minimum y value
y_max=15, # maximum y value
x0=-15, # initial position of the wave packet in x
y0=0, # initial position of the wave packet in y
sigma_x=2.0, # width of the wave packet in x direction
sigma_y=2.0, # width of the wave packet in y direction
kx=50.0, # initial wave vector in x direction
ky=0.0, # initial wave vector in y direction
t_max=15, # maximum simulation time
barrier_height=47.0, # height of the finite barrier in the middle
barrier_width=0.4, # half width in the x direction
total_duration=13, # total duration of the animation
fps=30 # frames per second for the animation
)


cfg = SimpleNamespace(
plot_prob=True, # plot probability or wavefunction
dev_simul=False, # align the number of steps to number of frames
infinite_barrier=False, # reflecting or absorbing boundary
middle_barrier=True, # set a finite barrier at x/2
periodic_boundary=False, # periodic boundary
high_res_grid=False, # enable high resolution simulation grid
high_res_dt=False, # enable high resolution simulation timestep
high_res_plot=True, # enable high resolution simulation plot
plot=False, # enable plotting
compute=True, # enable computations
animate=True, # enable animations
save_anim=True, # save animations
load_data=False, # load data from a file
save_data=True, # save data to a file
data_folder='data/simul', # folder for data files
animation_format='mp4', # animation format (mp4 or gif)
verbose=True
)
62 changes: 62 additions & 0 deletions mod_config_2d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3
'''
/************************/
/* mod_config_2d.py */
/* Version 1.0 */
/* 2024/08/12 */
/************************/
'''
import sys
from types import SimpleNamespace

p2 = SimpleNamespace(
# low resolution
Nx_lr=200, # number of grid points in x direction
Ny_lr=200, # number of grid points in y direction
dt_lr=0.01, # time step for the simulation
# high resolution
Nx_hr=400, # number of grid points in x direction
Ny_hr=400, # number of grid points in y direction
dt_hr=0.005, # time step for the simulation
x_min=-20, # minimum x value
x_max=20, # maximum x value
y_min=-15, # minimum y value
y_max=15, # maximum y value
x0=-15, # initial position of the wave packet in x
y0=0, # initial position of the wave packet in y
sigma_x=2.0, # width of the wave packet in x direction
sigma_y=2.0, # width of the wave packet in y direction
kx=50.0, # initial wave vector in x direction
ky=0.0, # initial wave vector in y direction
t_max=15, # maximum simulation time
barrier_height=47.0, # height of the finite barrier in the middle
barrier_width=0.4, # half width in the x direction
total_duration=13, # total duration of the animation
fps=30 # frames per second for the animation
)


cfg = SimpleNamespace(
plot_prob=True, # plot probability or wavefunction
dev_simul=False, # align the number of steps to number of frames
infinite_barrier=False, # reflecting or absorbing boundary
middle_barrier=True, # set a finite barrier at x/2
periodic_boundary=False, # periodic boundary
high_res_grid=False, # enable high resolution simulation grid
high_res_dt=False, # enable high resolution simulation timestep
high_res_plot=True, # enable high resolution simulation plot
plot=False, # enable plotting
compute=True, # enable computations
animate=True, # enable animations
save_anim=True, # save animations
load_data=False, # load data from a file
save_data=True, # save data to a file
data_folder='data/simul', # folder for data files
animation_format='mp4', # animation format (mp4 or gif)
verbose=True
)

if __name__ == '__main__':
if sys.version_info[0] < 3:
raise 'Must be using Python 3'
pass
44 changes: 21 additions & 23 deletions schrodinger_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,36 +524,34 @@ def make_plot(outfile: str):
with open(simul_dir + '/config.pkl', 'rb') as file:
cfg = pickle.load(file)
p = pickle.load(file)
# Do not compute if load
else:
t, psi = compute(
x, t, psi, v, params['total_duration'] * params['fps'])
# serialize data
if params['save_data']:
folder = params['data_folder']
script_dir = os.path.dirname(os.path.abspath(__file__))
simul_dir = os.path.join(script_dir, folder)
if cfg.verbose:
print(f"Saving config and data ({simul_dir})")
if not os.path.exists(simul_dir):
os.makedirs(simul_dir)
with open(simul_dir + '/config.pkl', 'wb') as file:
pickle.dump(cfg, file)
pickle.dump(p, file)
with open(simul_dir + '/data.pkl', 'wb') as file:
pickle.dump(t, file)
pickle.dump(x, file)
pickle.dump(psi, file)
pickle.dump(v, file)
# deserialize data
if params['load_data']:
if cfg.verbose:
print(f"Loading data ({simul_dir}/data.pkl)")
with open(simul_dir + '/data.pkl', 'rb') as file:
t = pickle.load(file)
x = pickle.load(file)
psi = pickle.load(file)
v = pickle.load(file)
# Do not compute or serialize if load
else:
t, psi = compute(
x, t, psi, v, params['total_duration'] * params['fps'])
# serialize data
if params['save_data']:
folder = params['data_folder']
script_dir = os.path.dirname(os.path.abspath(__file__))
simul_dir = os.path.join(script_dir, folder)
if cfg.verbose:
print(f"Saving config and data ({simul_dir})")
if not os.path.exists(simul_dir):
os.makedirs(simul_dir)
with open(simul_dir + '/config.pkl', 'wb') as file:
pickle.dump(cfg, file)
pickle.dump(p, file)
with open(simul_dir + '/data.pkl', 'wb') as file:
pickle.dump(t, file)
pickle.dump(x, file)
pickle.dump(psi, file)
pickle.dump(v, file)
plotter = MyPlotter(params, x, t, psi, v, outfile)
plotter.plot()
plotter.save_plot()
Expand Down
Loading

0 comments on commit ffc9bd9

Please sign in to comment.