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
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.github
.github
12 changes: 7 additions & 5 deletions .github/workflows/build_docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ jobs:

- name: Install graphviz
run: apt update && apt install -y graphviz-dev

- name: Install mesa-utils
run: apt-get update && apt-get install -y mesa-utils
run: apt-get update && apt-get install -y mesa-utils

- name: Install dependencies
run: python3 -m pip install ".[docs]"
run: |
python3 -m pip install git+https://github.com/funsim/moola.git
python3 -m pip install ".[docs]"

- name: Build docs
run: jupyter book build -W .

- name: Upload artifact
- name: Upload artifact
uses: actions/upload-artifact@v7
# always upload artifact, which can include error messages
if: always()
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/check_formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ jobs:
run: |
ruff check .
ruff format --check .

- name: Mypy check
run: python3 -m mypy .
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ concurrency:
group: "pages"
cancel-in-progress: true

jobs:
jobs:

check-formatting:
uses: ./.github/workflows/check_formatting.yml
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
# with:
# name: code-coverage-report
# path: "./public/code-coverage-report"

- name: Upload artifact
uses: actions/upload-pages-artifact@v5
with:
Expand Down
44 changes: 44 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: PyPI

on:
push:
branches:
- "main"
tags:
- "v*"
pull_request:
branches:
- "main"


jobs:
dist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6

- name: Build SDist and wheel
run: pipx run build

- uses: actions/upload-artifact@v7
with:
path: dist/*

- name: Check metadata
run: pipx run twine check dist/*

publish:
needs: [dist]
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags')
environment: pypi
permissions:
id-token: write

steps:
- uses: actions/download-artifact@v8
with:
name: artifact
path: dist

- uses: pypa/gh-action-pypi-publish@release/v1
6 changes: 3 additions & 3 deletions .github/workflows/test_package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ on:
push:
branches:
- main

pull_request:
branches:
- main

workflow_dispatch:
workflow_call:

Expand All @@ -28,4 +28,4 @@ jobs:


- name: Run tests parallel
run: mpirun -n 2 python3 -m pytest -vs tests/
run: mpirun -n 2 python3 -m pytest -vs tests/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ dmypy.json
_build/

*.dot
*.bp
*.bp
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
args: ['--maxkb=3000']
- id: check-docstring-first
- id: debug-statements
- id: check-toml

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: 'v0.15.14'
hooks:
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v2.1.0
hooks:
- id: mypy
files: ^src/|^tests/
args: ["--config-file", "pyproject.toml"]
6 changes: 3 additions & 3 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ authors:
email: dokken@simula.no
affiliation: Simula Research Laboratory
orcid: 'https://orcid.org/0000-0001-6489-8858'
repository-code: 'https://github.com/jorgensd/dxa'
url: 'https://jsdokken.com/dxa'
repository-code: 'https://github.com/scientificcomputing/dolfinx-adjoint'
url: 'https://github.com/scientificcomputing/dolfinx-adjoint'
abstract: >-
Adjoint framework for DOLFINx
keywords:
Expand All @@ -21,4 +21,4 @@ keywords:
- Optimization
license: MIT
version: v0.1.0
date-released: '2023-04-04'
date-released: '2023-04-04'
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use github pages for docker image
FROM ghcr.io/jorgensd/dxa:v0.1.0
FROM ghcr.io/scientificcomputing/dolfinx-adjoint:v0.2.0

# Create user with a home directory
ARG NB_USER
Expand Down
124 changes: 119 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,120 @@
# dxa
_________________
# DOLFINx-Adjoint

[![MIT](https://img.shields.io/github/license/jorgensd/dxa)](LICENSE)
[Read Latest Documentation](https://jorgensd.github.io/dxa/)
_________________
**DOLFINx-Adjoint** is an algorithmic differentiation (AD) framework for [DOLFINx](https://github.com/FEniCS/dolfinx). It allows you to automatically compute the gradients and Hessians of PDE-constrained optimization problems and track your computational graphs through the `pyadjoint` backend.

Read the [Latest Documentation here](https://scientificcomputing.github.io/dolfinx-adjoint).

> **Note for Legacy FEnICS Users:** If you are using the legacy FEnICS (`dolfin`) library, please refer to the original [dolfin-adjoint repository](https://github.com/dolfin-adjoint/dolfin-adjoint). This repository (DOLFINx-Adjoint) is built specifically for the modern DOLFINx environment and is currently under active development. It is intended to eventually support the same comprehensive feature set and capabilities as the legacy version.

## Features

* **Automated Adjoints:** Seamlessly derive discrete adjoint models from DOLFINx forward models.

* **Overloaded API:** Swap out standard `dolfinx` calls with their `dolfinx_adjoint` equivalents (e.g., `Function`, `Constant`, `LinearProblem`, `NonlinearProblem`, `assemble_scalar`) to automatically record the computational tape.

* **Optimization:** Seamless integration with PDE-constrained optimization frameworks like [Moola](https://github.com/funsim/moola) or SciPy via `pyadjoint.ReducedFunctional`.

## Installation

### Dependencies

DOLFINx-Adjoint requires **DOLFINx** (>=0.10.0) and **pyadjoint-ad**.

### via pip

The main way to install the package is via pip:

```bash
python3 -m pip install dolfinx-adjoint
```

### Development Install

To install the latest development version directly from the repository, use:

```bash
python3 -m pip install git+[https://github.com/scientificcomputing/dolfinx-adjoint.git](https://github.com/scientificcomputing/dolfinx-adjoint.git)
```

If you plan to actively modify the code, clone the repository and install the optional dependencies for testing, development, and documentation generation:

```bash
git clone [https://github.com/scientificcomputing/dolfinx-adjoint.git](https://github.com/scientificcomputing/dolfinx-adjoint.git)
cd dolfinx-adjoint
python3 -m pip install -e ".[all]"
```

### Docker

A pre-built Docker image is automatically published by the CI. You can pull the nightly build which comes with DOLFINx and DOLFINx-Adjoint pre-installed:

```bash
docker run -ti ghcr.io/scientificcomputing/dolfinx-adjoint:v0.2.0
```

*(Note: Adjust the tag to the latest release or build).*

## Quick Start

Using `dolfinx_adjoint` is designed to be as close to standard `dolfinx` syntax as possible. Here is a brief overview of how to track a parameter and assemble an objective functional:
<!-- #endregion -->

```python
import dolfinx
from mpi4py import MPI
import pyadjoint
import dolfinx_adjoint

# Create mesh and function space
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)
V = dolfinx.fem.functionspace(mesh, ("Lagrange", 1))

# Use dolfinx_adjoint overloaded types
# This ensures operations are tracked on the pyadjoint tape!
f = dolfinx_adjoint.Function(V, name="Control")
uh = dolfinx_adjoint.Function(V, name="State")

# ... Define your UFL forms ...

# Use overloaded solvers
problem = dolfinx_adjoint.LinearProblem(a, L, u=uh, bcs=[bc])
problem.solve()

# Assemble the objective scalar using the overloaded assembly
J_symbolic = 0.5 * ufl.inner(uh - d, uh - d) * ufl.dx
J = dolfinx_adjoint.assemble_scalar(J_symbolic)

# Create a ReducedFunctional for optimization
control = pyadjoint.Control(f)
Jhat = pyadjoint.ReducedFunctional(J, control)

# Evaluate gradient
gradient = Jhat.derivative()
```

<!-- #region -->
For more comprehensive examples, such as solving the optimal control of the Poisson equation or time-distributed control problems, check out the `demos/` directory or the [online documentation](https://scientificcomputing.github.io/dolfinx-adjoint).

## Development and Testing

Code formatting is enforced via `ruff` and type-checking via `mypy`. To set up your local development environment:

```bash
# Install development dependencies
python3 -m pip install -e ".[dev,test]"

# Run formatting checks
ruff check .
ruff format --check .

# Run type checking
python3 -m mypy .

# Run tests
python3 -m pytest -vs tests/
```

## License

MIT License. See [LICENSE](LICENSE) for more details.
<!-- #endregion -->
4 changes: 2 additions & 2 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ html:


bibtex_bibfiles:
- bibliography.bib
- docs/bibliography.bib


parse:
Expand All @@ -46,4 +46,4 @@ sphinx:
- 'sphinx.ext.autodoc'
- 'sphinx.ext.napoleon'
- 'sphinx.ext.viewcode'
exclude_patterns: ["README.md",".pytest_cache/*"]
exclude_patterns: [".pytest_cache/*"]
4 changes: 2 additions & 2 deletions _toc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
format: jb-book
root: index
root: README.md

parts:
- caption: Examples
Expand All @@ -9,4 +9,4 @@ parts:
- caption: Python API
chapters:
- file: "docs/api"
- file: "docs/api_blocks"
- file: "docs/api_blocks"
4 changes: 2 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ENV DEB_PYTHON_INSTALL_LAYOUT=deb_system

WORKDIR /tmp/

COPY . ./dxa
RUN python3 -m pip install ./dxa[all]
COPY . ./dolfinx-adjoint
RUN python3 -m pip install ./dolfinx-adjoint[all]


RUN rm -rf /tmp
1 change: 0 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ The following is the API of the functions and classes provided by the `dolfinx_a

.. automodule:: dolfinx_adjoint
:members:

2 changes: 1 addition & 1 deletion docs/api_blocks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ DOLFINx-adjoint Blocks API
The following is the API of the blocks implemented in the DOLFINx-adjoint package, which is used within the `pyadjoint` framework.

.. automodule:: dolfinx_adjoint.blocks
:members:
:members:
File renamed without changes.
7 changes: 0 additions & 7 deletions index.md

This file was deleted.

Loading