====== Feedback views ======
Feed back views are a way to run part of the model in a loop adjusting model inputs for each iteration and providing back to the view of the model outputs required. The reasons for using feedback views include handling model feedback links, and trying to get model outputs to track on some know data. There are many other ways of using feed back too.
There are a few built in tool variables that are available to the view writer to track the feedback process and terminate the loop when ready they are:
* **$fbViewIteration** - the iteration number of the loop
* **$fbViewMaxIters** - the maximum number of iterations requested by the user
* **$fbViewTolerance** - the convergence tolerance requested by the user
Note:
* You can declare regular **local** variables in a feedback view, however be aware that local variables are wiped out at the start of each iteration. If you want to reuse you local variables for each iteration you need to declare it as **keep** variable, which remain available to the view for every iteration. Usually you create keep variables on the first iteration inside an "if clause" as seen below:
keep A[]
if ($fbViewIteration)
A[] = create(...)
endif
==== Example feedback view files ====
Here is the code for the .v and .t files for a simple feedback view that modifies a model input until the model output matches some given "golden" value in the last time slice.
viewName.v:
varToAdjust F
modelCalculatedVal U
goldenValToTrackOn U
viewname.t:
creview varToAdjust[dims][]
view modelCalculatedVal[dims][]
view goldenValToTrackOn[dims][]
! These are the built in variables available in the .t file
! $fbViewIteration
! $fbViewMaxIters
! $fbViewTolerance
if $background == false
say ("This view requires a background.")
quit (-1)
endif
keep percentDecreaseScnLI[dims]
keep goldenToModelRatioOrig[dims]
local goldenToModelRatio[dims] = goldenValToTrackOn[dims][1] / modelCalculatedVal[dims][1]
if $fbViewIteration == 1
say ("Iteration: 1")
! Make your adjustment for the first iteration -- in this case we will decrease the value by some percent ...this is just an example
local percentDecrease_bck[dims] = create (; dimFrom1=varToAdjust[dims][1], dim=SEQ;time:1981:2006:1;year, data=1)
percentDecrease_scn[dims] = copyshape (percentDecrease_bck[dims])
percentDecrease_scn[dims] = table (;with=percentDecrease_bck[dims], title="..enter required percent decrease..")
varToAdjust[dims][2] = percentDecrease_scn[dims] / 100 * -1 + 1 * varToAdjust[dims][1]
goldenToModelRatioOrig[dims] = goldenToModelRatio[dims]
percentDecreaseScnLI[dims] = percentDecrease_scn[dims]
quit (1)
else
say ("Iteration: ", $fbViewIteration)
local goldenToModelRatioLI[dims] = goldenToModelRatio[dims]
! Calculate the adjusted variable value
local ratioDiff[dims] = percentDecreaseScnLI[dims] - goldenToModelRatioOrig[dims]
local percentDecrease_scn[dims] = 1 - goldenToModelRatioOrig[dims] * percentDecreaseScnLI[dims] / ratioDiff[dims]
varToAdjust[dims][2] = percentDecrease_scn[dims] / 100 * -1 + 1 * varToAdjust[dims][1]
export (percentDecrease_scn[dims]; format=tool, file=$persistence/percentDecreaseScnLI)
!check tolerances for convergence
!**NOTE** this one only checks for convergence in the last year!
local tol[] = create(; data=$fbViewTolerance)
local numNotConv[] = sum (boolgt (abs (extract (goldenToModelRatio[dims]; time:-0, shrink=on) - 1), tol[]))
integer $numNotConv
getobjinfo(numNotConv[], $numNotConv; info=dataElement)
if ($numNotConv == 0) or ($fbViewIteration == $fbViewMaxIters)
mumble (" numNotConv = ", $numNotConv, " at iteration ",$fbViewIteration, " stopping due to ")
if ($numNotConv == 0)
say ("convergence")
else
say ("maximum iterations")
endif
quit(0)
else
say (" numNotConv = ", $numNotConv, " at iteration ",$fbViewIteration, " continuing")
quit (1)
endif
endif
==== Use of quit () ====
''quit (1)'' exits the current iteration.
''quit (0)'' exits the entire feedback view.
==== Declaring in views.samm ====
Declare the feedback view in views.samm:
fbview trackEU compare trackEU.v trackEU.t "Tracking Energy Use“ 20 0.001
where the line contains the following parameters:
type of view (= fbview)\\
view name\\
model scope\\
.v file name\\
.t file name\\
description\\
default number of iterations \\
convergence tolerance \\
==== Object Array Size and Contents ====
Note that feedback views require a background scenario to run (i.e. $background must be true).
Also, the "current" instance identified in the table below refers to the instance of a use-type (U) object in a //fbview// which - in the first iterations contains the value for the background (or worksheet) scenario - and in subsequent scenarios contains the value corresponding to the most recent iteration. If the background value of such a variable is required in iterations greater than 1, the value should be persisted using a keep object during the first iteration.
^ $history ^ Object Type ^ Object Array Contents ^ Value of $numScns ^
| true | U | object[][1] = history instance \\ object[][2] = current instance | 2 |
| true | F | object[][1] = history instance \\ object[][2] = background instance \\ object[][3] = new instance to be filled | 3 |
| false | U | object[][1] = current instance | 3 |
| false | F | object[][1] = background instance \\ object[][2] = new instance to be filled | 3 |