Homework
Setup
- Download the module5_model.zip file.
- Create a new project folder for this homework.
- Extract the contents of
module5_model.zip
into adata
subfolder within your project folder. Your project structure should look something like below.
Your_Project_Folder/
├── data/
│ ├── Dyrup_uncalibrated.mupp
│ ├── Dyrup_uncalibrated.sqlite
│ └── ... (other files from the zip)
└── Module5_Homework.ipynb (or your script)
This is the exact same model that was used in Module 4 Homework, and for the content of Module 5.
Exercise 1
- Create a Python script or Jupyter Notebook.
- Import the
mikeplus
package asmp
. - Open the
Dyrup_uncalibrated.sqlite
database from yourdata
subfolder using a context manager (with mp.open(...) as db:
). - Inside the context manager:
- Print the database version.
- Print the name of the active model configuration.
- Print the name of the active scenario.
- Retrieve all MUIDs from the
msm_Catchment
table and print the first 5 MUIDs. - Retrieve the
Area
,ModelAConcTime
, andModelARFactor
for all catchments and display the head of the resulting DataFrame.
Exercise 2
For this exercise, make a copy of Dyrup_uncalibrated.sqlite
(e.g., Dyrup_uncalibrated_ex2_copy.sqlite
) and work with this copy.
- Open your copied database.
- Select the catchments with MUIDs ‘G61F080_7311’ and ‘G61F375_7353’. Display their current
ModelAConcTime
. - Update the
ModelAConcTime
for these two catchments to1800
seconds. - Verify the change by selecting and displaying the
ModelAConcTime
for these two catchments again. - Close the database.
Exercise 3
Continue working with a fresh copy of the original database or the copy from Exercise 2.
- Open your copied database.
- Identify the alternative group that contains catchment data (Hint: its name likely involves “Catchments” or “hydrology”). Store this
AlternativeGroup
object. - Create a new alternative named “High_ConcTime_AllCatchments” within this group, making it a child of the group’s base alternative.
- Create a new scenario named “Scenario_High_Tc”, making it a child of the base scenario.
- Assign the “High_ConcTime_AllCatchments” alternative to the “Scenario_High_Tc” scenario.
- Activate the “Scenario_High_Tc” scenario and confirm it was activated.
- Within this active scenario (“Scenario_High_Tc” with “High_ConcTime_AllCatchments” active for catchments):
- Update the
ModelAConcTime
for all catchments in themsm_Catchment
table to3600
seconds.
- Update the
- Verify the change:
- While “Scenario_High_Tc” is active, select and display the
ModelAConcTime
for a few catchments (e.g., the first 3 MUIDs frommsm_Catchment
). They should show3600
. - Activate the base scenario.
- Select and display the
ModelAConcTime
for the same catchments. They should show their original values (e.g.,1200.0
if unchanged from the original database).
- While “Scenario_High_Tc” is active, select and display the
- Close the database.
Exercise 4
Continue working with the database copy you modified in Exercise 3 (the one with “Scenario_High_Tc”).
- Open the database copy from Exercise 3.
- Retrieve the simulation setup with MUID ‘rainfall’ from the
msm_Project
table. - Update this ‘rainfall’ simulation setup to use your “Scenario_High_Tc” scenario by setting its
ScenarioName
column. - Verify that the
ScenarioName
for the ‘rainfall’ simulation setup inmsm_Project
is now “Scenario_High_Tc”. - Run the ‘rainfall’ simulation.
- Print the list of result files generated.
- Close the database.
Exercise 5
Start with a fresh copy of the original Dyrup_uncalibrated.sqlite
database (e.g., Dyrup_uncalibrated_ex5_copy.sqlite
). This exercise will create many scenarios and run multiple simulations, which can take some time.
The goal is to investigate the impact of ModelAConcTime
and ModelARFactor
for all catchments on the simulation results.
- Define two Python lists:
conc_times_sec = [1200, 1500, 1800, 2100, 2400]
(these are 20, 25, 30, 35, 40 minutes in seconds)r_factors = [0.6, 0.7, 0.8, 0.9, 1.0]
- Open your copied database.
- Get the “Catchments and hydrology data” alternative group object.
- Loop through each combination of
conc_time
fromconc_times_sec
andr_factor
fromr_factors
. For each of the 25 combinations:- Create a unique alternative name, e.g.,
Tc{tc}_Rf{rf}
. - Create a new alternative in the “Catchments and hydrology data” group using the unique name, parented to the group’s base alternative.
- Create a unique scenario name, e.g.,
Tc{tc}_Rf{rf}
. - Create a new scenario using the unique name, parented to the database’s base scenario.
- Assign the newly created alternative to the newly created scenario.
- Activate the new scenario.
- In the active scenario, update
ModelAConcTime
to the currenttc
value andModelARFactor
to the currentrf
value for all catchments in themsm_Catchment
table. - Update the simulation setup with MUID ‘rainfall’ in the
msm_Project
table to use the currentnew_scenario.name
for itsScenarioName
column. - Run the ‘rainfall’ simulation.
- Print the current scenario name and the list of result files generated for this scenario. (Optionally, just print the name of the
.res1d
file containing network HD results).
- Create a unique alternative name, e.g.,
- After the loop finishes, close the database.
- Open the model in MIKE+. Can you see your scenarios? Try activating a few and checking catchment parameters.
This set of exercises will guide you through the core functionalities of MIKE+Py, preparing you for more complex automated workflows. Good luck!
Download the exercise solutions: