Skip to content

Commit 3094a80

Browse files
committed
rename
1 parent ad03a5a commit 3094a80

File tree

3 files changed

+57
-47
lines changed

3 files changed

+57
-47
lines changed

examples/ansys/optim_bars.ipynb renamed to examples/ansys/pyvista_jaxfem.ipynb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@
854854
},
855855
{
856856
"cell_type": "code",
857-
"execution_count": 72,
857+
"execution_count": null,
858858
"id": "0ddca0dd",
859859
"metadata": {},
860860
"outputs": [],
@@ -921,8 +921,6 @@
921921
" dirichlet_values = jnp.array([0.0])\n",
922922
" van_neumann_values = jnp.array([[0.0, 0.0, 0.1]])\n",
923923
"\n",
924-
" # Instead of passing all inputs and trying to stop_gradient on them,\n",
925-
" # we need to wrap the tesseract call to only allow gradients w.r.t. rho\n",
926924
" c = apply_tesseract(\n",
927925
" fem_tess,\n",
928926
" {\n",

examples/ansys/optim_bars_pymapdl.ipynb renamed to examples/ansys/pyvista_pymapdl.ipynb

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@
7070
"metadata": {},
7171
"outputs": [],
7272
"source": [
73-
"# The PyMAPDL Host (X.X.X.X IP Address) and Port (XXX) are pulled from the environment \n",
73+
"# The PyMAPDL Host (X.X.X.X IP Address) and Port (XXX) are pulled from the environment\n",
7474
"import os\n",
75+
"\n",
7576
"HOST = os.getenv(\"MAPDL_HOST\")\n",
7677
"if HOST is None:\n",
7778
" raise ValueError(\n",
@@ -83,7 +84,7 @@
8384
" raise ValueError(\n",
8485
" \"Unable to read $MAPDL_PORT from the environment. \"\n",
8586
" + \"Use 'export MAPDL_PORT=X' for the port of your MAPDL Instance.\"\n",
86-
" )\n"
87+
" )"
8788
]
8889
},
8990
{
@@ -112,7 +113,7 @@
112113
"import matplotlib.pyplot as plt\n",
113114
"from tesseract_core import Tesseract\n",
114115
"from tesseract_core.runtime.experimental import TesseractReference\n",
115-
"from utils import hex_to_pyvista, plot_grid, plot_grid_slice, plot_mesh, stop_grads_int\n",
116+
"from utils import hex_to_pyvista, plot_grid, plot_grid_slice, plot_mesh\n",
116117
"\n",
117118
"from tesseract_jax import apply_tesseract"
118119
]
@@ -466,12 +467,12 @@
466467
}
467468
],
468469
"source": [
469-
"import pyvista as pv\n",
470470
"import numpy as np\n",
471+
"import pyvista as pv\n",
471472
"\n",
472473
"grid = pv.ImageData(\n",
473474
" dimensions=np.array((Nx, Ny, Nz)) + 1,\n",
474-
" origin=(-Lx / 2, -Ly / 2, -Lz / 2),\n",
475+
" origin=(-Lx / 2, -Ly / 2, -Lz / 2),\n",
475476
" spacing=(Lx / Nx, Ly / Ny, Lz / Nz), # These are the cell sizes along each axis\n",
476477
")\n",
477478
"# repeated casts will eventually expose cell_connectivitiy\n",
@@ -541,7 +542,7 @@
541542
},
542543
{
543544
"cell_type": "code",
544-
"execution_count": 13,
545+
"execution_count": null,
545546
"id": "9ea51a40",
546547
"metadata": {
547548
"editable": true,
@@ -572,6 +573,7 @@
572573
" Args:\n",
573574
" mesh: Dictionary containing 'points' array.\n",
574575
" Lx: Size of the domain in x-direction.\n",
576+
" Ly: Size of the domain in y-direction.\n",
575577
" Lz: Size of the domain in z-direction.\n",
576578
"\n",
577579
" Returns:\n",
@@ -582,7 +584,7 @@
582584
"\n",
583585
" dirichlet_mask = pts[:, 0] <= -Lx / 2\n",
584586
" # von Neumann condition (select nodes at x=Lx with constraints on y and z)\n",
585-
" x_lim = Lx/2\n",
587+
" x_lim = Lx / 2\n",
586588
" y_min = -0.1 * Ly\n",
587589
" y_max = 0.1 * Ly\n",
588590
" z_min = -0.1 * Lz\n",
@@ -599,7 +601,6 @@
599601
" ),\n",
600602
" )\n",
601603
"\n",
602-
"\n",
603604
" return dirichlet_mask, van_neumann_mask\n",
604605
"\n",
605606
"\n",
@@ -653,7 +654,7 @@
653654
"outputs": [],
654655
"source": [
655656
"# load the Finite Element Method Tesseract\n",
656-
"fem_tess = Tesseract.from_tesseract_api(\"pymapdl_tess/tesseract_api.py\")\n"
657+
"fem_tess = Tesseract.from_tesseract_api(\"pymapdl_tess/tesseract_api.py\")"
657658
]
658659
},
659660
{
@@ -877,7 +878,7 @@
877878
},
878879
{
879880
"cell_type": "code",
880-
"execution_count": 17,
881+
"execution_count": null,
881882
"id": "6c1dae2f",
882883
"metadata": {},
883884
"outputs": [],
@@ -942,16 +943,16 @@
942943
"\n",
943944
" van_neumann_mask = jax.lax.stop_gradient(van_neumann_mask)\n",
944945
" dirichlet_mask = jax.lax.stop_gradient(dirichlet_mask)\n",
945-
" dirichlet_values = jnp.array([0.0])\n",
946-
" van_neumann_values = jnp.array([[0.0, 0.0, 0.1]])\n",
946+
" # dirichlet_values = jnp.array([0.0])\n",
947+
" # van_neumann_values = jnp.array([[0.0, 0.0, 0.1]])\n",
947948
"\n",
948-
" # Instead of passing all inputs and trying to stop_gradient on them,\n",
949-
" # we need to wrap the tesseract call to only allow gradients w.r.t. rho\n",
950949
" # TODO unify this framework\n",
951950
" dirichlet_idx = np.where(dirichlet_mask)[0]\n",
952951
" von_neumann_idx = np.where(van_neumann_mask)[0]\n",
953-
" von_neumann_values = np.array([0, 0.0, 0.1]) + np.zeros((von_neumann_idx.shape[0], 3))\n",
954-
" \n",
952+
" von_neumann_values = np.array([0, 0.0, 0.1]) + np.zeros(\n",
953+
" (von_neumann_idx.shape[0], 3)\n",
954+
" )\n",
955+
"\n",
955956
" # TODO again note the transpose on rho!\n",
956957
" rho_for_fea = rho_grid.T.reshape((hex_mesh[\"faces\"].shape[0], 1))\n",
957958
"\n",
@@ -973,14 +974,11 @@
973974
" },\n",
974975
" )[\"compliance\"]\n",
975976
"\n",
976-
" return c, {\n",
977-
" \"sdf\": sdf,\n",
978-
" \"rho_grid\": rho_grid\n",
979-
" }\n",
977+
" return c, {\"sdf\": sdf, \"rho_grid\": rho_grid}\n",
980978
"\n",
981979
"\n",
982980
"identity_and_store_grads.defvjp(identity_fwd, identity_bwd)\n",
983-
"grad_fn = jax.value_and_grad(loss, has_aux=True)\n"
981+
"grad_fn = jax.value_and_grad(loss, has_aux=True)"
984982
]
985983
},
986984
{
@@ -1200,11 +1198,11 @@
12001198
"for i in range(num_tests):\n",
12011199
" print(i)\n",
12021200
" params_up = params.at[i].add(FD_delta)\n",
1203-
" (fupp, _), _ = grad_fn(params_up, iteration=i*2)\n",
1201+
" (fupp, _), _ = grad_fn(params_up, iteration=i * 2)\n",
12041202
"\n",
12051203
" if run_central_difference:\n",
12061204
" params_down = params.at[i].subtract(2.0 * FD_delta)\n",
1207-
" (fdown, _), _ = grad_fn(params_down, iteration=i*2+1)\n",
1205+
" (fdown, _), _ = grad_fn(params_down, iteration=i * 2 + 1)\n",
12081206
" FD_grads[i] = (fupp - fdown) / FD_delta / 2.0\n",
12091207
"\n",
12101208
" else:\n",
@@ -1235,7 +1233,7 @@
12351233
},
12361234
{
12371235
"cell_type": "code",
1238-
"execution_count": 18,
1236+
"execution_count": null,
12391237
"id": "849a7559-90fc-4531-a0ed-54bf4a1c7817",
12401238
"metadata": {
12411239
"editable": true,
@@ -1248,16 +1246,17 @@
12481246
"source": [
12491247
"import mmapy\n",
12501248
"import numpy as np\n",
1251-
"from tesseract_core.runtime import Array, Differentiable, Float32, Int32\n",
1249+
"from tesseract_core.runtime import Array, Float32, Int32\n",
12521250
"\n",
12531251
"\n",
12541252
"class MMAOptimizer:\n",
12551253
" \"\"\"A wrapper for the MMA optimizer from mmapy.\n",
1256-
" github.com/arjendeetman/GCMMA-MMA-Python\n",
1254+
"\n",
1255+
" Source is github.com/arjendeetman/GCMMA-MMA-Python.\n",
12571256
" mmapy is a pretty barebones implementation of MMA in python. It should work for now.\n",
12581257
" Alternatives to consider:\n",
12591258
" - github.com/LLNL/pyMMAopt\n",
1260-
" - pyopt.org/reference/optimizers.mma.html\n",
1259+
" - pyopt.org/reference/optimizers.mma.html.\n",
12611260
"\n",
12621261
" \"\"\"\n",
12631262
"\n",
@@ -1317,7 +1316,9 @@
13171316
" if iteration == 1:\n",
13181317
" self.objective_scale_factor = np.abs(self.objective_scale / objective_value)\n",
13191318
" objective_value *= self.objective_scale_factor\n",
1320-
" objective_gradient = np.asarray(objective_gradient) * self.objective_scale_factor\n",
1319+
" objective_gradient = (\n",
1320+
" np.asarray(objective_gradient) * self.objective_scale_factor\n",
1321+
" )\n",
13211322
"\n",
13221323
" # the bounds dont necessarily change every iteration\n",
13231324
" if x_min is None:\n",
@@ -1335,7 +1336,7 @@
13351336
" )\n",
13361337
"\n",
13371338
" # calculate the next iteration of x\n",
1338-
" xmma, ymma, zmma, lam, xsi, eta, mu, zet, s, low, upp = mmapy.mmasub(\n",
1339+
" xmma, _ymma, _zmma, _lam, _xsi, _eta, _mu, _zet, _s, low, upp = mmapy.mmasub(\n",
13391340
" self.m,\n",
13401341
" self.n,\n",
13411342
" iteration,\n",
@@ -1378,7 +1379,9 @@
13781379
" constraint_gradients: Array[(None, None), Float32] = None,\n",
13791380
" ) -> None:\n",
13801381
" def check_shape(shape: tuple, expected_shape: tuple, name: str) -> None:\n",
1381-
" if (len(shape)==1) or (shape[0] != expected_shape[0] or shape[1] != expected_shape[1]):\n",
1382+
" if (len(shape) == 1) or (\n",
1383+
" shape[0] != expected_shape[0] or shape[1] != expected_shape[1]\n",
1384+
" ):\n",
13821385
" raise TypeError(\n",
13831386
" f\"MMAError: The '{name}' was expected to have shape {expected_shape} but has shape {shape}.\"\n",
13841387
" )\n",
@@ -1393,12 +1396,12 @@
13931396
" if constraint_gradients is not None:\n",
13941397
" check_shape(\n",
13951398
" constraint_gradients.shape, (self.m, self.n), \"constraint gradients\"\n",
1396-
" )\n"
1399+
" )"
13971400
]
13981401
},
13991402
{
14001403
"cell_type": "code",
1401-
"execution_count": 19,
1404+
"execution_count": null,
14021405
"id": "39e30381",
14031406
"metadata": {
14041407
"editable": true,
@@ -1739,12 +1742,12 @@
17391742
"delta_x = 0.0\n",
17401743
"delta_y = 0.0\n",
17411744
"delta_z = Lz / 3\n",
1742-
"eps = 1.e-3 # a small value to ease numerics\n",
1745+
"eps = 1.0e-3 # a small value to ease numerics\n",
17431746
"bar_params = np.array(initial_params).reshape(n_chains, n_edges_per_chain + 1, 3)\n",
17441747
"param_min = bar_params - eps\n",
17451748
"param_max = bar_params + eps\n",
17461749
"for bar_idx, bar in enumerate(bar_params):\n",
1747-
" for coord_idx, xyz in enumerate(bar):\n",
1750+
" for coord_idx, _xyz in enumerate(bar):\n",
17481751
" for i, delta in enumerate([delta_x, delta_y, delta_z]):\n",
17491752
" param_min[bar_idx][coord_idx][i] -= delta\n",
17501753
" param_max[bar_idx][coord_idx][i] += delta\n",
@@ -1762,24 +1765,31 @@
17621765
"loss_hist = []\n",
17631766
"params_hist = []\n",
17641767
"aux_hist = []\n",
1765-
"optimizer = MMAOptimizer(initial_params[:,None], param_min, param_max, num_constraints, x_update_limit=x_update_limit)\n",
1768+
"optimizer = MMAOptimizer(\n",
1769+
" initial_params[:, None],\n",
1770+
" param_min,\n",
1771+
" param_max,\n",
1772+
" num_constraints,\n",
1773+
" x_update_limit=x_update_limit,\n",
1774+
")\n",
17661775
"\n",
17671776
"\n",
17681777
"for i in range(n_steps):\n",
17691778
" (loss_value, aux), grads = grad_fn(params, iteration=i)\n",
17701779
" print(loss_value)\n",
17711780
" print(params)\n",
17721781
" print(grads)\n",
1773-
" np_grads = np.array(grads[:,None])\n",
1774-
" np_params = np.array(params[:,None])\n",
1775-
" np_params = optimizer.calculate_next_x(loss_value, np_grads, g, dgdx, i+1, np_params)\n",
1782+
" np_grads = np.array(grads[:, None])\n",
1783+
" np_params = np.array(params[:, None])\n",
1784+
" np_params = optimizer.calculate_next_x(\n",
1785+
" loss_value, np_grads, g, dgdx, i + 1, np_params\n",
1786+
" )\n",
17761787
" params = jnp.array(np_params.flatten())\n",
1777-
" \n",
1788+
"\n",
17781789
" loss_hist.append(loss_value)\n",
17791790
" params_hist.append(params)\n",
17801791
" aux_hist.append(aux)\n",
17811792
"\n",
1782-
"\n",
17831793
" print(f\"Iteration {i + 1}, Loss: {loss_value:.4f}\")"
17841794
]
17851795
},
@@ -1845,7 +1855,9 @@
18451855
" mesh = hex_mesh\n",
18461856
" rho_dot = grad_storage[i + 2000][1][: len(mesh[\"faces\"])]\n",
18471857
" pv_mesh = hex_to_pyvista(\n",
1848-
" mesh[\"points\"], mesh[\"faces\"], {\"rho\": aux_hist[i][\"rho_grid\"].T.flatten(), \"rho_dot\": rho_dot.T.flatten()}\n",
1858+
" mesh[\"points\"],\n",
1859+
" mesh[\"faces\"],\n",
1860+
" {\"rho\": aux_hist[i][\"rho_grid\"].T.flatten(), \"rho_dot\": rho_dot.T.flatten()},\n",
18491861
" )\n",
18501862
" pv_mesh.save(f\"vtks/fem_shapeopt_mesh{i + 1}.vtk\")"
18511863
]
@@ -1882,9 +1894,9 @@
18821894
],
18831895
"metadata": {
18841896
"kernelspec": {
1885-
"display_name": "tesseract-jax",
1897+
"display_name": "fem",
18861898
"language": "python",
1887-
"name": "tesseract-jax"
1899+
"name": "python3"
18881900
},
18891901
"language_info": {
18901902
"codemirror_mode": {
@@ -1896,7 +1908,7 @@
18961908
"name": "python",
18971909
"nbconvert_exporter": "python",
18981910
"pygments_lexer": "ipython3",
1899-
"version": "3.13.5"
1911+
"version": "3.13.9"
19001912
}
19011913
},
19021914
"nbformat": 4,
File renamed without changes.

0 commit comments

Comments
 (0)