This document defines the working rules for coding agents in this repository. Keep AGENTS.md and CLAUDE.md identical.
- Use the
maxwellconda environment for all Python commands, tests, and scripts. - Keep the codebase clean. Do not preserve legacy code, compatibility shims, or backward-support paths unless explicitly requested.
- Write all code comments and commit messages in English.
- Never add AI or agent attribution to git history. Commit messages must not contain
Co-Authored-Bytrailers naming an assistant,Claude-Sessionor similar session links, or "generated with" banners, and the author/committer identity is always the human owner. This applies to commits, tags, PR titles and PR bodies. - Keep third-party commercial product, service, and solver names out of commit messages, branch names, PR titles, general development documents, comments, and feature descriptions. Describe the behavior with neutral terms such as
external reference solver,reference backend, orthird-party adapterso commercial brands do not spread through the repository or GitHub history. - Use an exact commercial product name only where technically required for an interoperability API, adapter module, executable command, dependency declaration, legal attribution, or a narrowly scoped adapter test/document. Do not repeat that name in unrelated implementation, validation, or numerical-method changes unless the user explicitly requests it.
- Maintain
FEATURE_LIST.mdas the user-facing feature inventory. Every new user-visible feature, public API capability, or meaningful workflow addition must update that file in the same change. - Store active development documents by type under
docs/plans/,docs/memory/,docs/assessments/, anddocs/reference/. Keep solver and feature areas together in each type directory; use descriptive filenames instead of extrafdtd,fdfd, or similar directory layers. Move completed or superseded development documents to the matching type underdocs/archive/. KeepFEATURE_LIST.mdat the repository root as the user-facing feature inventory. - Prioritize correctness and efficiency over convenience.
- Core computation is GPU-first. Minimize GPU-CPU transfers and do not introduce new CPU fallback paths unless explicitly requested.
- PyTorch-native integration is a hard requirement. New differentiable or optimization-facing APIs must be designed as native PyTorch workflows, not thin wrappers around non-PyTorch side paths.
- The stable public architecture is
Scene + Simulation + Result. New user-facing code must follow this model. Scenemust support both direct declarative construction (mw.Scene(...)) and a PyTorch-native module-style scene definition that ultimately materializes into the same publicSceneobject model.- Do not reintroduce old public convenience paths such as
Scene.set_*,Scene.with_*,mw.FDFD, ormw.FDTD. - Preserve the compile-layer naming convention: scene-facing helpers use
compile_*names such asScene.compile_materials(...),Scene.compile_relative_materials(...), andScene.compile_material_tensors(...). - Shared core geometry constructors default to
device=None;Scene(...)owns device placement and defaults to CUDA.
This repository is a PyTorch-native differentiable full-wave electromagnetic simulation framework for Maxwell's equations with two internal solver runtimes:
FDFD: frequency-domain finite-difference solver using sparse linear algebra.FDTD: time-domain Yee-grid solver with native CUDA kernels, CPML support, and differentiable adjoint workflows.
The public API is declarative:
Scene: physical problem definitionSimulation: execution configuration and backend selectionResult: fields, monitors, metadata, plotting, and persistence
For differentiable and optimization workflows, the public API must also remain PyTorch-native:
- trainable scene definitions may be implemented as
torch.nn.ModuleviaSceneModule - module-style scenes must compile or materialize to the same
Scenepublic contract - solver entry remains
Scene -> Simulation -> Result, even when theSceneis produced by a PyTorch module
Supporting first-class Maxwell scene objects include:
DomainGridSpecBoundarySpecBoundaryKindMaterialRegionStructureMaterialPointDipolePlaneWaveGaussianBeamModeSourcePointMonitorPlaneMonitorFluxMonitorModeMonitorModePort
The current codebase is organized as a Python package under witwin/maxwell/, with tests under tests/. Shared geometry and structure primitives are re-exported through witwin.maxwell but originate from the shared witwin.core dependency.
witwin/maxwell/__init__.py: public package exports.witwin/maxwell/scene.py: declarative scene model, domain/grid/boundary handling,MaterialRegion, andSceneModule.witwin/maxwell/media.py: public Maxwell material models, dispersion poles, and diagonal anisotropy descriptors.witwin/maxwell/sources.py: public source definitions and source-time waveforms.witwin/maxwell/monitors.py: point, plane, flux, and modal monitors.witwin/maxwell/ports.py: first-class modal port definitions.witwin/maxwell/simulation.py: public simulation entrypoint, runtime preparation, and backend dispatch.witwin/maxwell/result.py: unified simulation result container.witwin/maxwell/compiler/: scene compilers for materials, sources, ports, and monitors.witwin/maxwell/fdfd/: internal FDFD runtime and postprocessing helpers.witwin/maxwell/fdtd/: internal FDTD runtime, CPML logic, source injection, observers, and adjoint support.witwin/maxwell/postprocess/: near/far-field, directivity, RCS, scattering-parameter, and modal postprocessing.witwin/maxwell/adapters/: interoperability adapters such as Tidy3D export.witwin/maxwell/visualization/: shared visualization utilities.docs/plans/: active implementation plans and functional roadmaps.docs/memory/: current implementation notes and durable engineering context.docs/assessments/: current audits and capability assessments.docs/reference/: maintained technical reference material.docs/archive/: completed or superseded plans, assessments, design records, and status snapshots.tests/: pytest-based coverage for public API behavior, numerics, gradients, and validation workflows.
- Prefer package-style imports from
witwin.maxwell, typicallyimport witwin.maxwell as mw. - New public examples, tests, and scripts should look like
Scene -> Simulation -> Result, not direct backend construction. - Build scenes with
Domain,GridSpec,BoundarySpec,Structure,Material, source objects, monitor objects,ModePort, andMaterialRegionas appropriate. - Use the declarative
BoundaryKind/BoundarySpecvocabulary consistently; do not introduce new raw boundary-mode strings outside validated APIs. - Use
Scene.add_structure(...),Scene.add_source(...),Scene.add_monitor(...),Scene.add_port(...), andScene.add_material_region(...)for scene assembly. - For PyTorch-native scene definitions, prefer
SceneModulesubclasses that exposeto_scene()and return a standardScene; do not create a second incompatible simulation entrypoint. - When behavior changes in a way a user would notice, update
FEATURE_LIST.mdto keep the documented feature inventory current. - If backend internals must be inspected, prefer
Simulation.prepare()and then inspectprepared.solverrather than importing or constructing backend classes through the public path. - Keep new implementations aligned with the current declarative 3D API and compiler/runtime split.
- If a design choice would add extra abstraction, data copies, or compatibility branches, prefer the simpler and faster path.
- When changing numerics, preserve Yee-grid consistency, PML behavior, Bloch/periodic boundary semantics, and GPU execution flow.
- Avoid adding duplicate implementations in parallel files. Update the primary module instead.
Activate the environment first:
conda activate maxwellCommon commands:
python -m pytest tests
python -m pytest tests/api/public/test_public_api.py tests/api/public/test_simulation_smoke.py
python -m pytest tests/core/scene/test_scene.py tests/materials/compiler/test_material_compiler.py
python -m pytest tests/boundaries/cpml/test_fdtd_cpml.py tests/monitors/observers/test_fdtd_observers.py
python -m pytest tests/gradients/test_fdtd_adjoint_bridge.py
python -m pytest tests/validation/benchmark/test_benchmark_system.py
python -m benchmark
python -m benchmark dipole_vacuum
python -m benchmark planewave_vacuumBenchmark / validation workflow:
- Use
python -m benchmarkas the unified Maxwell-vs-Tidy3D validation entrypoint. - Scenario definitions live under
benchmark/scenes/, grouped by family under directories such asdipole/andplanewave/. - Tidy3D reference caches are stored under
benchmark/cache/. - Generated comparison plots are stored under
benchmark/plots/. - Benchmark metrics are aggregated into
benchmark/RESULTS.mdand should be updated by rerunning the benchmark command after benchmark-related changes.
If you add or modify Python code, prefer validating with targeted pytest coverage first, then broader tests if needed.
- In this environment,
rgmay exist but fail withAccess is denied. If that happens, switch immediately to PowerShell-native search withGet-ChildItem+Select-Stringinstead of retryingrg. conda run -n maxwellmay fail to pass stdin through topython -reliably in this Codex/PowerShell setup. Preferconda run -n maxwell python -c ...for short snippets, or invoke the environment interpreter directly withC:\Users\Asixa\miniconda3\envs\maxwell\python.exe.- Windows command-line length limits are easy to hit with
python -cand large inline scripts. For long scripts, pass the code through an environment variable such asPYCODEand executepython -c "import os; exec(os.environ['PYCODE'])", or use the environment interpreter with stdin if that path is reliable. - Large single-shot
apply_patchupdates can also hit Windows path/command length limits. If a full-file replacement fails for size reasons, rewrite it in smaller patch chunks instead of retrying the same oversized patch. - When checking whether
AGENTS.mdandCLAUDE.mdmatch, do not use barefcin PowerShell because it resolves toFormat-Custom. Callfc.exeexplicitly if you want the file-compare tool.
Primary dependencies include:
- PyTorch
- NumPy
- SciPy
- Matplotlib
- tqdm
- CuPy
- witwin
Optional dependencies:
tidy3dfor adapter export and benchmark cross-validation workflows
An NVIDIA GPU with CUDA is expected for core solver workflows.