Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Mike1D in notebook using dotnet interactive #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 235 additions & 0 deletions notebooks/Demo.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Run Mike1D in notebook using dotnet interactive"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Install Mike1D from public NuGet"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#r \"nuget:DHI.Mike.Install\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#r \"nuget:DHI.Mike1D.Generic\"\n",
"#r \"nuget:DHI.Mike1D.BoundaryModule\"\n",
"#r \"nuget:DHI.Mike1D.Mike1DDataAccess\"\n",
"#r \"nuget:DHI.Mike1D.ResultDataAccess\"\n",
"#r \"nuget:DHI.Mike1D.RainfallRunoffModule\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Import"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"using System.IO;\n",
"using System.Linq;\n",
"using System.Collections.Generic;\n",
"using DHI.Mike.Install;\n",
"using DHI.Mike1D.BoundaryModule;\n",
"using DHI.Mike1D.Generic;\n",
"using DHI.Mike1D.Mike1DDataAccess;\n",
"using DHI.Mike1D.RainfallRunoffModule;\n",
"using DHI.Mike1D.ResultDataAccess;"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if (MikeImport.SetupLatest())\n",
" Console.Write(MikeImport.ActiveProduct().ToDisplayString());"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Mike1D example model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"var modelFilePath = Path.Combine(\"..\", \"mike1d-rtc-simulator-example\", \"MU\", \"RTCExampleBase.m1dx\");"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"public static Mike1DController CreateEngine(string modelFilePath) \n",
"{\n",
" var engineController = new Mike1DController();\n",
" var diagnostics = new Diagnostics(\"notebook\");\n",
" engineController.Mike1DData = new Mike1DBridge().Open(Connection.Create(modelFilePath), diagnostics);\n",
" return engineController;\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Change result file paths"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"public static void SetResultFiles(Mike1DData mike1DData, DateTime from, DateTime to) \n",
"{\n",
" var currentDirectory = Directory.GetCurrentDirectory(); \n",
" var modelFileName = Path.GetFileNameWithoutExtension(mike1DData.Connection.FilePath.ToString());\n",
"\n",
" foreach (var result in mike1DData.ResultSpecifications)\n",
" {\n",
" var resultFilePath = Path.Combine(currentDirectory, $\"{modelFileName}_{result.ID.ToString()}.res1d\");\n",
" result.Connection.FilePath = new FilePath(resultFilePath);\n",
" result.StartTime = from;\n",
" result.EndTime = to;\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prepare and run simulation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"public static void Prepare(IMike1DController engine, IDiagnostics diagnostics = null)\n",
"{\n",
" diagnostics = diagnostics ?? new Diagnostics();\n",
"\n",
" if (diagnostics.ErrorCountRecursive > 0)\n",
" throw new Exception(\"Loading errors, aborting. \" + diagnostics.ErrorsRecursive[0].Message);\n",
"\n",
" IDiagnostics validation = engine.Validate();\n",
"\n",
" if (validation.ErrorCountRecursive > 0)\n",
" throw new Exception(\"Validation errors, aborting. \" + validation.ErrorsRecursive[0].Message);\n",
"\n",
" engine.Initialize(diagnostics);\n",
"\n",
" if (diagnostics.ErrorCountRecursive > 0)\n",
" throw new Exception(\"Initialization errors, aborting. \" + diagnostics.ErrorsRecursive[0].Message);\n",
"\n",
" engine.Prepare();\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"public static void SetTimePeriod(IMike1DController engine, DateTime from, DateTime to)\n",
"{\n",
" SetResultFiles(engine.Mike1DData, from, to); \n",
" engine.Mike1DData.SimulationStart = from;\n",
" engine.Mike1DData.SimulationEnd = to;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"var engineController = CreateEngine(modelFilePath);\n",
"// SetTimePeriod(engineController, new DateTime(1953, 05, 16, 0, 0, 0), new DateTime(1953, 05, 16, 1, 0, 0))\n",
"Prepare(engineController);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"engineController.CurrentTime"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"engineController.RunUntil(engineController.CurrentTime.AddMinutes(5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"engineController.Finish()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"language_info": {
"file_extension": ".cs",
"mimetype": "text/x-csharp",
"name": "C#",
"pygments_lexer": "csharp",
"version": "8.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
7 changes: 7 additions & 0 deletions notebooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Run Mike1D in Jupyter notebook
Installation instructions:
1. Download and install .NET Core SDK 3.0+ from https://dotnet.microsoft.com/download
2. Install dotnet interactive by running the comand: dotnet tool install --global Microsoft.dotnet-interactive or dotnet tool install --global dotnet-try

3. Install C# kernel for jupyter by running dotnet try jupyter install in your python environment
4. Open Jupyter notebook (pip install jupyter in python)