Skip to content

Commit

Permalink
Update nbvalx if statements to version 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-ballarin committed Jan 12, 2024
1 parent 1c21882 commit 1f70803
Showing 1 changed file with 38 additions and 43 deletions.
81 changes: 38 additions & 43 deletions tutorials/01_thermal_block.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
"metadata": {},
"outputs": [],
"source": [
"%register_run_if_allowed_tags \\\n",
" reduced_basis_inefficient, reduced_basis_efficient, \\\n",
" pod_galerkin_inefficient, pod_galerkin_efficient"
"%%register_run_if_allowed_tags\n",
"reduction_method: \"Reduced Basis\", \"POD Galerkin\"\n",
"online_efficient: True, False"
]
},
{
Expand All @@ -84,7 +84,9 @@
"metadata": {},
"outputs": [],
"source": [
"%register_run_if_current_tag reduced_basis_inefficient"
"%%register_run_if_current_tags\n",
"reduction_method = \"Reduced Basis\"\n",
"online_efficient = True"
]
},
{
Expand Down Expand Up @@ -383,7 +385,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"class StabilityFactorEvaluation(object):\n",
" \"\"\"Define the eigenvalue problem to compute the coercivity constant, and solve it with EPS.\"\"\"\n",
"\n",
Expand Down Expand Up @@ -484,7 +486,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"stability_factor_evaluation = StabilityFactorEvaluation(problem)"
]
},
Expand All @@ -495,7 +497,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and not online_efficient\n",
"stability_factor_lower_bound = stability_factor_evaluation"
]
},
Expand All @@ -506,7 +508,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and not online_efficient\n",
"stability_factor = stability_factor_evaluation.evaluate(mu_solve)\n",
"print(f\"Stability factor at {mu_solve} is {stability_factor}\")"
]
Expand All @@ -518,7 +520,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"class RieszRepresentationProblem(object):\n",
" \"\"\"Define the Riesz representation problem, and solve it with KSP.\"\"\"\n",
"\n",
Expand Down Expand Up @@ -581,7 +583,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and online_efficient\n",
"class StabilityFactorLowerBound(object):\n",
" \"\"\"Evaluate a lower bound of the stability factor using the min-theta method.\"\"\"\n",
"\n",
Expand All @@ -599,7 +601,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and online_efficient\n",
"stability_factor_lower_bound = StabilityFactorLowerBound()"
]
},
Expand All @@ -610,7 +612,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and online_efficient\n",
"stability_factor_exact = stability_factor_evaluation.evaluate(mu_solve)\n",
"stability_factor_min_theta = stability_factor_lower_bound.evaluate(mu_solve)\n",
"print(f\"Stability factor at {mu_solve} is {stability_factor_exact}\")\n",
Expand All @@ -627,7 +629,6 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_inefficient, reduced_basis_inefficient, pod_galerkin_efficient, reduced_basis_efficient\n",
"class ProjectionBasedReducedProblem(abc.ABC):\n",
" \"\"\"Define a linear projection-based problem, and solve it with KSP.\"\"\"\n",
"\n",
Expand Down Expand Up @@ -747,7 +748,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_inefficient, reduced_basis_inefficient\n",
"%%run_if not online_efficient\n",
"class ProjectionBasedInefficientReducedProblem(ProjectionBasedReducedProblem):\n",
" \"\"\"Specialize the projection-based problem to inefficient assembly of the system.\"\"\"\n",
"\n",
Expand All @@ -773,7 +774,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_inefficient\n",
"%%run_if reduction_method == \"POD Galerkin\" and not online_efficient\n",
"class PODGInefficientReducedProblem(ProjectionBasedInefficientReducedProblem):\n",
" \"\"\"Specialize the inefficient projection-based problem to basis generation with POD.\"\"\"\n",
"\n",
Expand All @@ -787,7 +788,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and not online_efficient\n",
"class RBInefficientReducedProblem(ProjectionBasedInefficientReducedProblem):\n",
" \"\"\"Specialize the inefficient projection-based problem to basis generation with RB.\"\"\"\n",
"\n",
Expand Down Expand Up @@ -834,7 +835,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_efficient, reduced_basis_efficient\n",
"%%run_if online_efficient\n",
"class ProjectionBasedEfficientReducedProblem(ProjectionBasedReducedProblem):\n",
" \"\"\"Specialize the projection-based problem to efficient assembly of the system.\"\"\"\n",
"\n",
Expand Down Expand Up @@ -922,7 +923,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_efficient\n",
"%%run_if reduction_method == \"POD Galerkin\" and online_efficient\n",
"class PODGEfficientReducedProblem(ProjectionBasedEfficientReducedProblem):\n",
" \"\"\"Specialize the efficient projection-based problem to basis generation with RB.\"\"\"\n",
"\n",
Expand All @@ -936,7 +937,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and online_efficient\n",
"class RBEfficientReducedProblem(ProjectionBasedEfficientReducedProblem):\n",
" \"\"\"Specialize the efficient projection-based problem to basis generation with POD.\"\"\"\n",
"\n",
Expand Down Expand Up @@ -1090,7 +1091,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_inefficient\n",
"%%run_if reduction_method == \"POD Galerkin\" and not online_efficient\n",
"print(rbnicsx.io.TextBox(\"POD-Galerkin offline phase begins\", fill=\"=\"))\n",
"print(\"\")\n",
"\n",
Expand Down Expand Up @@ -1129,7 +1130,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_efficient\n",
"%%run_if reduction_method == \"POD Galerkin\" and online_efficient\n",
"print(rbnicsx.io.TextBox(\"POD-Galerkin offline phase begins\", fill=\"=\"))\n",
"print(\"\")\n",
"\n",
Expand Down Expand Up @@ -1173,7 +1174,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_efficient, pod_galerkin_inefficient\n",
"%%run_if reduction_method == \"POD Galerkin\"\n",
"positive_eigenvalues = np.where(eigenvalues > 0., eigenvalues, np.nan)\n",
"singular_values = np.sqrt(positive_eigenvalues)"
]
Expand All @@ -1185,7 +1186,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_efficient, pod_galerkin_inefficient\n",
"%%run_if reduction_method == \"POD Galerkin\"\n",
"fig = go.Figure()\n",
"fig.add_scatter(\n",
" x=np.arange(singular_values.shape[0]), y=singular_values / singular_values[0], mode=\"markers+lines\")\n",
Expand All @@ -1202,7 +1203,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_efficient, pod_galerkin_inefficient\n",
"%%run_if reduction_method == \"POD Galerkin\"\n",
"assert np.all(\n",
" rbnicsx.test.order_of_magnitude(singular_values[:Nmax] / singular_values[0]) == [0, -1, -3, -5])"
]
Expand All @@ -1214,7 +1215,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and not online_efficient\n",
"_RBReducedProblemType = RBInefficientReducedProblem"
]
},
Expand All @@ -1225,7 +1226,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and online_efficient\n",
"_RBReducedProblemType = RBEfficientReducedProblem"
]
},
Expand All @@ -1236,7 +1237,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"def greedy(reduced_problem: _RBReducedProblemType, N: int) -> typing.Tuple[ # type: ignore[no-any-unimported]\n",
" np.int32, petsc4py.PETSc.RealType]:\n",
" \"\"\"Carry out a greedy search of the training set.\"\"\"\n",
Expand All @@ -1255,7 +1256,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and not online_efficient\n",
"print(rbnicsx.io.TextBox(\"RB offline phase begins\", fill=\"=\"))\n",
"print(\"\")\n",
"\n",
Expand Down Expand Up @@ -1319,7 +1320,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient\n",
"%%run_if reduction_method == \"Reduced Basis\" and online_efficient\n",
"print(rbnicsx.io.TextBox(\"RB offline phase begins\", fill=\"=\"))\n",
"print(\"\")\n",
"\n",
Expand Down Expand Up @@ -1394,7 +1395,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"fig = go.Figure()\n",
"fig.add_scatter(\n",
" x=np.arange(N + 1),\n",
Expand All @@ -1413,7 +1414,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"assert len(absolute_error_estimators) == Nmax + 1\n",
"assert np.all(rbnicsx.test.order_of_magnitude(absolute_error_estimators) == [0, 0, -2, -5, -6])"
]
Expand All @@ -1425,7 +1426,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"not_greedy_indices = np.setdiff1d(np.arange(len(training_set)), greedy_indices)\n",
"fig = go.Figure()\n",
"fig.add_scatter(\n",
Expand All @@ -1447,7 +1448,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"assert len(greedy_indices) == Nmax + 1\n",
"assert np.all(greedy_indices == [9, 99, 29, 19, 59])"
]
Expand All @@ -1459,7 +1460,6 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient, reduced_basis_efficient, pod_galerkin_inefficient, pod_galerkin_efficient\n",
"viskex.dolfinx.plot_scalar_field(reduced_problem.basis_functions[0], \"first basis solution\")"
]
},
Expand All @@ -1470,7 +1470,6 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient, reduced_basis_efficient, pod_galerkin_inefficient, pod_galerkin_efficient\n",
"viskex.dolfinx.plot_scalar_field(reduced_problem.basis_functions[1], \"second basis solution\")"
]
},
Expand All @@ -1481,7 +1480,6 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient, reduced_basis_efficient, pod_galerkin_inefficient, pod_galerkin_efficient\n",
"viskex.dolfinx.plot_scalar_field(reduced_problem.basis_functions[2], \"third basis solution\")"
]
},
Expand All @@ -1500,7 +1498,6 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient, reduced_basis_efficient, pod_galerkin_inefficient, pod_galerkin_efficient\n",
"reduced_solution = reduced_problem.solve(mu_solve, Nmax)"
]
},
Expand All @@ -1511,7 +1508,6 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient, reduced_basis_efficient, pod_galerkin_inefficient, pod_galerkin_efficient\n",
"reconstructed_reduced_solution = reduced_problem.reconstruct_solution(reduced_solution)\n",
"viskex.dolfinx.plot_scalar_field(reconstructed_reduced_solution, \"reconstructed reduced solution\")"
]
Expand All @@ -1523,7 +1519,6 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_inefficient, reduced_basis_efficient, pod_galerkin_inefficient, pod_galerkin_efficient\n",
"viskex.dolfinx.plot_scalar_field(\n",
" (solution - reconstructed_reduced_solution, problem.function_space), \"pointwise error\")"
]
Expand All @@ -1535,7 +1530,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_efficient, pod_galerkin_inefficient\n",
"%%run_if reduction_method == \"POD Galerkin\"\n",
"error_norm = reduced_problem.compute_norm(solution - reconstructed_reduced_solution)\n",
"error_energy_norm = reduced_problem.compute_energy_norm(mu_solve, solution - reconstructed_reduced_solution)\n",
"print(\"Natural norm of the error\", error_norm)\n",
Expand All @@ -1549,7 +1544,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if pod_galerkin_efficient, pod_galerkin_inefficient\n",
"%%run_if reduction_method == \"POD Galerkin\"\n",
"assert rbnicsx.test.order_of_magnitude(error_norm) == -9\n",
"assert rbnicsx.test.order_of_magnitude(error_energy_norm) == -8"
]
Expand All @@ -1561,7 +1556,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"error_norm = reduced_problem.compute_norm(solution - reconstructed_reduced_solution)\n",
"error_norm_estimator = reduced_problem.estimate_error_norm(mu_solve, reduced_solution)\n",
"error_energy_norm = reduced_problem.compute_energy_norm(mu_solve, solution - reconstructed_reduced_solution)\n",
Expand All @@ -1577,7 +1572,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%run_if reduced_basis_efficient, reduced_basis_inefficient\n",
"%%run_if reduction_method == \"Reduced Basis\"\n",
"assert rbnicsx.test.order_of_magnitude(error_norm) == -7\n",
"assert rbnicsx.test.order_of_magnitude(error_norm_estimator) in (-7, -6)\n",
"assert rbnicsx.test.order_of_magnitude(error_energy_norm) == -7\n",
Expand Down

0 comments on commit 1f70803

Please sign in to comment.