Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model): particle in a box #44

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions hamilflow/models/particle_in_box.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
from functools import cached_property
from typing import Dict, List, Optional, Union

import numpy as np
import pandas as pd
import scipy as sp
from pydantic import BaseModel, Field, computed_field, field_validator


PLANCK_CONST = 6.626e-34
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.scipy.org/doc/scipy/reference/constants.html#physical-constants

Alternatively we may let the use decide the unit. Tiny numbers can become impractical.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, too small numbers could be impractical. Another option is not to use planck constant explicitly. Or do planck related calculations at the very end.

PI = 3.142
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



class FiniteBox(BaseModel):
r"""Definition of the potential of the one dimensional box

For consistency, we always use $\mathbf x$ for displacement,
$L$ for box size.

The potential distribution we use is

$$
V(x)=\left\{\begin{array}{ll}
-V_{0} & |x|<L \\
0 & |x|>L
\end{array}\right.
$$

:cvar length: length of the box
:cvar p_height: potential well height
"""

length: float = Field(ge=0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ge=0.0 for being a float.

Also the length you defined here is actually half-width. It may become tricky at some point..

p_height: float = Field(g=0, default=1.0)


class QuantumParticle(BaseModel):
r"""Definition of the quantum particle

:cvar energy: energy of the particle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to set the ground energy, which is a quantity that the particle becomes unbounded at finite possibility. Something like this.

:cvar mass: weight of the particle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mass of the particle

"""

energy: float = Field(l=0, ge=-FiniteBox.p_height)
mass: float = Field(ge=0, default=1e-5)


class WaveFunctionConst(BaseModel):
r"""Schrodinger equation simplification terms"""

@property
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferably cached_property or some kinds of computed field? @emptymalei

We also have hbar in https://docs.scipy.org/doc/scipy/reference/constants.html#physical-constants

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preferably cached_property or some kinds of computed field? @emptymalei

We also have hbar in https://docs.scipy.org/doc/scipy/reference/constants.html#physical-constants

+1

  1. To serialize the properties properly in Pydantic, we need computed_field.
  2. Also better to cache it as this is used n-time-step times.

def h_bar(self) -> float:
"""The hbar simplification
"""
return PLANCK_CONST / (2 * PI)

@computed_field # type: ignore[misc]
@cached_property
def calculate_alpha(self) -> float:
"""Simplification term alpha

$$
\alpha=\sqrt{\frac{2 m}{\hbar^{2}}\left(V_{0}-|E|\right)}
$$
"""
return (
np.sqrt(
2 * QuantumParticle.mass * (FiniteBox.p_height - np.abs(QuantumParticle.energy))
/ (self.h_bar ** 2)
)
)

@computed_field # type: ignore[misc]
@cached_property
def calculate_beta(self) -> float:
"""Simplification term beta

$$
\beta=\sqrt{\frac{2 m}{\hbar^{2}}|E|}
$$
"""
return (
np.sqrt(
2 * QuantumParticle.mass * np.abs(QuantumParticle.energy) / (self.h_bar ** 2)
)
)


class ParticleInBox:
r"""Definition of the particle in a box quantum system

For consistency, we always use
$\mathbf x$ for displacement, $E$ for particle energy,
$V$ for barrier energy, and $L$ for box size.
The one-dimensional Schrödinger equation we are using is

$$
\begin{align}
-{\frac{\hbar^2}{2m}} {\frac{\partial^2 \psi}{\partial x^2}} + V(r) \psi = E \psi \label{eq1}
\end{align}
$$

And the even wave functions we are using are

$$
\psi^{F}(x)=\left\{\begin{array}{ll}
A \cos (\alpha \alpha) & 0<x<L \\
C e^{-\beta \hbar} & x>L
\end{array}\right.
$$

The odd wave functions we are using are

$$
\psi^{o}(x)=\left\{\begin{array}{ll}
A \sin (\alpha \alpha) & 0<x<L \\
C e^{-\beta_{x}} & x>L
\end{array}\right.
$$

References:

1. Particle in a box and tunneling. [cited 26 Mar 2024].
Available: https://chem.libretexts.org/Courses/University_of_California_Davis/UCD_Chem_110A%3A_Physical_Chemistry__I/UCD_Chem_110A%3A_Physical_Chemistry_I_(Koski)/Text/03%3A_The_Schrodinger_Equation/3.09%3A_Particle_in_a_Finite_Box_and_Tunneling_(optional)
2. Contributors to Wikimedia projects. Particle in a box.
In: Wikipedia [Internet]. 22 Jan 2024 [cited 26 Mar 2024].
Available: https://en.wikipedia.org/wiki/Particle_in_a_box

:param finite_box: the finite box definition
:param quantum_particle: the particle in the box
"""

def __init__(
self,
finite_box: Dict[str, float],
quantum_particle: Dict[str, float],
):
self.finite_box = FiniteBox.model_validate(finite_box)
self.quantum_particle = QuantumParticle.model_validate(quantum_particle)

def __call__(self) -> pd.DataFrame:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a brain fart, wondering what should be the variables here. Should it be the box size or particle energy or both?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I were to write the code now, I would just input $x$ or $x, t$ and give a complex probability amplitude. Or an array of probability amplitudes if $x$ were an array.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the $x$ will be the displacement anywhere the user provides?

pass

Loading