No description
Find a file
2025-09-13 14:59:45 +02:00
__pycache__ Initial commit: Deep Ritz method implementation 2025-09-13 14:59:45 +02:00
deepritz1d_test.ipynb Initial commit: Deep Ritz method implementation 2025-09-13 14:59:45 +02:00
deepritz_1d.py Initial commit: Deep Ritz method implementation 2025-09-13 14:59:45 +02:00
deepritz_2d.py Initial commit: Deep Ritz method implementation 2025-09-13 14:59:45 +02:00
deepritz_2d_test.ipynb Initial commit: Deep Ritz method implementation 2025-09-13 14:59:45 +02:00
readme.md Initial commit: Deep Ritz method implementation 2025-09-13 14:59:45 +02:00
requirements.txt Initial commit: Deep Ritz method implementation 2025-09-13 14:59:45 +02:00

DeepRitz: Code & Experiments (1D/2D)

A compact playground for the Deep Ritz method to solve Poisson-type problems with neural networks. Includes clean, importable Python modules for 1D and 2D, optional shape (envelope) functions for Dirichlet BCs, and MonteCarlo quadrature.


Whats inside

  • deepritz_1d.py: 1D solver on [0,\pi], exact case -u''=1. 4block MLP, selectable activations, optional boundary shape or penalty (\lambda=1/(2\delta)).
  • deepritz_2d.py: 2D solver on [-1,1]^2 with exact solution u_{ex}(x,y)=(x^2-1)(y^2-1).
  • Utilities to track loss & L2 error and to plot history and solutions.

Requirements

pip install torch matplotlib numpy

Quickstart (1D)

from deepritz_1d import RunConfig, runcase, plot_history, plot_solution
import torch

cfg = RunConfig(
    width=10, activation="relu", shape="xpix",  # or 'none' + delta
    delta=None, penalty_lambda=None,              # use delta=1e-3 for penalty
    iters=8000, batch_interior=4096, lr=1e-3,
    device="cuda" if torch.cuda.is_available() else "cpu"
)
model, hist, l2 = runcase(cfg)
plot_history(hist)
plot_solution(model, shape=cfg.shape)

Shapes (1D): none, sin, sin2, xpix (x(\pi-x)), x2xpi (x^2(x-\pi)) Activations: tanh, relu, relu2, relu3, sin


Quickstart (2D)

from deepritz_2d import RunConfig2D, runcase as run2d, plot_history, plot_solution2d
import torch

device = "cuda" if torch.cuda.is_available() else "cpu"
cfg2 = RunConfig2D(width=10, activation="relu", shape="prod", iters=8000,
                   batch_interior=3000, device=device)
model2, hist2, l2_2d = run2d(cfg2)
plot_history(hist2)
plot_solution2d(model2, shape=cfg2.shape)

Shapes (2D): none, prod (1-x^2)(1-y^2), sinxy \sin(\tfrac{\pi(x+1)}{2})\sin(\tfrac{\pi(y+1)}{2})


Typical experiments

  • Penalty vs. shape: set shape="none", delta=1e-3 vs. shape="xpix" (1D) or shape="prod" (2D).
  • Activation study: swap activation in config.
  • Sampling: batch_interior, boundary_batch control MC quadrature.

Error tracking

  • Loss: Deep Ritz energy per iteration.
  • L2 error: evaluated periodically on a fixed grid (history keys: l2, l2_k).

Notes

  • Total error splits into modeling (penalty \delta), discretization (MC quadrature + network approximation), and optimization (training). The code reports loss/L2; optimization accuracy depends on SGD/Adam settings.

Citation pointers

  • Deep Ritz idea: E & Yu (2018).
  • Apriori/aposteriori insights: Minakowski & Richter (2023, JCAM).