Skip to content

Commit

Permalink
Implement the PULSE function by proper XMILE specification way.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Sep 21, 2017
1 parent d6ee6ee commit b571a9a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
26 changes: 25 additions & 1 deletion pysd/py_backend/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@ def pulse(start, duration):
t = time()
return 1 if start <= t < start + duration else 0


def pulse_train(start, duration, repeat_time, end):
""" Implements vensim's PULSE TRAIN function
Expand All @@ -819,6 +818,31 @@ def pulse_train(start, duration, repeat_time, end):
else:
return 0

def pulse_magnitude(magnitude, start, repeat_time=0):
""" Implements xmile's PULSE function
PULSE: Generate a one-DT wide pulse at the given time
Parameters: 2 or 3: (magnitude, first time[, interval])
Without interval or when interval = 0, the PULSE is generated only once
Example: PULSE(20, 12, 5) generates a pulse value of 20/DT at time 12, 17, 22, etc.
In rage [-inf, start) returns 0
In range [start + n * repeat_time, start + n * repeat_time + dt) return magnitude/dt
In rage [start + n * repeat_time + dt, start + (n + 1) * repeat_time) return 0
"""
t = time()
small = 1e-6 # What is considered zero according to Vensim Help
if repeat_time <= small:
if abs(t - start) < time_step:
return magnitude * time_step
else:
return 0
else:
if abs((t - start) % repeat_time) < time_step:
return magnitude * time_step
else
return 0


def lookup(x, xs, ys):
""" Provides the working mechanism for lookup functions the builder builds """
Expand Down
12 changes: 2 additions & 10 deletions pysd/py_backend/xmile/SMILE2Py.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,8 @@
# 3.5.4 Test Input Functions
# http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039983
# ===

# !TODO!
# This function works with wrong way for XMILE specification: http://docs.oasis-open.org/xmile/xmile/v1.0/csprd01/xmile-v1.0-csprd01.html#_Toc398039983
# ----------------------------------------------------------------------------------------------
# PULSE: Generate a one-DT wide pulse at the given time
# Parameters: 2 or 3: (magnitude, first time[, interval])
# Without interval or when interval = 0, the PULSE is generated only once
# Example: PULSE(20, 12, 5) generates a pulse value of 20/DT at time 12, 17, 22, etc.
# ----------------------------------------------------------------------------------------------
"pulse": "functions.pulse"

"pulse": "functions.pulse_magnitude",
"step": "functions.step",

# "ramp" !TODO!
Expand Down

0 comments on commit b571a9a

Please sign in to comment.