@@ -26,29 +26,29 @@ filters such as `Warp By Scalar`.
2626
2727The example solves
2828
29- $$
29+ ``` math
3030-\nabla\cdot(\theta\nabla u)=f
3131\quad\mathrm{in}\quad \Omega=(0,1)^2,
32- $$
32+ ```
3333
3434with homogeneous Dirichlet boundary conditions on the bottom and top edges,
3535
36- $$
36+ ``` math
3737u(x,0)=u(x,1)=0,
38- $$
38+ ```
3939
4040and periodicity between the left and right edges,
4141
42- $$
42+ ``` math
4343u(0,y)=u(1,y).
44- $$
44+ ```
4545
4646The source term is
4747
48- $$
48+ ``` math
4949f(x,y)=x\sin(5\pi y)
5050+\exp\left(-\frac{(x-0.5)^2+(y-0.5)^2}{0.02}\right).
51- $$
51+ ```
5252
5353The material parameter ` theta ` is treated as the differentiable scalar
5454parameter in the AD check.
@@ -59,10 +59,10 @@ Let `u_full` be the full vector of nodal degrees of freedom and `u_red` be the
5959reduced vector after eliminating duplicate periodic degrees of freedom. The
6060periodic constraint is represented as
6161
62- $$
62+ ``` math
6363\boldsymbol{u}_{\mathrm{full}}
6464=\boldsymbol{P}\boldsymbol{u}_{\mathrm{red}}.
65- $$
65+ ```
6666
6767Here ` P_mat ` has shape ` (N, M) ` , where ` N ` is the number of full DOFs before
6868applying periodic constraints and ` M ` is the number of independent DOFs after
@@ -75,55 +75,55 @@ or adjoint loads back to the independent reduced DOFs.
7575For this example, every right-boundary node is matched to the corresponding
7676left-boundary node shifted by
7777
78- $$
78+ ``` math
7979(x,y)\mapsto(x+1,y).
80- $$
80+ ```
8181
8282` periodic_boundary_conditions ` constructs ` P_mat ` by assigning the slave
8383right-boundary DOF to the same reduced column as its master left-boundary DOF.
8484The nonlinear residual and tangent are then assembled in reduced coordinates:
8585
86- $$
86+ ``` math
8787\boldsymbol{R}_{\mathrm{red}}
8888=\boldsymbol{P}^{T}\boldsymbol{R}_{\mathrm{full}},
8989\qquad
9090\boldsymbol{K}_{\mathrm{red}}
9191=\boldsymbol{P}^{T}\boldsymbol{K}_{\mathrm{full}}\boldsymbol{P}.
92- $$
92+ ```
9393
9494Dirichlet rows are eliminated before applying the periodic projection.
9595
9696## Toy problem
9797
9898Consider a tiny non-FEM system with four full unknowns,
9999
100- $$
100+ ``` math
101101\boldsymbol{u}_{\mathrm{full}}
102102=
103103\begin{bmatrix}
104104u_0 & u_1 & u_2 & u_3
105105\end{bmatrix}^{T},
106- $$
106+ ```
107107
108108and impose one periodic constraint:
109109
110- $$
110+ ``` math
111111u_3=u_0.
112- $$
112+ ```
113113
114114Only three independent unknowns remain:
115115
116- $$
116+ ``` math
117117\boldsymbol{u}_{\mathrm{red}}
118118=
119119\begin{bmatrix}
120120a & b & c
121121\end{bmatrix}^{T}.
122- $$
122+ ```
123123
124124The expansion matrix is
125125
126- $$
126+ ``` math
127127\boldsymbol{P}
128128=
129129\begin{bmatrix}
140140\begin{bmatrix}
141141a\\ b\\ c\\ a
142142\end{bmatrix}.
143- $$
143+ ```
144144
145145Thus ` N = 4 ` , ` M = 3 ` , and the slave DOF ` u_3 ` reuses the same reduced column as
146146the master DOF ` u_0 ` .
147147
148148Suppose the full residual is
149149
150- $$
150+ ``` math
151151\boldsymbol{R}_{\mathrm{full}}
152152(\boldsymbol{u},\theta)
153153=
168168\begin{bmatrix}
1691691\\0\\0\\1
170170\end{bmatrix}.
171- $$
171+ ```
172172
173173The reduced residual is
174174
175- $$
175+ ``` math
176176\boldsymbol{R}_{\mathrm{red}}
177177=
178178\boldsymbol{P}^{T}
181181=
182182\boldsymbol{K}_{\mathrm{red}}\boldsymbol{u}_{\mathrm{red}}
183183-\theta\boldsymbol{f}_{\mathrm{red}},
184- $$
184+ ```
185185
186186where
187187
188- $$
188+ ``` math
189189\boldsymbol{K}_{\mathrm{red}}
190190=
191191\boldsymbol{P}^{T}\boldsymbol{K}_{\mathrm{full}}\boldsymbol{P}
203203\begin{bmatrix}
2042042\\0\\0
205205\end{bmatrix}.
206- $$
206+ ```
207207
208208The first reduced residual component contains the sum of the master and slave
209209full residual entries. For example, if
210210
211- $$
211+ ``` math
212212\boldsymbol{R}_{\mathrm{full}}
213213=
214214\begin{bmatrix}
215215R_0\\R_1\\R_2\\R_3
216216\end{bmatrix},
217- $$
217+ ```
218218
219219then
220220
221- $$
221+ ``` math
222222\boldsymbol{P}^{T}\boldsymbol{R}_{\mathrm{full}}
223223=
224224\begin{bmatrix}
225225R_0+R_3\\R_1\\R_2
226226\end{bmatrix}.
227- $$
227+ ```
228228
229229At ` theta = 1 ` , solving
230230
231- $$
231+ ``` math
232232\boldsymbol{K}_{\mathrm{red}}\boldsymbol{u}_{\mathrm{red}}
233233=
234234\boldsymbol{f}_{\mathrm{red}}
235- $$
235+ ```
236236
237237gives
238238
239- $$
239+ ``` math
240240\boldsymbol{u}_{\mathrm{red}}
241241=
242242\begin{bmatrix}
250250\begin{bmatrix}
2512511\\1\\1\\1
252252\end{bmatrix}.
253- $$
253+ ```
254254
255255## Sensitivity in reduced coordinates
256256
257257Let $\hat{\boldsymbol{u}}\in\mathbb{R}^{M}$ denote the independent reduced DOFs
258258and $\boldsymbol{u}\in\mathbb{R}^{N}$ denote the full DOFs. The forward solve
259259can be viewed as
260260
261- $$
261+ ``` math
262262\boldsymbol{u}
263263=
264264\boldsymbol{P}\hat{\boldsymbol{u}},
265265\qquad
266266\boldsymbol{P}\in\mathbb{R}^{N\times M},
267267\qquad
268268N\ge M.
269- $$
269+ ```
270270
271271After applying periodic constraints, the full residual equation
272272$\boldsymbol{R}(\boldsymbol{u},\theta)=\boldsymbol{0}$ becomes the reduced
273273equation
274274
275- $$
275+ ``` math
276276\boldsymbol{P}^{T}
277277\boldsymbol{R}
278278\left(\boldsymbol{P}\hat{\boldsymbol{u}},\theta\right)
279279=
280280\boldsymbol{0}.
281- $$
281+ ```
282282
283283Differentiating with respect to $\theta$ gives
284284
285- $$
285+ ``` math
286286\boldsymbol{P}^{T}
287287\frac{\partial \boldsymbol{R}}{\partial \boldsymbol{u}}
288288\boldsymbol{P}
292292\frac{\partial \boldsymbol{R}}{\partial \theta}
293293=
294294\boldsymbol{0}.
295- $$
295+ ```
296296
297297Therefore
298298
299- $$
299+ ``` math
300300\frac{d\hat{\boldsymbol{u}}}{d\theta}
301301=
302302-
307307\right)^{-1}
308308\boldsymbol{P}^{T}
309309\frac{\partial \boldsymbol{R}}{\partial \theta}.
310- $$
310+ ```
311311
312312For an objective $J(\boldsymbol{u},\theta)$, write gradients of $J$ as row
313313vectors. The reduced and full gradients are related by
314314
315- $$
315+ ``` math
316316\left(
317317\frac{\partial J}{\partial \hat{\boldsymbol{u}}}
318318\right)^{T}
321321\left(
322322\frac{\partial J}{\partial \boldsymbol{u}}
323323\right)^{T}.
324- $$
324+ ```
325325
326326The adjoint variable $\boldsymbol{\lambda}\in\mathbb{R}^{M}$ is defined in the
327327reduced space:
328328
329- $$
329+ ``` math
330330\left(
331331\boldsymbol{P}^{T}
332332\frac{\partial \boldsymbol{R}}{\partial \boldsymbol{u}}
337337\left(
338338\frac{\partial J}{\partial \hat{\boldsymbol{u}}}
339339\right)^{T}.
340- $$
340+ ```
341341
342342Start from the chain rule in reduced coordinates:
343343
344- $$
344+ ``` math
345345\frac{dJ}{d\theta}
346346=
347347\frac{\partial J}{\partial \theta}
348348+
349349\frac{\partial J}{\partial \hat{\boldsymbol{u}}}
350350\frac{d\hat{\boldsymbol{u}}}{d\theta}.
351- $$
351+ ```
352352
353353Using the adjoint definition,
354354
355- $$
355+ ``` math
356356\frac{\partial J}{\partial \hat{\boldsymbol{u}}}
357357=
358358\boldsymbol{\lambda}^{T}
361361\frac{\partial \boldsymbol{R}}{\partial \boldsymbol{u}}
362362\boldsymbol{P}
363363\right),
364- $$
364+ ```
365365
366366so
367367
368- $$
368+ ``` math
369369\frac{dJ}{d\theta}
370370=
371371\frac{\partial J}{\partial \theta}
377377\boldsymbol{P}
378378\right)
379379\frac{d\hat{\boldsymbol{u}}}{d\theta}.
380- $$
380+ ```
381381
382382The differentiated reduced residual gives
383383
384- $$
384+ ``` math
385385\left(
386386\boldsymbol{P}^{T}
387387\frac{\partial \boldsymbol{R}}{\partial \boldsymbol{u}}
392392-
393393\boldsymbol{P}^{T}
394394\frac{\partial \boldsymbol{R}}{\partial \theta}.
395- $$
395+ ```
396396
397397Substituting this into the chain rule gives
398398
399- $$
399+ ``` math
400400\frac{dJ}{d\theta}
401401=
402402\frac{\partial J}{\partial \theta}
403403-
404404\boldsymbol{\lambda}^{T}
405405\boldsymbol{P}^{T}
406406\frac{\partial \boldsymbol{R}}{\partial \theta}.
407- $$
407+ ```
408408
409409The important point is that both the tangent solve and the adjoint solve live
410410in the reduced space. The full residual and full tangent are evaluated at
@@ -423,9 +423,9 @@ The script generates a Gmsh mesh, solves the forward problem, writes the VTU
423423solution, and prints a finite-difference versus automatic-differentiation check
424424for
425425
426- $$
426+ ``` math
427427J(\theta)=\sum_i u_i(\theta)^2.
428- $$
428+ ```
429429
430430For derivative verification, prefer a direct linear solve such as
431431` spsolve_solver ` , or PETSc with ` ksp_type="preonly" ` and ` pc_type="lu" ` .
0 commit comments