Skip to content
Snippets Groups Projects
exercise-runtimeparams.patch 12.8 KiB
Newer Older
diff -ruN exercises/exercise-runtimeparams/CMakeLists.txt exercises/solution/exercise-runtimeparams/CMakeLists.txt
--- exercises/exercise-runtimeparams/CMakeLists.txt	2025-03-03 15:56:59.908625000 +0100
+++ exercises/solution/exercise-runtimeparams/CMakeLists.txt	2025-03-03 15:56:59.911624948 +0100
@@ -2,7 +2,7 @@
 # SPDX-License-Identifier: GPL-3.0-or-later
 
 # the runtime parameter exercise simulation program
-dumux_add_test(NAME exercise_runtimeparams
+dumux_add_test(NAME exercise_runtimeparams_solution
                SOURCES main.cc)
 
 # add a symlink for each input file
diff -ruN exercises/exercise-runtimeparams/params.input exercises/solution/exercise-runtimeparams/params.input
--- exercises/exercise-runtimeparams/params.input	2024-07-11 13:35:11.652137556 +0200
+++ exercises/solution/exercise-runtimeparams/params.input	2025-03-03 15:23:33.247567621 +0100
@@ -12,11 +12,12 @@
 AquiferDepth = 2700.0 # m
 InjectionDuration = 2.628e6 # in seconds, i.e. one month
 # TODO: Task 2: Create a parameter called "TotalAreaSpecificInflow"
+TotalAreaSpecificInflow = -1e-4 # kg/(s m^2)
 
 [SpatialParams]
 PermeabilityAquitard = 1e-15 # m^2
 # TODO: Task 1: Change the Aquitard's Entry Pressure
-Aquitard.BrooksCoreyPcEntry = 4.5e4 # Pa
+Aquitard.BrooksCoreyPcEntry = 4.5e3 # Pa
 Aquitard.BrooksCoreyLambda = 2.0
 Aquitard.Swr = 0.2
 Aquitard.Snr = 0.0
diff -ruN exercises/exercise-runtimeparams/problem.hh exercises/solution/exercise-runtimeparams/problem.hh
--- exercises/exercise-runtimeparams/problem.hh	2025-03-03 15:56:59.909624983 +0100
+++ exercises/solution/exercise-runtimeparams/problem.hh	2025-03-03 15:56:59.911624948 +0100
@@ -81,12 +81,16 @@
         injectionDuration_ = getParamFromGroup<Scalar>("Problem","InjectionDuration");
Martin Schneider's avatar
Martin Schneider committed
         // TODO: dumux-course-task 2
         // Set a variable "TotalAreaSpecificInflow" to read in a value from the parameter tree via the input file
-
+        totalAreaSpecificInflow_ = getParam<Scalar>("Problem.TotalAreaSpecificInflow");
Martin Schneider's avatar
Martin Schneider committed
         // TODO: dumux-course-task 3
         // Set a default value for the above parameter.
-
+        // totalAreaSpecificInflow_ = getParam<Scalar>("Problem.TotalAreaSpecificInflow", -1e-4);
Martin Schneider's avatar
Martin Schneider committed
         // TODO: dumux-course-task 4
         // Provide output describing where the parameter value comes from using parameter bool functions.
+        // if (hasParamInGroup("Problem","TotalAreaSpecificInflow"))
+        //     std::cout << "Parameter value is read from the input file." << std::endl;
+        // else
+        //     std::cout << "Using the default parameter value." << std::endl;
     }
 
 
@@ -149,7 +153,7 @@
Martin Schneider's avatar
Martin Schneider committed
             // TODO: dumux-course-task 2
             // Incorporate "totalAreaSpecificInflow_"  into the injection boundary condition
-            values[Indices::conti0EqIdx + FluidSystem::N2Idx]= -1e-4;
+            values[Indices::conti0EqIdx + FluidSystem::N2Idx] = totalAreaSpecificInflow_;
             values[Indices::conti0EqIdx + FluidSystem::H2OIdx] = 0.0;
         }
 
@@ -204,6 +208,7 @@
     Scalar injectionDuration_; //! Duration of the injection in seconds
Martin Schneider's avatar
Martin Schneider committed
     // TODO: dumux-course-task 2
     // Set a variable "totalAreaSpecificInflow_" to read in a value from the parameter tree via the input file
+    Scalar totalAreaSpecificInflow_; //! Rate of the Injection in kg/(s m^2)
     Scalar time_;
 };
 
diff -ruN exercises/exercise-runtimeparams/properties.hh exercises/solution/exercise-runtimeparams/properties.hh
--- exercises/exercise-runtimeparams/properties.hh	2025-03-03 15:56:59.909624983 +0100
+++ exercises/solution/exercise-runtimeparams/properties.hh	2025-03-03 15:56:59.911624948 +0100
@@ -58,6 +58,6 @@
                                       FluidSystems::H2ON2DefaultPolicy</*fastButSimplifiedRelations=*/ true> >;
 };
 
-} //end namespace Dumux::Properties
+} // end namespace Dumux::Properties
 
 #endif
diff -ruN exercises/exercise-runtimeparams/README.md exercises/solution/exercise-runtimeparams/README.md
--- exercises/exercise-runtimeparams/README.md	2025-03-03 15:23:33.245567666 +0100
+++ exercises/solution/exercise-runtimeparams/README.md	1970-01-01 01:00:00.000000000 +0100
@@ -1,171 +0,0 @@
-# Exercise Runtime Parameters (DuMuX course)
-
-## Problem set-up
-
-Here we will expand on what we've covered in the basics exercise (see `dumux-course/exercises/exercise-basic/README.md`), and the problem set up will remain the same.
-
-## Preparing the exercise
-
-Navigate to the directory `dumux-course/exercises/exercise-runtimeparams/`
-
-
-## Task 1: Understanding Input Parameters
-
-For this task we will edit the following files:
-
-* The shared __problem file__: `problem.hh`
-* And the shared __input file__: `params.input`
-
-Parameters can either be directly defined within your program, or specified via the input file.  Within every main file, (`*.cc`), the following function is called
-
-```c++
-// parse command line arguments and input file
-Parameters::init(argc, argv);
-```
-This will read in the parameters from the input file.
Martin Schneider's avatar
Martin Schneider committed
-The input file should either be named the same as the executable file, with a trailing `*.input`, or be named `params.input` as this is the standard in our CMake system.
-Alternatively, arbitrarily named input files (e.g. `exercise1.input`) can be explicitly written as the first shell argument after the executable file (here `exercise_runtimeparams`) is called.
-
-```bash
-./exercise_runtimeparams
-    #(Calls the file params.input as the default input file.)
-```
-```bash
-./exercise_runtimeparams exercise1.input
-   #(Calls the input file provided (exercise1.input) as the input file.)
-```
-In the input file `params.input` you can find the following section
-
-```ini
-[SpatialParams]
-PermeabilityAquitard = 1e-15 # m^2
-Aquitard.BrooksCoreyPcEntry = 4.5e4 # Pa
-Aquitard.BrooksCoreyLambda = 2.0
-Aquitard.Swr = 0.2
-Aquitard.Snr = 0.0
-```
-When a parameter is defined directly within your program, you'll need to recompile your program every time you change the value. When a parameter is passed via the input file, this is not the case. If we decided to vary the entry pressure in our geologic units a few times via the parameters listed above, there would be no need to recompile between simulation runs.
-
-* > __Task 1__: Change the aquitard's entry pressure in the input file to a lower value and compare the results with the previous solution. You do not need to recompile the executable.
-
-
-## Task 2: Setting a variable to collect a runtime parameter
-
-Let's free one of the variables in this exercise. Within the class `problem.hh`, in the first line of the Neumann boundary condition definition function `neumannAtPos(...)`, the injection rate is defined as $`1.0 \cdot 10^{-4} kg s^{-1} m^{-2}`$.
-
-```c++
-// inject nitrogen. negative values mean injection
-// units kg/(s*m^2)
-values[Indices::conti0EqIdx + FluidSystem::N2Idx]= -1e-4;
-values[Indices::conti0EqIdx + FluidSystem::H2OIdx] = 0.0;
-```
-
-This parameter may need to change, and if we choose to always change this within the class, we will need to recompile every time.
-
-Instead of hard defining this parameter within the function, we can set a variable to read into our parameter tree via the input file and use this in our function instead.
-To do this, there are two functions defined in `dumux/dumux/common/parameters.hh`, `getParam()` and `getParamFromGroup()`.  They use the following format:
-
-```c++
-variable_ = getParam<TYPE>("GROUPNAME.PARAMNAME");
-```
-or
-```c++
-variable_ = getParamFromGroup<TYPE>("GROUPNAME", "PARAMNAME");
-```
-
-`<TYPE>`,`<GROUPNAME>`,`<PARAMNAME>` should be appropriately defined for your variable:
-  * `<TYPE>` is the type of the parameter to read (e.g. `Scalar`)
-  * `<GROUPNAME>` is the group in the input file (e.g. `Problem`)
-  * `<PARAMNAME>` is the name of the parameter in the input file (e.g. `AquiferDepth`)
-
-An example of this is already performed in the problem constructor. The Injection Duration (`injectionDuration_`) is defined via the input file and can then be used later in the problem header.
-
-```c++
-// depth of the aquifer, units: m
-aquiferDepth_ = getParam<Scalar>("Problem.AquiferDepth");
-// the duration of the injection, units: second
-injectionDuration_ = getParamFromGroup<Scalar>("Problem","InjectionDuration");
-```
-
-The injection duration parameter is located in the `[Problem]` group, is named `InjectionDuration`, and has the type `Scalar`.
-
-This variable should then be defined as a `Scalar` at the bottom of this problem class in the private section.
-
-```c++
-Scalar injectionDuration_;
-```
-
-In the input file, within the group `[Problem]`, a value is set to the Parameter name. This is then called in the problem class.
-
-```ini
-[Problem]
-InjectionDuration = 2.628e6 # in seconds, i.e. one month
-```
-
-* > __Task 2__: The goal is to replace the value `-1e-4` in
-   >
-   >```c++
-   >values[Indices::conti0EqIdx + FluidSystem::N2Idx]= -1e-4;
-   >```
-   >with a runtime variable.
-   >
-   > * (2a) Develop a new variable called `totalAreaSpecificInflow_`,
-   > * (2b) Assign a value to this variable from a path in the input file, and
-   > * (2c) Incorporate this variable into the injection boundary condition.
-   >
-   > When your problem file and the input file are edited, compile and run the exercise.
-   >
-   >```bash
-   > make exercise_runtimeparams && ./exercise_runtimeparams params.input
-   >```
-
-
-## Task 3: Default Values for Runtime Parameters
-
-In the case that no path to a required runtime parameter is provided in the input file, the program will no longer run. You can try this if you like by removing the `TotalAreaSpecificInflow` from the input file. The resulting error message should look like this:
-
-```bash
-terminate called after throwing an instance of 'Dumux::ParameterException'
-  what():  Dumux::ParameterException [get:/dumux/dumux/common/loggingparametertree.hh:411]: Key Problem.TotalAreaSpecificInflow not found in the parameter tree
-```
-
-To avoid this, we can place a default value in the variable definition. Whenever the parameter is specified in the input file, this default value in your class will be overwritten. In the case that no value is provided in the input file, this default value will be used. In order to do this, follow the following template.
-
-```c++
-variable_ = getParam<TYPE>("GROUPNAME.PARAMNAME", DEFAULTVALUE);
-```
-or
-```c++
-variable_ = getParamFromGroup<TYPE>("GROUPNAME","PARAMNAME", DEFAULTVALUE);
-```
-
-* > __Task 3__: Set up the `totalAreaSpecificInflow_` variable to record a default value of `-1e-4` and run this with and without a provided value of `-1e-3` in the input file.
-
-
-## Task 4: Other Runtime Parameter Functions
-
-Setting default values for variables defined with runtime parameters can also lead to problems. If one runtime parameter from the input file is set to multiple different variables, each with a different default value, changing the variable in one location can lead to unexpected changes elsewhere. On top of this, in DuMu<sup>x</sup>, there are a few base variables that are set with default values for all DuMu<sup>x</sup> simulations. These can be found in the header file `dumux/common/parameters.hh` in the function `globalDefaultParameters`.
-
-One way to check this is to use either the `hasParam()` or the `hasParamInGroup()` function. These functions returning `bool`s will check to see if a parameter is read in via the input file. These functions are also both defined in the `dumux/dumux/common/parameters.hh` class, and follow a similar format to that of `getParam()` and `getParamFromGroup()`.
-
-An example of this would look like this:
-
-```c++
-if (hasParam("GROUPNAME.PARAMNAME"))
-   std::cout << "Parameter value is read from the input file." << std::endl;
-else
-   std::cout << "Using the default parameter value." << std::endl;
-```
-or,
-```c++
-if (hasParamInGroup("GROUPNAME","PARAMNAME"))
-   std::cout << "Parameter value is read from the input file." << std::endl;
-else
-   std::cout << "Using the default parameter value." << std::endl;
-```
-
-Using these functions we can better check which parameter values are being included in our program.
-
-* > __Task 4__: Using one of the bool `hasParam` functions, place an output in the problem file to alert the user where the parameter value comes from.
diff -ruN exercises/exercise-runtimeparams/spatialparams.hh exercises/solution/exercise-runtimeparams/spatialparams.hh
--- exercises/exercise-runtimeparams/spatialparams.hh	2025-03-03 15:56:59.909624983 +0100
+++ exercises/solution/exercise-runtimeparams/spatialparams.hh	2025-03-03 15:56:59.911624948 +0100
@@ -12,8 +12,8 @@
  *        fully implicit model.
  */
 
-#ifndef DUMUX_EXRUNTIMEPARAMS_INJECTION_SPATIAL_PARAMS_HH
-#define DUMUX_EXRUNTIMEPARAMS_INJECTION_SPATIAL_PARAMS_HH
+#ifndef DUMUX_RUNTIMEPARAMS_INJECTION_SPATIAL_PARAMS_HH
+#define DUMUX_RUNTIMEPARAMS_INJECTION_SPATIAL_PARAMS_HH
 
 #include <dumux/porousmediumflow/fvspatialparamsmp.hh>
 #include <dumux/material/fluidmatrixinteractions/2p/brookscorey.hh>