-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
4,183 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Sphinx build info version 1 | ||
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. | ||
config: 838cceab9bf6970cab9bdd64855333e1 | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 |
Binary file modified
BIN
+1.55 KB
(110%)
doc/_downloads/07fcc19ba03226cd3d83d4e40ec44385/auto_examples_python.zip
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+2.56 KB
(110%)
doc/_downloads/6f1e7a639e0699d6164445b55e6c116d/auto_examples_jupyter.zip
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
field | ||
Field | ||
===== | ||
""" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
doc/_downloads/e2351dd27667554a422ac2d62b6aca00/harmonic_oscillator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
''' | ||
Harmonic oscillator | ||
=================== | ||
''' | ||
|
||
|
||
import numpy as np | ||
import torch | ||
import torch.nn as nn | ||
import matplotlib.pyplot as plt | ||
import math | ||
|
||
def oscillator(d, w0, x): | ||
w = math.sqrt(w0**2 - d**2) | ||
phi = math.atan2(-d, w) | ||
y = torch.exp(-d * x) * torch.cos(phi + w * x) / math.cos(phi) | ||
return y | ||
|
||
|
||
torch.manual_seed(123) | ||
torch.set_default_dtype(torch.float32) | ||
d, w0 = 2.0, 20.0 | ||
x = torch.linspace(0, 1, 500)[:, None] | ||
y = oscillator(d, w0, x) | ||
x_data = x[0:20:10] | ||
y_data = y[0:20:10] | ||
x_physics = torch.linspace(0, 1, 25)[:, None].requires_grad_(True) | ||
model = nn.Sequential(nn.Linear(1, 16), nn.Tanh(), nn.Linear(16, 16), nn.Tanh(), nn.Linear(16, 1)) | ||
optimizer = torch.optim.Adam(model.parameters(), lr=1e-2) | ||
ones = torch.ones_like(x_physics) | ||
for i in range(5000): | ||
optimizer.zero_grad() | ||
yhp = model(x_physics) | ||
dx, = torch.autograd.grad(yhp, x_physics, ones, create_graph=True) | ||
dx2, = torch.autograd.grad(dx, x_physics, ones, create_graph=True) | ||
physics = dx2 + 2 * d * dx + w0 * w0 * yhp | ||
loss = torch.mean( | ||
(model(x_data) - y_data)**2) + 1e-4 * torch.mean(physics**2) | ||
loss.backward() | ||
optimizer.step() | ||
plt.plot(x, y) | ||
plt.plot(x, model(x).detach(), '-') | ||
plt.show() |
54 changes: 54 additions & 0 deletions
54
doc/_downloads/e30c667c9801d5417694b836d4b76169/harmonic_oscillator.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%pip install -q git+https://github.com/cselab/odil" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n# Harmonic oscillator\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import numpy as np\nimport torch\nimport torch.nn as nn\nimport matplotlib.pyplot as plt\nimport math\n\ndef oscillator(d, w0, x):\n w = math.sqrt(w0**2 - d**2)\n phi = math.atan2(-d, w)\n y = torch.exp(-d * x) * torch.cos(phi + w * x) / math.cos(phi)\n return y\n\n\ntorch.manual_seed(123)\ntorch.set_default_dtype(torch.float32)\nd, w0 = 2.0, 20.0\nx = torch.linspace(0, 1, 500)[:, None]\ny = oscillator(d, w0, x)\nx_data = x[0:20:10]\ny_data = y[0:20:10]\nx_physics = torch.linspace(0, 1, 25)[:, None].requires_grad_(True)\nmodel = nn.Sequential(nn.Linear(1, 16), nn.Tanh(), nn.Linear(16, 16), nn.Tanh(), nn.Linear(16, 1))\noptimizer = torch.optim.Adam(model.parameters(), lr=1e-2)\nones = torch.ones_like(x_physics)\nfor i in range(5000):\n optimizer.zero_grad()\n yhp = model(x_physics)\n dx, = torch.autograd.grad(yhp, x_physics, ones, create_graph=True)\n dx2, = torch.autograd.grad(dx, x_physics, ones, create_graph=True)\n physics = dx2 + 2 * d * dx + w0 * w0 * yhp\n loss = torch.mean(\n (model(x_data) - y_data)**2) + 1e-4 * torch.mean(physics**2)\n loss.backward()\n optimizer.step()\nplt.plot(x, y)\nplt.plot(x, model(x).detach(), '-')\nplt.show()" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
17 changes: 17 additions & 0 deletions
17
doc/_images/math/0f716ccfb45e4bb9bd5a80d0c9cc97871344b62a.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions
24
doc/_images/math/0fa66331941f83e8be152ad2fcb91c9f1057715d.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.