Skip to content
Snippets Groups Projects
Commit ae05871a authored by abaucher's avatar abaucher
Browse files

Added comments in System

parent 95cab99c
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,8 @@
__version__ = "0.1"
from pydynamo.core.system import System
# import pydynamo.core.dynamo_converter
from pydynamo.core import psdsystem
from pydynamo.core.dynamo_converter import convert_dynamo_file
from .world3 import World3
# File delays.py Delay functions of dynamo.
# Linear systems of order 1 or 3 for delay and smoothing
"""Functions implementing a delayed smooth effect in pydynamo.
"""
import numpy as np
class Smooth:
......
"""
Functions to convert a file in DYNAMO to pydynamo syntax.
"""
import re
def convert_dynamo_file(filename, new_file):
def convert_dynamo_file(DYNAMO_file, pydynamo_file):
"""Create a new file with pydynamo equations from DYNAMO equations file.
Parameters
----------
DYNAMO_file : str
File name of DYNAMO code.
pydynamo_file : File name of the pydynamo code which will be created.
"""
with open(new_file, 'w+') as nf:
with open(filename, 'r') as f:
for l in f.readlines():
new_l = dy2pydy(l)
nf.write(new_l)
nf.write(dy2pydy(l))
def dy2pydy(l):
"""Convert a DYNAMO equation line to a pydynamo syntax equation.
Parameters
---------
l : str
DYNAMO equation.
"""
l = re.sub('^[ATRCLS] ', '', l)
l = re.sub('^N (\w*)', '\\1.i', l)
l = re.sub('(?!<\w)smooth\((\w+?)\.k(?!\w)', 'smooth(\\1.j',l)
......
"""Functions to add new politics in a System object.
"""
import re
def new_politic(self, name, date, new_val):
......
"""System class which creates a System from a Pysd model.
"""
import networkx as nx
import numpy as np
import re
from .system import System
class PsdSystem(System):
"""System which initialises with a Pysd model.
"""
def __init__(self, model):
"""Intialise from a Pysd model. Comments are retrieved from code.
"""
self.model = model
self.caracs = {}
self.comments = {}
for n in self.model.components._dependencies:
try :
doc = getattr(self.model.components, n).__doc__
......@@ -21,6 +29,7 @@ class PsdSystem(System):
coms.append(l)
car['Comment'] = re.sub(' *', ' ', ' '.join(coms))
self.caracs[n] = car
try:
self.comments[n] = f"{car['Real Name']} [{car['Units']}]: {car['Comment']}"
except Exception as e:
......@@ -37,6 +46,7 @@ class PsdSystem(System):
self.df_run = None
def get_influence_graph(self):
"""Return a networkx Graph in which there is a node from A to B if B needs A to be computed."""
G = nx.Graph()
for var, deps in self.model.components._dependencies.items():
for dep, i in deps.items():
......@@ -45,7 +55,20 @@ class PsdSystem(System):
return G
def get_tabhl_args(self, name):
# print(self.caracs)
"""
Get indications about a tabhl function.
Parameters
----------
name: str
Name of the variable using a tabhl function.
Returns
-------
np.array, np.array, str, str, str:
x, f(x), x label, y label, title
"""
if self.caracs[name]['Type'] == 'lookup':
try:
x, y = np.array(eval(self.caracs[name]['Original Eqn'])).T
......@@ -61,9 +84,6 @@ class PsdSystem(System):
def get_time(self):
return np.arange(self.model.time.initial_time(), self.model.time.final_time()+self.model.time.time_step()/2, self.model.time.time_step())
def get_tabhl_arg(self, name):
pass
def get_var(self, name):
if self.df_run is None:
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment