Skip to content

doc: Thomson Scattering with SciPy minimize #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
184 changes: 184 additions & 0 deletions examples/integration/tsadar/1d-defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
parameters:
electron:
Te:
val: .6
active: False
lb: 0.01
ub: 1.5
ne:
val: 0.2
active: False
lb: 0.001
ub: 1.0

ion-1:
Ti:
val: 0.12
active: False
same: False
lb: 0.001
ub: 1.0
Z:
val: 14.0
active: False
lb: 0.5
ub: 7.0
A:
val: 40.0
active: False
fract:
val: 0.1
active: False

general:
amp1:
val: 1.
active: False
lb: 0.01
ub: 3.75
amp2:
val: 1.
active: False
lb: 0.01
ub: 3.75
amp3:
val: 1.0
active: False
lb: 0.01
ub: 3.75
lam:
val: 526.5
active: False
lb: 523.0
ub: 528.0
Te_gradient:
val: 0.0
active: False
lb: 0.
ub: 10.
num_grad_points: 1
ne_gradient:
val: 0.
active: False
lb: 0.
ub: 15.
num_grad_points: 1
ud:
val: 0.0
angle: 0.0
active: False
lb: -100.0
ub: 100.0
Va:
val: 0.0
angle: 0.0
active: False
lb: -20.5
ub: 20.5

other:
expandedions: False
extraoptions:
spectype: 1d
load_ion_spec: false
load_ele_spec: True
fit_IAW: False
fit_EPWb: True
fit_EPWr: True
absolute_timing: false
PhysParams:
background: [0, 0]
norm: 0
widIRF:
spect_stddev_ele: 1.3
spect_stddev_ion: 0.015
iawoff: 0
iawfilter: [1, 4, 24, 528]
CCDsize: [1024, 1024]
flatbg: 0
gain: 1
points_per_pixel: 5
ang_res_unit: 10
lam_res_unit: 5
refit: False
refit_thresh: 0.25
calc_sigmas: False

data:
shotnum: 101675
shotDay: False
launch_data_visualizer: True
fit_rng:
blue_min: 450
blue_max: 510
red_min: 540
red_max: 625
iaw_min: 525.5
iaw_max: 527.5
iaw_cf_min: 526.49
iaw_cf_max: 526.51
forward_epw_start: 400
forward_epw_end: 700
forward_iaw_start: 525.75
forward_iaw_end: 527.25
bgscaleE: 1.0
bgscaleI: 0.1
bgshotmult: 1
ion_loss_scale: 1.0
ele_t0: 0
ion_t0_shift: 0
ele_lam_shift: 0.0
probe_beam: P9
dpixel: 2
lineouts:
type:
pixel
start: 400
end: 600
skip: 50
background:
type:
pixel
slice: 900

plotting:
n_sigmas: 3
rolling_std_width: 5
data_cbar_u: 10
data_cbar_l: 0
ion_window_start: 525
ion_window_end: 528
ele_window_start: 425
ele_window_end: 625

optimizer:
# use adam for nn / stochastic gradient descent
# use L-BFGS-B for full batch / parameter learning
# although adam will work here too
method: l-bfgs-b
moment_loss: false
loss_method: l2
hessian: False
y_norm: True
x_norm: False
grad_method: AD
batch_size: 2
num_epochs: 120
learning_rate: 1.0e-2
parameter_norm: True
refine_factor: 0
num_mins: 1

nn:
use: false
conv_filters: 32|32|16
linear_widths: 16|8

dist_fit:
window:
len: 0.2 #should be even
type: hamming # one of [hamming, hann, bartlett]

mlflow:
experiment: inverse-thomson-scattering
run: new
91 changes: 91 additions & 0 deletions examples/integration/tsadar/1d-inputs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
parameters:
electron:
Te:
val: .5
active: True
lb: 0.001
ub: 1.5
ne:
val: .2
active: True
lb: .001
ub: 1.
fe:
active: False
type: dlm
dim: 1
nv: 64
params:
m:
val: 2.5
lb: 2.0
ub: 5.0
ion-1:
Ti:
val: .2
active: False
lb: 0.01
ub: 3.
same: False
Z:
val: 8.
active: False
lb: 1.
ub: 25.
A:
val: 40.
active: False
fract:
val: 1.
active: False
general:
amp1:
val: 1.
active: True
lb: 0.01
ub: 3.75
amp2:
val: 1.
active: True
lb: 0.01
ub: 3.75
amp3:
val: 1.
active: False
lb: 0.
ub: 10.
lam:
val: 524.0
active: True
lb: 523.
ub: 528.
Te_gradient:
val: 0.0
active: False
lb: 0.
ub: 10.
num_grad_points: 1
ne_gradient:
val: 0.
active: False
lb: 0.
ub: 15.
num_grad_points: 1
ud:
val: 0.0
angle: 0.0
active: False
lb: -10.0
ub: 10.0
Va:
val: 0.0
angle: 0.0
active: False
lb: -20.5
ub: 20.5


other:
refit: False
refit_thresh: 5.0
calc_sigmas: false
30 changes: 30 additions & 0 deletions examples/integration/tsadar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# TSADAR Tesseract

TSADAR is a package for Thomson Scattering analysis developed by Ergodic LLC in collaboration with LLE and UR.

The Thomson Scattering diagnostic is used to infer plasma conditions in fusion-relevant experiments at LLE, ZAP Energy, among others. It is a model-based diagnostic
where the forward model is capable of describing the measured signal relatively accurately. There are some additional transformations that are specific to the device on which and facility where the measurement is being performed.

![Fitted](./fit_trajectory.gif)

In this example, we show how to build a Tesseract that wraps the TSADAR package, and then how to use that Tesseract to fit some data using a gradient-based fitting approach. This particular example uses the L-BFGS-B algorithm implemented in `scipy.optimize.minimize`.


## The TSADAR Tesseract

In the file `tesseract_api.py´ we define the endpoints of this tesseract,




## Build
`tesseract --loglevel debug build . --tag 0.0.1`

## Serve
`tesseract serve tsadaract:0.0.1`

## Use
`python3 fitter.py`

## Citation
1. Milder, A. L., Joglekar, A. S., Rozmus, W. & Froula, D. H. Qualitative and quantitative enhancement of parameter estimation for model-based diagnostics using automatic differentiation with an application to inertial fusion. Mach. Learn.: Sci. Technol. 5, 015026 (2024).
31 changes: 31 additions & 0 deletions examples/integration/tsadar/animate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import numpy as np
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt

def animate(trajectory : list[np.ndarray], true_electron_spectrum: np.ndarray):

n = len(trajectory)

optim_steps = np.linspace(0, n, n + 1)

# repeat last trajectory point
for _ in range(10):
trajectory.append(trajectory[-1])
optim_steps = np.append(optim_steps, n)
fig, ax = plt.subplots()


def update(i):
ax.clear()
ax.set_xlabel("Wavelength")
ax.set_ylabel("Intensity")
ax.set_title(f"Optimization step {int(optim_steps[i])}")

ax.plot(trajectory[i], label="Fit")
ax.plot(true_electron_spectrum, label="True")
ax.legend()
ax.grid()


ani = FuncAnimation(fig, update, frames=len(trajectory), repeat=False)
ani.save("fit_trajectory.gif", writer="imagemagick", fps=3)
12 changes: 12 additions & 0 deletions examples/integration/tsadar/example_vjp_inputs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"inputs": {
"ne": [0.1]
},
"vjp_inputs": ["ne"],
"vjp_outputs": ["electron_spectrum"],
"cotangent_vector": {
"electron_spectrum": [
[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.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