Skip to content
Merged
Show file tree
Hide file tree
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
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install tox
run: pip install tox
- name: Run lint
run: tox run -e lint

test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install tox
run: pip install tox
- name: Run tests
run: tox run -e py
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ ENV/

# mypy
.mypy_cache/

# ruff
.ruff_cache/
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"."
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
8 changes: 4 additions & 4 deletions philsol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"""

from philsol.core import eigen_build
from philsol import solve
from philsol import construct

__all__ = ["eigen_build", "solve", "construct"]

"""
Contains the main maths, lifted from the Zhu paper to assemble Maxwells equations into a linear eigenvalue problem:
Expand All @@ -19,16 +23,12 @@
- Other operator can be return to help construct other coupled fields (such as E_z, H_x, H_y, H_z)
"""

from philsol import solve

"""
- Handles the eigen solving bit
- Nothing too complicated here at the moment it just uses standard library scipy sparse libary.
- We hope to add some more fancy eigen solving tricks at a later date (e.g SLEPc )
"""

from philsol import construct

"""
- This function is empty.
"""
22 changes: 11 additions & 11 deletions philsol/classy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Tuple, Union, List
from typing import Optional
import philsol as ps
import numpy as np

Expand Down Expand Up @@ -27,17 +27,17 @@ def __init__(self, n: np.ndarray, k0: float, x_max: Optional[float] = None, y_ma
self.P = None
self.mats = None

if x_max == None and y_max == None:
if dx != None and dy != None:
if x_max is None and y_max is None:
if dx is not None and dy is not None:
self.dx = dx
self.dy = dy
self.x = np.array(range(self.num_x)) * self.dx
self.y = np.array(range(self.num_y)) * self.dy
else:
raise Exception('Gonna need some dimensions yo!')

if dx == None and dy == None:
if x_max != None and y_max != None:
if dx is None and dy is None:
if x_max is not None and y_max is not None:
'''
self.dx = dx
self.dy = dy
Expand Down Expand Up @@ -65,7 +65,7 @@ def build_stuff(self, x_bound: Optional[str] = None, y_bound: Optional[str] = No
"""


if matrices == None:
if matrices is None:
self.P, _ = ps.core.eigen_build( self.k0,
self.n,
self.dx,
Expand Down Expand Up @@ -101,10 +101,10 @@ def solve_stuff(self, neigs: int, beta_trial: float, extra_fields: bool = False,
"""

# some insults if everything isn't set up
if self.P == None:
if self.P is None:
raise Exception("Build your eigenproblem you idiot")

if self.mats == None and extra_fields == True:
if self.mats is None and extra_fields:
raise Exception("If full vector fields are required the build_stuff step needs matrices = True")


Expand All @@ -115,7 +115,7 @@ def solve_stuff(self, neigs: int, beta_trial: float, extra_fields: bool = False,
neigs = self.Eigs
)

if extra_fields == True:
if extra_fields:

self.E = np.empty((self.Eigs, self.num_x * self.num_y, 3), dtype = complex)
self.H = np.empty_like(self.E , dtype = complex )
Expand All @@ -129,7 +129,7 @@ def solve_stuff(self, neigs: int, beta_trial: float, extra_fields: bool = False,
self.H[i, :, 1] = hy
self.H[i, :, 2] = hz

if poynting_vector == True:
if poynting_vector:
if not extra_fields:
raise Exception("Poynting vector requires full E and H fields")

Expand All @@ -155,7 +155,7 @@ def destroy_crap(self, fields: bool = False) -> None:
self.Ex = None
self.Ey = None

if fields == True:
if fields:
self.E = None
self.H = None

14 changes: 7 additions & 7 deletions philsol/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def eigen_build(k0: float, n: np.ndarray, dx: float, dy: float,
if x_boundary == 'periodic':
Ux_temp[nx-1, 0] = 1. / dx

# Wrap around periodic boundary. Something something bloch functions.
# Wrap around periodic boundary. With index = i + j*nx ordering the row
if y_boundary == 'periodic':
Uy = Uy + sps.eye(nx*ny, k= -(nx-1)*ny) / dy
Uy = Uy + sps.eye(nx*ny, k= -(ny-1)*nx) / dy


#This statement is kind of confusing but is the equivalent to doing a tensor
Expand All @@ -55,7 +55,7 @@ def eigen_build(k0: float, n: np.ndarray, dx: float, dy: float,
# Construct other differential operators
Vx = - Ux.transpose()
Vy = - Uy.transpose()
I = sps.eye(nx*ny)
identity = sps.eye(nx*ny)


# We then build relative permitivity tensors
Expand All @@ -79,16 +79,16 @@ def eigen_build(k0: float, n: np.ndarray, dx: float, dy: float,
# Now we need to construct the full operator matrices
t = time.time()
Pxx = (- Ux * epszi * Vy * Vx * Uy / k0**2
+ (k0**2 * I + Ux * epszi * Vx) * (epsx + Vy * Uy / k0**2) )
+ (k0**2 * identity + Ux * epszi * Vx) * (epsx + Vy * Uy / k0**2) )

Pyy = (- Uy * epszi * Vx * Vy * Ux / k0**2
+ (k0**2 * I + Uy * epszi * Vy) * (epsy + Vx * Ux / k0**2) )
+ (k0**2 * identity + Uy * epszi * Vy) * (epsy + Vx * Ux / k0**2) )

Pxy = ( Ux * epszi * Vy * (epsy + Vx * Ux / k0**2)
- (k0**2 * I + Ux * epszi * Vx) * Vy * Ux / k0**2 )
- (k0**2 * identity + Ux * epszi * Vx) * Vy * Ux / k0**2 )

Pyx = ( Uy * epszi * Vx * (epsx + Vy * Uy / k0**2 )
- (k0**2 * I + Uy * epszi * Vy) * Vx * Uy / k0**2 )
- (k0**2 * identity + Uy * epszi * Vy) * Vx * Uy / k0**2 )

print('and we are done (after {} secs).'.format(time.time() - t))

Expand Down
9 changes: 4 additions & 5 deletions philsol/solve.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import scipy.sparse.linalg as linalg
import time
import numpy as np
from typing import Tuple, List, Optional, Union
from typing import Tuple, List, Optional
import scipy.sparse as sparse

def solve(P: sparse.spmatrix, beta_trial: float, E_trial: Optional[np.ndarray] = None, neigs: int = 1) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
Expand Down Expand Up @@ -53,22 +53,21 @@ def solve_fancy(P: sparse.spmatrix, beta_trial: float, E_trial: Optional[np.ndar
from slepc4py import SLEPc


t = time.time()

# convert eigenproblem into a form that petsc can understand
fancy_P = PETSc.Mat().createAIJ(size=P.shape,
csr=(P.indptr, P.indices,
P.data))


# initalise solver object
E = SLEPc.EPS(); E.create()
E = SLEPc.EPS()
E.create()

# lets set the solver options
E.setOperators(fancy_P)
E.setProblemType(SLEPc.EPS.ProblemType.NHEP)
E.setDimensions(nev = neigs)
if E_trial != None:
if E_trial is not None:
E.setInitialSpace(E_trial)

# now we set up the spectral region to look in
Expand Down
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ keywords = ['vector', 'finite difference', 'waveguide', 'mode solver']
python = "^3.11"
numpy = "^1.24"
scipy = "^1.11"

[tool.pytest.ini_options]
testpaths = ["tests"]
pythonpath = ["."]

[tool.ruff.lint]
# Pin the rule set so new ruff releases cannot silently add rules; the
# legacy code is not ready for the full default set (UP, PL, ...).
select = ["E4", "E7", "E9", "F"]
Loading
Loading