|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Description\n", |
| 8 | + "\n", |
| 9 | + "This notebook is to generate known good outputs (KGOs) for solving KorakianitisMixedModelPP (KMMPP) after we have added a cycle step size argument to the `advance_cycle()` function of the solver.\n", |
| 10 | + "\n", |
| 11 | + "With different step sizes, there are slightly different solutions reached, so tests need these different KGOs to have as comparisons." |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 1, |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import numpy as np\n", |
| 21 | + "import json\n", |
| 22 | + "\n", |
| 23 | + "from ModularCirc.Solver import Solver\n", |
| 24 | + "\n", |
| 25 | + "from ModularCirc.Models.KorakianitisMixedModelPP import KorakianitisMixedModelPP\n", |
| 26 | + "from ModularCirc.Models.KorakianitisMixedModel_parameters import KorakianitisMixedModel_parameters" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": null, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [ |
| 34 | + { |
| 35 | + "name": "stdout", |
| 36 | + "output_type": "stream", |
| 37 | + "text": [ |
| 38 | + "Running simulation with cycle step size: 1\n", |
| 39 | + "Running simulation with cycle step size: 1\n", |
| 40 | + "Running simulation with cycle step size: 3\n", |
| 41 | + "Running simulation with cycle step size: 3\n", |
| 42 | + "Running simulation with cycle step size: 5\n", |
| 43 | + "Running simulation with cycle step size: 5\n", |
| 44 | + "Running simulation with cycle step size: 7\n", |
| 45 | + "Running simulation with cycle step size: 7\n" |
| 46 | + ] |
| 47 | + } |
| 48 | + ], |
| 49 | + "source": [ |
| 50 | + "time_setup_dict = {\n", |
| 51 | + " 'name': 'TimeTest',\n", |
| 52 | + " 'ncycles': 40,\n", |
| 53 | + " 'tcycle': 1.0,\n", |
| 54 | + " 'dt': 0.001,\n", |
| 55 | + " 'export_min': 1\n", |
| 56 | + " }\n", |
| 57 | + "\n", |
| 58 | + "# Start the loop with different cycle step size to generate JSON files for each loop:\n", |
| 59 | + "cycle_step_sizes = [1, 3, 5, 7]\n", |
| 60 | + "\n", |
| 61 | + "# Dictionary to store the results for all cycle step sizes\n", |
| 62 | + "all_results = {\n", |
| 63 | + " \"metadata\": {\n", |
| 64 | + " \"description\": \"Results for different cycle_step_size values\",\n", |
| 65 | + " \"cycle_step_sizes\": cycle_step_sizes\n", |
| 66 | + " },\n", |
| 67 | + " \"results\": {}\n", |
| 68 | + "}\n", |
| 69 | + "\n", |
| 70 | + "# Loop through each cycle step size\n", |
| 71 | + "for i_cycle_step_size in cycle_step_sizes:\n", |
| 72 | + "\n", |
| 73 | + " print(f\"Running simulation with cycle step size: {i_cycle_step_size}\")\n", |
| 74 | + "\n", |
| 75 | + " # Initializing the parameter object\n", |
| 76 | + " parobj = KorakianitisMixedModel_parameters()\n", |
| 77 | + "\n", |
| 78 | + " # Initializing the model \n", |
| 79 | + " model = KorakianitisMixedModelPP(time_setup_dict=time_setup_dict, \n", |
| 80 | + " parobj=parobj, \n", |
| 81 | + " suppress_printing=True)\n", |
| 82 | + "\n", |
| 83 | + " # Initializing the solver\n", |
| 84 | + " solver = Solver(model=model)\n", |
| 85 | + "\n", |
| 86 | + " # Initializing the parameter object\n", |
| 87 | + " parobj = KorakianitisMixedModel_parameters()\n", |
| 88 | + "\n", |
| 89 | + " # Initializing the model \n", |
| 90 | + " model = KorakianitisMixedModelPP(time_setup_dict=time_setup_dict, \n", |
| 91 | + " parobj=parobj, \n", |
| 92 | + " suppress_printing=True)\n", |
| 93 | + "\n", |
| 94 | + " # Initializing the solver\n", |
| 95 | + " solver = Solver(model=model)\n", |
| 96 | + "\n", |
| 97 | + " # Solver is being setup: switching off console printing and setting the solver method to \"LSODA\"\n", |
| 98 | + " solver.setup(suppress_output=True, \n", |
| 99 | + " method='LSODA',\n", |
| 100 | + " step=i_cycle_step_size)\n", |
| 101 | + "\n", |
| 102 | + " # Running the model\n", |
| 103 | + " solver.solve()\n", |
| 104 | + "\n", |
| 105 | + " # Define the indexes of the equivalent to the last cycles\n", |
| 106 | + " tind_fin = np.arange(start=model.time_object.n_t-model.time_object.n_c,\n", |
| 107 | + " stop=model.time_object.n_t)\n", |
| 108 | + "\n", |
| 109 | + " # Dictionary to store the final cycle values for each component for the current step size\n", |
| 110 | + " final_cycle_values = {}\n", |
| 111 | + "\n", |
| 112 | + " # From each of the components, retrieve the volume (<V>), pressure (<P_i>) and flow (<Q_i>)\n", |
| 113 | + " for key, value in model.components.items():\n", |
| 114 | + " final_cycle_values[key] = {\n", |
| 115 | + " 'V': value.V.values[tind_fin].mean(),\n", |
| 116 | + " 'P_i': value.P_i.values[tind_fin].mean(),\n", |
| 117 | + " 'Q_i': value.Q_i.values[tind_fin].mean()\n", |
| 118 | + " }\n", |
| 119 | + "\n", |
| 120 | + " # Add the results for the current cycle step size to the main dictionary\n", |
| 121 | + " all_results[\"results\"][i_cycle_step_size] = final_cycle_values\n", |
| 122 | + "\n", |
| 123 | + "# Save all results to a single JSON file in the tests/expected_outputs directory\n", |
| 124 | + "with open('../../tests/expected_outputs/KorakianitisMixedModelPP_expected_output.json', 'w') as f:\n", |
| 125 | + " json.dump(all_results, f, indent=4)" |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + "metadata": { |
| 130 | + "kernelspec": { |
| 131 | + "display_name": "venv", |
| 132 | + "language": "python", |
| 133 | + "name": "python3" |
| 134 | + }, |
| 135 | + "language_info": { |
| 136 | + "codemirror_mode": { |
| 137 | + "name": "ipython", |
| 138 | + "version": 3 |
| 139 | + }, |
| 140 | + "file_extension": ".py", |
| 141 | + "mimetype": "text/x-python", |
| 142 | + "name": "python", |
| 143 | + "nbconvert_exporter": "python", |
| 144 | + "pygments_lexer": "ipython3", |
| 145 | + "version": "3.12.4" |
| 146 | + } |
| 147 | + }, |
| 148 | + "nbformat": 4, |
| 149 | + "nbformat_minor": 2 |
| 150 | +} |
0 commit comments