Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Vensim parser for scenario 5 #294

Merged
merged 6 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions mira/modeling/amr/stockflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def __init__(self, model: Model):
# test to see if parameter is present in any of the rate laws
used_parameter_flag = False
for flow in model.transitions.values():
if flow.template.rate_law is None:
continue
if sympy.Symbol(key) in flow.template.rate_law.free_symbols:
used_parameter_flag = True
break
Expand Down Expand Up @@ -181,14 +183,15 @@ def __init__(self, model: Model):

self.flows.append(flow_dict)

for symbol in flow.template.rate_law.free_symbols:
link_dict = {'id': f'link{link_id}'}
str_symbol = str(symbol)
if flow.template.rate_law is not None:
for symbol in flow.template.rate_law.free_symbols:
link_dict = {'id': f'link{link_id}'}
str_symbol = str(symbol)

link_dict['source'] = str_symbol
link_dict['target'] = "flow" + fid
link_id += 1
self.links.append(link_dict)
link_dict['source'] = str_symbol
link_dict['target'] = "flow" + fid
link_id += 1
self.links.append(link_dict)

def to_json(self):
"""Return a JSON dict structure of the Stock and Flow model."""
Expand Down
8 changes: 5 additions & 3 deletions mira/sources/system_dynamics/vensim.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,22 @@ def extract_vensim_variable_expressions(model_text):
# variable name and accompanying expression
var_declaration = text.split("~")[0].split("=")
old_var_name = var_declaration[0].strip()
text_expression = var_declaration[1]
text_expression = var_declaration[1].strip()

# account for variables with expressions that have "=" in them besides the
# initial "=" character for var declaration, stitch together the expression
if len(var_declaration) > 2:
for part_expression_text in var_declaration[2:]:
text_expression += "=" + part_expression_text

# current way of identifying built-in vensim functions.
# vensim has several builtin functions, like MIN(), MAX(), XIDZ(), INTEG()
# we pass these along for sympy to just consider like function calls.
# Hackathon file does not use any built-in functions that don't take a single argument
# Can account for single argument Vensim functions as well
# List of Vensim functions: https://www.vensim.com/documentation/22300.html
# "INTEG" is the keyword used to define a state/stock
if "," in text_expression and "INTEG" not in text_expression:
# however, we can't yet handle if/then/else constructs, so we skip them
if "if then else" in text_expression.lower():
expression_map[old_var_name] = "0"
continue

Expand Down
640 changes: 398 additions & 242 deletions notebooks/hackathon_2024.02/scenario5/Scenario 5 Notebook.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion notebooks/hackathon_2024.02/scenario5/grounding_map.csv
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ infected_sym_hospital_overflow,ido:0000511/infected population,hospitalization=n
infected_sym_serious_hospital,ido:0000511/infected population,hospitalization=ncit:C25179/Hospitalization|serious_injury_illness_impairment=ncit:C172031
infected_sym_icu_overflow,ido:0000511/infected population,intensive_care_unit=ncit:C53511|surge_capacity=ncit:C173775
infected_sym_extreme_icu,ido:0000511/infected population,intensive_care_unit=ncit:C53511|immediately_life_threatening_disorder=ncit:C171511
recovered_from_hospital,ido:0000592/immune population,hospitalization=ncit:C25179/Hospitalization|patient_discharge=ncit:c25166
recovered_from_hospital,ido:0000592/immune population,hospitalization=ncit:C25179/Hospitalization|patient_discharge=ncit:c25166
Binary file modified notebooks/hackathon_2024.02/scenario5/scenario5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading