Skip to content
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
2 changes: 1 addition & 1 deletion examples/benchmarking.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def future_loading(t, x=None):
# Step 3: Benchmark simulation for 600 seconds
print('Benchmarking:')
def sim():
(times, inputs, states, outputs, event_states) = batt.simulate_to(600, future_loading, {'t': 18.95, 'v': 4.183})
(times, inputs, states, outputs, event_states) = batt.simulate_to(600, future_loading)
time = timeit(sim, number=500)

# Print results
Expand Down
4 changes: 2 additions & 2 deletions examples/dynamic_step_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def next_time(t, x):
# Here we're printing every time step so we can see the step size change
print('\n\n------------------------------------------------')
print('Simulating to threshold\n\n')
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, save_freq=1e-99, print=True, dt=next_time, threshold_keys=['impact'])
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, save_freq=1e-99, print=True, dt=next_time, threshold_keys=['impact'])

# Example 2
print("EXAMPLE 2: dt of 1 until impact event state 0.5, then 0.25 \n\nSetting up...\n")
Expand All @@ -49,7 +49,7 @@ def next_time(t, x):
# Here we're printing every time step so we can see the step size change
print('\n\n------------------------------------------------')
print('Simulating to threshold\n\n')
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, save_freq=1e-99, print=True, dt=next_time, threshold_keys=['impact'])
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, save_freq=1e-99, print=True, dt=next_time, threshold_keys=['impact'])

# This allows the module to be executed directly
if __name__ == '__main__':
Expand Down
10 changes: 5 additions & 5 deletions examples/future_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def future_loading(t, x=None):
'save_freq': 100, # Frequency at which results are saved
'dt': 2 # Timestep
}
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, {'t': 18.95, 'v': 4.183}, **options)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, **options)

# Now lets plot the inputs and event_states
plot_timeseries(times, inputs, options={'ylabel': 'Variable Load Current (amps)'})
Expand Down Expand Up @@ -68,7 +68,7 @@ def moving_avg(i):

# Now the future_loading eqn is setup to use the moving average of whats been seen
# Simulate to threshold
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, {'t': 18.95, 'v': 4.183}, **options)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, **options)

# Now lets plot the inputs and event_states
plot_timeseries(times, inputs, options={'ylabel': 'Moving Average Current (amps)'})
Expand Down Expand Up @@ -98,7 +98,7 @@ def future_loading(t, x=None):
future_loading.std = 0.2

# Simulate to threshold
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, {'t': 18.95, 'v': 4.183}, **options)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, **options)

# Now lets plot the inputs and event_states
plot_timeseries(times, inputs, options={'ylabel': 'Variable Gaussian Current (amps)'})
Expand Down Expand Up @@ -137,7 +137,7 @@ def moving_avg(i):
moving_avg({'i': load})

# Simulate to threshold
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, {'t': 18.95, 'v': 4.183}, **options)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, **options)

# Now lets plot the inputs and event_states
plot_timeseries(times, inputs, options={'ylabel': 'Moving Average Current (amps)'})
Expand All @@ -159,7 +159,7 @@ def future_loading(t, x=None):
future_loading.start = 0.5

# Simulate to threshold
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, {'t': 18.95, 'v': 4.183}, **options)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_loading, **options)

# Now lets plot the inputs and event_states
plot_timeseries(times, inputs, options={'ylabel': 'Moving Average Current (amps)'})
Expand Down
2 changes: 1 addition & 1 deletion examples/model_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def future_load(t, x=None):

# Step 8: Simulate to impact
event = 'impact'
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':thrower_height}, threshold_keys=[event], dt = 0.005, save_freq=1, print = True)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt = 0.005, save_freq=1, print = True)

# Print flight time
print('The object hit the ground in {} seconds'.format(round(times[-1],2)))
Expand Down
10 changes: 5 additions & 5 deletions examples/new_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def future_load(t, x=None):

# Step 3: Simulate to impact
event = 'impact'
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1, print = True)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1, print = True)

# Print flight time
print('The object hit the ground in {} seconds'.format(round(times[-1],2)))
Expand All @@ -85,16 +85,16 @@ def future_load(t, x=None):

# The first way to change the configuration is to pass in your desired config into construction of the model
m = ThrownObject(g = grav_moon)
(times_moon, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], options={'dt':0.005, 'save_freq':1})
(times_moon, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, threshold_keys=[event], options={'dt':0.005, 'save_freq':1})

grav_mars = -3.711
# You can also update the parameters after it's constructed
m.parameters['g'] = grav_mars
(times_mars, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], options={'dt':0.005, 'save_freq':1})
(times_mars, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, threshold_keys=[event], options={'dt':0.005, 'save_freq':1})

grav_venus = -8.87
m.parameters['g'] = grav_venus
(times_venus, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], options={'dt':0.005, 'save_freq':1})
(times_venus, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, threshold_keys=[event], options={'dt':0.005, 'save_freq':1})

print('Time to hit the ground: ')
print('\tvenus: {}s'.format(round(times_venus[-1],2)))
Expand All @@ -103,7 +103,7 @@ def future_load(t, x=None):
print('\tmoon: {}s'.format(round(times_moon[-1],2)))

# We can also simulate until any event is met by neglecting the threshold_keys argument
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, options={'dt':0.005, 'save_freq':1})
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, options={'dt':0.005, 'save_freq':1})
threshs_met = m.threshold_met(states[-1])
for (key, met) in threshs_met.items():
if met:
Expand Down
14 changes: 7 additions & 7 deletions examples/noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def future_load(t, x=None):
# Ex1: No noise
process_noise = 0
m = ThrownObject(process_noise = process_noise)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1)
(times, _, states, outputs, _) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1)
print('Example without noise')
print('\t- states: {}'.format(['{}s: {}'.format(round(t,2), x) for (t,x) in zip(times, states)]))
print('\t- impact time: {}s'.format(times[-1]))
Expand All @@ -25,15 +25,15 @@ def future_load(t, x=None):
process_noise = 0.5
m = ThrownObject(process_noise = process_noise) # Noise with a std of 0.5 to every state
print('\nExample without same noise for every state')
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1)
(times, _, states, outputs, _) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1)
print('\t- states: {}'.format(['{}s: {}'.format(round(t,2), x) for (t,x) in zip(times, states)]))
print('\t- impact time: {}s'.format(times[-1]))

# Ex3: noise- more noise on position than velocity
process_noise = {'x': 0.25, 'v': 0.75}
m = ThrownObject(process_noise = process_noise)
print('\nExample with more noise on position than velocity')
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1)
(times, _, states, outputs, _) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1)
print('\t- states: {}'.format(['{}s: {}'.format(round(t,2), x) for (t,x) in zip(times, states)]))
print('\t- impact time: {}s'.format(times[-1]))

Expand All @@ -43,7 +43,7 @@ def future_load(t, x=None):
model_config = {'process_noise_dist': process_noise_dist, 'process_noise': process_noise}
m = ThrownObject(**model_config)
print('\nExample with more uniform noise')
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1)
(times, _, states, outputs, _) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1)
print('\t- states: {}'.format(['{}s: {}'.format(round(t,2), x) for (t,x) in zip(times, states)]))
print('\t- impact time: {}s'.format(times[-1]))

Expand All @@ -53,7 +53,7 @@ def future_load(t, x=None):
model_config = {'process_noise_dist': process_noise_dist, 'process_noise': process_noise}
m = ThrownObject(**model_config)
print('\nExample with triangular process noise')
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1)
(times, _, states, outputs, _) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1)
print('\t- states: {}'.format(['{}s: {}'.format(round(t,2), x) for (t,x) in zip(times, states)]))
print('\t- impact time: {}s'.format(times[-1]))

Expand All @@ -65,7 +65,7 @@ def future_load(t, x=None):
model_config = {'measurement_noise_dist': measurement_noise_dist, 'measurement_noise': measurement_noise}
m = ThrownObject(**model_config)
print('\nExample with measurement noise')
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1)
(times, _, states, outputs, _) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1)
print('\t- states: {}'.format(['{}s: {}'.format(round(t,2), x) for (t,x) in zip(times, states)]))
print('\t- outputs: {}'.format(['{}s: {}'.format(round(t,2), x) for (t,x) in zip(times, outputs)]))
print('\t- impact time: {}s'.format(times[-1]))
Expand All @@ -81,7 +81,7 @@ def apply_proportional_process_noise(self, x, dt = 1):
model_config = {'process_noise': apply_proportional_process_noise}
m = ThrownObject(**model_config)
print('\nExample with proportional noise on velocity')
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1)
(times, _, states, outputs, _) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1)
print('\t- states: {}'.format(['{}s: {}'.format(round(t,2), x) for (t,x) in zip(times, states)]))
print('\t- impact time: {}s'.format(times[-1]))

Expand Down
5 changes: 2 additions & 3 deletions examples/sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def future_load(t, x=None):
eods = np.empty(len(thrower_height_range))
for (i, thrower_height) in zip(range(len(thrower_height_range)), thrower_height_range):
m.parameters['thrower_height'] = thrower_height
z_i = {'x': thrower_height} # First output
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, z_i, threshold_keys=[event], dt =1e-3, save_freq =10)
(times, _, _, _, _) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt =1e-3, save_freq =10)
eods[i] = times[-1]

# Step 5: Analysis
Expand All @@ -41,7 +40,7 @@ def future_load(t, x=None):
eods = np.empty(len(throw_speed_range))
for (i, throw_speed) in zip(range(len(throw_speed_range)), throw_speed_range):
m.parameters['throwing_speed'] = throw_speed
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], options={'dt':1e-3, 'save_freq':10})
(times, _, _, _, _) = m.simulate_to_threshold(future_load, threshold_keys=[event], options={'dt':1e-3, 'save_freq':10})
eods[i] = times[-1]

print('\nFor a reasonable range of throwing speeds, impact time is between {} and {}'.format(round(eods[0],3), round(eods[-1],3)))
Expand Down
4 changes: 2 additions & 2 deletions examples/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def future_loading(t, x=None):
# simulate for 200 seconds
print('\n\n------------------------------------------------')
print('Simulating for 200 seconds\n\n')
(times, inputs, states, outputs, event_states) = batt.simulate_to(200, future_loading, {'t': 18.95, 'v': 4.183}, print = True)
(times, inputs, states, outputs, event_states) = batt.simulate_to(200, future_loading, print = True)

# Simulate to threshold
print('\n\n------------------------------------------------')
Expand All @@ -40,7 +40,7 @@ def future_loading(t, x=None):
'dt': 2, # Timestep
'print': True
}
(times, inputs, states, outputs, event_states) = batt.simulate_to_threshold(future_loading, {'t': 18.95, 'v': 4.183}, **options)
(times, inputs, states, outputs, event_states) = batt.simulate_to_threshold(future_loading, **options)

# This allows the module to be executed directly
if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion examples/sim_battery_eol.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def future_loading(t, x=None):
'threshold_keys': ['InsufficientCapacity'], # Simulate to InsufficientCapacity
'print': True
}
(times, inputs, states, outputs, event_states) = batt.simulate_to_threshold(future_loading, {'t': 18.95, 'v': 4.183}, **options)
(times, inputs, states, outputs, event_states) = batt.simulate_to_threshold(future_loading, **options)

# This allows the module to be executed directly
if __name__ == '__main__':
Expand Down
6 changes: 3 additions & 3 deletions examples/state_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def future_load(t, x=None):

# Step 3: Simulate to impact
event = 'impact'
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1)

# Print states
print('Example 1')
Expand All @@ -40,7 +40,7 @@ def future_load(t, x=None):
x0 = m.initialize(u = {}, z = {})
x0['x'] = -1

(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=1, x = x0)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=1, x = x0)

# Print states
print('Example 2')
Expand All @@ -55,7 +55,7 @@ def future_load(t, x=None):
m.parameters['g'] = -50000000

print('Example 3')
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x':m.parameters['thrower_height']}, threshold_keys=[event], dt=0.005, save_freq=0.3, x = x0, print = True)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, threshold_keys=[event], dt=0.005, save_freq=0.3, x = x0, print = True)

# Note that the limits can also be applied manually using the apply_limits function
print('limiting states')
Expand Down
4 changes: 2 additions & 2 deletions examples/vectorized.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def future_load(t, x=None):

# Step 3: Simulate to threshold
# Here we are simulating till impact using the first state defined above
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x': first_state['x']}, x = first_state, threshold_keys=['impact'], print = True, dt=0.1, save_freq=2)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, x = first_state, threshold_keys=['impact'], print = True, dt=0.1, save_freq=2)

# Now lets do the same thing but only stop when all hit the ground
def thresholds_met_eqn(thresholds_met):
return all(thresholds_met['impact']) # Stop when all impact ground

(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, {'x': first_state['x']}, x = first_state, thresholds_met_eqn=thresholds_met_eqn, print = True, dt=0.1, save_freq=2)
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load, x = first_state, thresholds_met_eqn=thresholds_met_eqn, print = True, dt=0.1, save_freq=2)

# This allows the module to be executed directly
if __name__=='__main__':
Expand Down
2 changes: 0 additions & 2 deletions examples/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ def future_load(t, x=None):

# Step 3: Simulate to impact
event = 'impact'
first_output = {'x':m.parameters['thrower_height']}
options={'dt':0.005, 'save_freq':1}
(times, inputs, states, outputs, event_states) = m.simulate_to_threshold(future_load,
first_output,
threshold_keys=[event],
**options)

Expand Down
5 changes: 4 additions & 1 deletion prog_model_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ def __init__(self, **kwargs):

super().__init__(**kwargs) # Run Parent constructor

def initialize(self, u, z):
# Sometimes initial input (u) and initial output (z) are needed to initialize the model
# In that case remove the '= None' for the appropriate argument
# Note: If they are needed, that requirement propogated through to the simulate_to* functions
def initialize(self, u=None, z=None):
"""
Calculate initial state given inputs and outputs

Expand Down
6 changes: 3 additions & 3 deletions src/prog_models/models/battery_circuit.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Copyright © 2021 United States Government as represented by the Administrator of the
# National Aeronautics and Space Administration. All Rights Reserved.

from .. import prognostics_model
from .. import PrognosticsModel

from math import inf
from numpy import exp, minimum


class BatteryCircuit(prognostics_model.PrognosticsModel):
class BatteryCircuit(PrognosticsModel):
"""
Prognostics model for a battery, represented by an electric circuit

Expand Down Expand Up @@ -114,7 +114,7 @@ class BatteryCircuit(prognostics_model.PrognosticsModel):
'qb': (0, inf)
}

def initialize(self, u={}, z={}):
def initialize(self, u=None, z=None):
return self.parameters['x0']

def dx(self, x, u):
Expand Down
4 changes: 2 additions & 2 deletions src/prog_models/models/battery_electrochem.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ class BatteryElectroChemEOD(PrognosticsModel):
'xnMax': [update_qmax, update_qnmax, update_qnSBmax]
}

def initialize(self, u = {}, z = {}):
def initialize(self, u=None, z=None):
return self.parameters['x0']

def dx(self, x, u):
Expand Down Expand Up @@ -419,7 +419,7 @@ class BatteryElectroChemEOL(PrognosticsModel):
'qMax': (0, inf)
}

def initialize(self, u = {}, z = {}):
def initialize(self, u=None, z=None):
return self.parameters['x0']

def dx(self, x, u):
Expand Down
Loading