- Editted to take into account initial feedback
To do a sensitivity-analsysis on some of the parameters in my model, I want to overwrite and redefine a single variable. This variable can be handpicked by the user. I cannot get the overwriting to work.
For now, I have focussed mainly on getting the locals() or globals() to the right value, but as shown below. this did not work.
Two important parts of my code: first the Main function, doing all the work to create the model. It has many variables, one is shown: the Wave_height
def Main(loopname, loopvalue = 0):
Wave_height = 2 #m
# And_many_other_characteristics
# redefine one single variable name
vars()[loopname] = loopvalue
globals()[loopname] = loopvalue
locals()[loopname] = loopvalue
print (Wave_height)
return_info = 1 ## rows of code i dont want to bother you with ##
return(return_info)
if __name__ == "__main__":
Main("testname", 0)
For the sensitivity analysis, I would like to have all variables constant, except for a single variable. Lets assume I want to vary the Wave_height. I want to do something like the following code:
import Main
import numpy as np
loopname = "Wave_height"
loopvalue = [1,2,3]
max_z_displacement = np.zeros(len(loopvalue))
for i in range (len(loopvalue)):
return_info = Main.Main(loopname, loopvalue[i])
max_z_displacement[i] = return_info[2]
Currently, the prints are as follows:
2
I would like to have the result being:
1
I would like to avoid using 50 different variables in the def-row, as that would not make it easy to use.