{ "cells": [ { "cell_type": "markdown", "id": "aececd27", "metadata": { "papermill": { "duration": 0.001041, "end_time": "2026-03-19T15:28:12.771353", "exception": false, "start_time": "2026-03-19T15:28:12.770312", "status": "completed" }, "tags": [] }, "source": [ "(Check Convergence Example)=\n", "# Check Convergence Script Examples" ] }, { "cell_type": "markdown", "id": "8f03602b", "metadata": { "papermill": { "duration": 0.00095, "end_time": "2026-03-19T15:28:12.773728", "exception": false, "start_time": "2026-03-19T15:28:12.772778", "status": "completed" }, "tags": [] }, "source": [ "Download the [UserWarningModel.fez](https://github.com/Rocscience/rs2-scripting/blob/main/docs/example_code/example_models/UserWarningModel.fez) for this example." ] }, { "cell_type": "code", "execution_count": 1, "id": "efd68734", "metadata": { "execution": { "iopub.execute_input": "2026-03-19T15:28:12.780326Z", "iopub.status.busy": "2026-03-19T15:28:12.779926Z", "iopub.status.idle": "2026-03-19T15:28:12.792219Z", "shell.execute_reply": "2026-03-19T15:28:12.790565Z" }, "papermill": { "duration": 0.01767, "end_time": "2026-03-19T15:28:12.793509", "exception": false, "start_time": "2026-03-19T15:28:12.775839", "status": "completed" }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "At stage 1, model didn't converge.\n" ] } ], "source": [ "import os\n", "import zipfile\n", "\n", "def check_convergence(filePath):\n", " \"\"\"\n", " To detect convergence after stress analysis, stage number and convergence flag \n", " are extracted from the .rng file inside the .fez file.\n", "\n", " If the model didn't converge, the unstable stage number will be returned.\n", " If all stages converge, the model will return None.\n", " \"\"\"\n", "\n", " with zipfile.ZipFile(filePath, 'r') as model_file:\n", " # Find the .rng file\n", " rng_file = next((f for f in model_file.namelist() if f.endswith('.rng')), None)\n", " \n", " if not rng_file:\n", " raise FileNotFoundError(\"No .rng file found in the given file.\")\n", "\n", " with model_file.open(rng_file) as file:\n", " stage, converged_flag = None, None\n", " for line in file:\n", " decoded_line = line.decode('utf-8').strip()\n", " if decoded_line.startswith(\"* Stage number =\"):\n", " value = decoded_line.split('=')[1].strip()\n", " if value.isdigit():\n", " stage = int(value)\n", " \n", " # If the model doesn't converge at this stage, stage converged flag \n", " # will be raised\n", " elif decoded_line.startswith(\"* Stage converged flag =\"):\n", " value = decoded_line.split('=')[1].strip()\n", " if value.isdigit():\n", " converged_flag = int(value)\n", " if converged_flag == 1:\n", " return stage\n", "\n", " return None\n", "\n", "current_dir = os.path.dirname(os.path.abspath(\"\"))\n", "file_path = rf\"{current_dir}\\example_models\\UserWarningModel.fez\"\n", "\n", "stage = check_convergence(file_path)\n", "if stage != None:\n", " print(f\"At stage {stage}, model didn't converge.\")\n", "else:\n", " print(\"Model converged.\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.0" }, "papermill": { "default_parameters": {}, "duration": 4.093833, "end_time": "2026-03-19T15:28:15.575125", "environment_variables": {}, "exception": null, "input_path": "C:\\Users\\GraceHu\\source\\repos\\RS2_Python_Client_Library\\docs\\example_code\\support_functions\\check_convergence_script_examples.ipynb", "output_path": "C:\\Users\\GraceHu\\source\\repos\\RS2_Python_Client_Library\\docs\\example_code\\support_functions\\check_convergence_script_examples.ipynb", "parameters": {}, "start_time": "2026-03-19T15:28:11.481292", "version": "2.6.0" } }, "nbformat": 4, "nbformat_minor": 5 }