forked from quaquel/EMAworkbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_debugger.py
71 lines (58 loc) · 3.24 KB
/
model_debugger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Created on 11 aug. 2011
.. codeauthor:: wauping <w.auping (at) student (dot) tudelft (dot) nl>
jhkwakkel <j.h.kwakkel (at) tudelft (dot) nl>
To be able to debug the Vensim model, a few steps are needed:
1. The case that gave a bug, needs to be saved in a text file. The entire
case description should be on a single line.
2. Reform and clean your model ( In the Vensim menu: Model, Reform and
Clean). Choose
* Equation Order: Alphabetical by group (not really necessary)
* Equation Format: Terse
3. Save your model as text (File, Save as..., Save as Type: Text Format
Models
4. Run this script
5. If the print in the end is not set([]), but set([array]), the array
gives the values that where not found and changed
5. Run your new model (for example 'new text.mdl')
6. Vensim tells you about your critical mistake
"""
fileSpecifyingError = ""
pathToExistingModel = "/salinization/Verzilting_aanpassingen incorrect.mdl"
pathToNewModel = "/models/salinization/Verzilting_aanpassingen correct.mdl"
newModel = open(pathToNewModel, "w")
# line = open(fileSpecifyingError).read()
line = "rainfall : 0.154705633188; adaptation time from non irrigated agriculture : 0.915157119079; salt effect multiplier : 1.11965969891; adaptation time to non irrigated agriculture : 0.48434342934; adaptation time to irrigated agriculture : 0.330990830832; water shortage multiplier : 0.984356102036; delay time salt seepage : 6.0; adaptation time : 6.90258192256; births multiplier : 1.14344734715; diffusion lookup : [(0, 8.0), (10, 8.0), (20, 8.0), (30, 8.0), (40, 7.9999999999999005), (50, 4.0), (60, 9.982194802803703e-14), (70, 1.2455526635140464e-27), (80, 1.5541686655435471e-41), (90, 1.9392517969836692e-55)]; salinity effect multiplier : 1.10500381093; technological developments in irrigation : 0.0117979353255; adaptation time from irrigated agriculture : 1.58060947607; food shortage multiplier : 0.955325345996; deaths multiplier : 0.875605669911; "
# we assume the case specification was copied from the logger
splitOne = line.split(",")
variable = {}
for n in range(len(splitOne) - 1):
splitTwo = splitOne[n].split(":")
variableElement = splitTwo[0]
# Delete the spaces and other rubish on the sides of the variable name
variableElement = variableElement.lstrip()
variableElement = variableElement.lstrip("'")
variableElement = variableElement.rstrip()
variableElement = variableElement.rstrip("'")
print(variableElement)
valueElement = splitTwo[1]
valueElement = valueElement.lstrip()
valueElement = valueElement.rstrip()
variable[variableElement] = valueElement
print(variable)
# This generates a new (text-formatted) model
changeNextLine = False
settedValues = []
for line in open(pathToExistingModel):
if line.find("=") != -1:
elements = line.split("=")
value = elements[0]
value = value.strip()
if value in variable:
elements[1] = variable.get(value)
line = elements[0] + " = " + elements[1]
settedValues.append(value)
newModel.write(line)
newModel.close() # in order to be able to open the model in Vensim
notSet = set(variable.keys()) - set(settedValues)
print(notSet)