Skip to content

Commit

Permalink
Merge pull request #44 from viljarjf/dev
Browse files Browse the repository at this point in the history
v0.1.2
  • Loading branch information
viljarjf authored Jun 10, 2023
2 parents c4dd608 + 5cb4cc4 commit 1c24b02
Show file tree
Hide file tree
Showing 13 changed files with 213 additions and 107 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# QM_sim

Python library for simulation of quantum mechanical systems.
Python library for simulation of quantum mechanical systems. The documentation is available on [GitHub pages](https://viljarjf.github.io/QM_sim/).

[![Build docs](https://github.com/viljarjf/QM_sim/actions/workflows/build_docs.yml/badge.svg)](https://github.com/viljarjf/QM_sim/actions/workflows/build_docs.yml)

Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
project = 'QM sim'
copyright = '2023, Viljar Femoen'
author = 'Viljar Femoen'
release = '0.1.1'
release = '0.1.2'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down
44 changes: 0 additions & 44 deletions docs/source/examples.rst

This file was deleted.

1 change: 0 additions & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ A python library for simulation of quantum mechanical systems.

usage
api
examples
contribution

Indices and tables
Expand Down
51 changes: 50 additions & 1 deletion docs/source/usage.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Usage
=====
========

Installation
------------
Expand All @@ -10,3 +10,52 @@ To use QM sim, first install it using pip:
$ pip install qm-sim
To run calculations on a GPU, install the PyTorch version for your system at the
`PyTorch website <https://pytorch.org/get-started/locally/>`_ as well


Examples
--------

No potential
------------

.. literalinclude:: ../../examples/01_zero_potential.py
:language: python

Quadratic potential
-------------------

.. literalinclude:: ../../examples/02_quadratic_potential.py
:language: python

2D system
---------

.. literalinclude:: ../../examples/03_2D_potential.py
:language: python

Adiabatic evolution
-------------------

.. literalinclude:: ../../examples/04_adiabatic_evolution.py
:language: python

Temporal evolution
------------------

.. literalinclude:: ../../examples/05_temporal_evolution.py
:language: python

Temporal evolution of a 2D system
---------------------------------

.. literalinclude:: ../../examples/06_2D_temporal_evolution.py
:language: python

Hydrogen atom
-------------

.. literalinclude:: ../../examples/07_hydrogen_atom.py
:language: python
2 changes: 1 addition & 1 deletion examples/02_quadratic_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
H = Hamiltonian(N, L, m)

# Set potential
x = np.linspace(-L/2, L/2, N)
x, = H.get_coordinate_arrays()

def V(t):
k = 200
Expand Down
3 changes: 1 addition & 2 deletions examples/04_adiabatic_evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
H = Hamiltonian(N, L, m)

# Set potential. Here, a square well is used, with dV = 0.15 eV
x,y = np.linspace(-L[0]/2,L[0]/2, N[0]), np.linspace(-L[0]/2,L[0]/2, N[1])
X, Y = np.meshgrid(x,y)
X, Y = H.get_coordinate_arrays()

# Smoothed rectangular time-dependent potential, assymetric to avoid degeneracy
def V(t):
Expand Down
2 changes: 1 addition & 1 deletion examples/05_temporal_evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
H = Hamiltonian(N, L, m, temporal_scheme="leapfrog")

# Set the potential to a quadratic potential oscilating from side to side
z = np.linspace(-L/2, L/2, N)
z, = H.get_coordinate_arrays()
H.V = lambda t: 6*z**2 + 3*z*np.abs(z)*np.sin(4e15*t)

# Plot
Expand Down
4 changes: 1 addition & 3 deletions examples/06_2D_temporal_evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
# Set the potential. Use a cool elipse cross that rotates over time
a = 2e-9
b = 5e-9
x = np.linspace(-L[0]/2, L[0]/2, N[0])
y = np.linspace(-L[1]/2, L[1]/2, N[1])
X, Y = np.meshgrid(x, y)
X, Y = H.get_coordinate_arrays()
def V(theta: float) -> np.ndarray:
ct, st = np.cos(theta), np.sin(theta)

Expand Down
2 changes: 1 addition & 1 deletion examples/07_hydrogen_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
H = Hamiltonian(N, L, m)

# Define the potential: -e^2 / (4*pi*𝜀_0*r)
x, y, z = np.meshgrid(*(np.linspace(-L[i]/2, L[i]/2, N[i]) for i in range(3)))
x, y, z = H.get_coordinate_arrays()
r = (x**2 + y**2 + z**2)**0.5
H.V = -e_0**2 / (4 * np.pi * 𝜀_0 * r)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "qm_sim"
version = "0.1.1"
version = "0.1.2"
authors = [
{ name="Viljar Femoen", email="[email protected]" },
]
Expand Down
55 changes: 24 additions & 31 deletions src/qm_sim/hamiltonian/spatial_hamiltonian.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""

from collections.abc import Iterable
from typing import Any, Callable

import numpy as np
Expand All @@ -16,7 +15,11 @@
from ..eigensolvers import get_eigensolver
from ..nature_constants import h_bar
from ..spatial_derivative import get_scheme_order
from ..spatial_derivative.cartesian import laplacian, nabla
from ..spatial_derivative.cartesian import (
CartesianDiscretization,
laplacian,
nabla,
)
from ..temporal_solver import TemporalSolver, get_temporal_solver


Expand Down Expand Up @@ -84,29 +87,11 @@ def __init__(
Defaults to "zero"
:type boundary_condition: str, optional
"""
# 1D inputs
if isinstance(N, int):
N = (N,)
if isinstance(L, (float, int)):
L = (L,)

# Allow any iterable that can be converted to a tuple
if isinstance(N, Iterable):
N = tuple(N)
if isinstance(L, Iterable):
L = tuple(L)

# Check type
if not isinstance(N, tuple) or not all(isinstance(i, int) for i in N):
raise ValueError(f"Param `N` must be int or tuple of ints, got {type(N)}")
if not isinstance(L, tuple) or not all(isinstance(i, (float, int)) for i in L):
raise ValueError(f"Param `L` must be float or tuple, got {type(L)}")

if len(N) != len(L):
raise ValueError("`N`and `L`must have same length")

self.N = N
self.L = L
# Creating this object performs the necessary type checking
self.discretization = CartesianDiscretization(L, N)
self.N = self.discretization.N
self.L = self.discretization.L
self.deltas = self.discretization.dx

self.eigensolver = get_eigensolver(eigensolver)
if self.eigensolver is None:
Expand All @@ -116,9 +101,6 @@ def __init__(
if order is None:
raise ValueError("Requested finite difference is invalid")

self._dim = len(N)
self.delta = [Li / Ni for Li, Ni in zip(L, N)]

# Handle non-isotropic effective mass
if isinstance(m, np.ndarray) and np.all(m == m.flat[0]):
m = m.flatten()[0]
Expand All @@ -130,16 +112,20 @@ def __init__(
)
m_inv = 1 / m.flatten()

_n = nabla(N, L, order=order, boundary_condition=boundary_condition)
_n2 = laplacian(N, L, order=order, boundary_condition=boundary_condition)
_n = nabla(
self.discretization, order=order, boundary_condition=boundary_condition
)
_n2 = laplacian(
self.discretization, order=order, boundary_condition=boundary_condition
)

# nabla m_inv nabla + m_inv nabla^2
_n.data *= _n @ m_inv # First term
_n2.data *= m_inv # Second term
self.mat = _n + _n2
else:
self.mat = laplacian(
N, L, order=order, boundary_condition=boundary_condition
self.discretization, order=order, boundary_condition=boundary_condition
)
self.mat *= 1 / m

Expand Down Expand Up @@ -427,3 +413,10 @@ def asarray(self) -> np.ndarray:
:rtype: np.ndarray
"""
return self.mat.toarray()

def get_coordinate_arrays(self) -> tuple[np.ndarray]:
return self.discretization.get_coordinate_arrays()

get_coordinate_arrays.__doc__ = (
CartesianDiscretization.get_coordinate_arrays.__doc__
)
Loading

0 comments on commit 1c24b02

Please sign in to comment.