diff --git a/tutorials/01_thermal_block.ipynb b/tutorials/01_thermal_block.ipynb index bfbd339..1e45c41 100644 --- a/tutorials/01_thermal_block.ipynb +++ b/tutorials/01_thermal_block.ipynb @@ -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" ] }, { @@ -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" ] }, { @@ -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", @@ -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)" ] }, @@ -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" ] }, @@ -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}\")" ] @@ -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", @@ -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", @@ -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()" ] }, @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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", @@ -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)" ] @@ -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", @@ -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])" ] @@ -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" ] }, @@ -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" ] }, @@ -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", @@ -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", @@ -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", @@ -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", @@ -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])" ] @@ -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", @@ -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])" ] @@ -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\")" ] }, @@ -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\")" ] }, @@ -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\")" ] }, @@ -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)" ] }, @@ -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\")" ] @@ -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\")" ] @@ -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", @@ -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" ] @@ -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", @@ -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",