-
Notifications
You must be signed in to change notification settings - Fork 114
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
Prototype System class. #81
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
77cd5d4
Prototype System class.
chrisdembia 61cd1a7
WIP: Started tests for System class.
moorepants 29fc9cb
Removing default behaviors from System class.
chrisdembia 418c1fc
Fix bug with mass spring damper kwarg.
chrisdembia 2a5b721
WIP Write most of the System class, with tests.
chrisdembia 3c6344a
Changed the arguments to the generated rhs to support dictionaries.
moorepants bfe2138
Added test for specified dict.
moorepants 9aef7d8
Updated all code to use new dictionary inputs for generate_ode_function.
moorepants 8f30674
About to redo System defaults.
chrisdembia 35cba4d
Replace calls to find_ with custom methods.
chrisdembia d0d945f
Fix some minor typos.
chrisdembia 0ca1d2b
Not making constants, specifieds, and initial conditions properties.
chrisdembia 331aa33
Finish System class, with passing tests.
chrisdembia 826f4d5
Update double_pendulum example to use System.
chrisdembia 964dbe7
Update documentation for System class.
chrisdembia 1778fee
Minor cleanup.
moorepants fd981e7
Restore the fast way of parsing specifieds.
chrisdembia f6c03a3
Edits to the README.
moorepants File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Restore the fast way of parsing specifieds.
- Loading branch information
commit fd981e761e4b8c045231c0752c24021280efa100
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,24 +174,46 @@ def _constants_padded_with_defaults(self): | |
def specifieds(self): | ||
"""A dict that provides numerical values for the specified quantities | ||
in the problem (all dynamicsymbols that are not defined by the | ||
equations of motion). Keys are the symbols for the specified | ||
quantities, or a tuple of symbols, and values are the floats, arrays of | ||
floats, or functions that generate the values. If a dictionary value is | ||
a function, it must have the same signature as ``f(x, t)``, the ode | ||
right-hand-side function (see the documentation for the ``ode_solver`` | ||
attribute). You needn't provide values for all specified symbols. Those | ||
for which you do not give a value will default to 0.0. | ||
equations of motion). There are two possible formats. (1) is more | ||
flexible, but (2) is more efficient (by a factor of 3). | ||
|
||
(1) Keys are the symbols for the specified quantities, or a tuple of | ||
symbols, and values are the floats, arrays of floats, or functions that | ||
generate the values. If a dictionary value is a function, it must have | ||
the same signature as ``f(x, t)``, the ode right-hand-side function | ||
(see the documentation for the ``ode_solver`` attribute). You needn't | ||
provide values for all specified symbols. Those for which you do not | ||
give a value will default to 0.0. | ||
|
||
(2) There are two keys: 'symbols' and 'values'. The value for 'symbols' | ||
is an iterable of *all* the specified quantities in the order that you | ||
have provided them in 'values'. Values is an ndarray, whose length is | ||
`len(sys.specifieds_symbols)`, or a function of x and t that returns an | ||
ndarray (also of length `len(sys.specifieds_symbols)`). NOTE: You must | ||
provide values for all specified symbols. In this case, we do *not* | ||
provide default values. | ||
|
||
NOTE: If you switch formats with the same instance of System, you | ||
*must* call `generate_ode_function()` before calling `integrate()` | ||
again. | ||
|
||
Examples | ||
-------- | ||
Keys can be individual symbols, or a tuple of symbols. Length of a | ||
value must match the length of the corresponding key. Values can be | ||
functions that return iterables:: | ||
Here are examples for (1). Keys can be individual symbols, or a tuple | ||
of symbols. Length of a value must match the length of the | ||
corresponding key. Values can be functions that return iterables:: | ||
|
||
sys = System(km) | ||
sys.specifieds = {(a, b, c): np.ones(3), d: lambda x, t: -3 * x[0]} | ||
sys.specifieds = {(a, b, c): lambda x, t: np.ones(3)} | ||
|
||
Here are examples for (2): | ||
|
||
sys.specifieds = {'symbols': (a, b, c, d), | ||
'values': np.ones(4)} | ||
sys.specifieds = {'symbols': (a, b, c, d), | ||
'values': lambda x, t: np.ones(4)} | ||
|
||
""" | ||
return self._specifieds | ||
|
||
|
@@ -216,29 +238,55 @@ def _assert_symbol_appears_multiple_times(self, symbol, symbols_so_far): | |
raise ValueError("Symbol {} appears more than once.".format( | ||
symbol)) | ||
|
||
def _specifieds_are_in_format_2(self, specifieds): | ||
keys = specifieds.keys() | ||
if ('symbols' in keys and 'values' in keys): | ||
return True | ||
else: | ||
return False | ||
|
||
def _check_specifieds(self, specifieds): | ||
symbols = self.specifieds_symbols | ||
|
||
symbols_so_far = list() | ||
|
||
for k, v in specifieds.items(): | ||
if self._specifieds_are_in_format_2(specifieds): | ||
|
||
# The symbols must be specifieds. | ||
if isinstance(k, tuple): | ||
for ki in k: | ||
self._assert_is_specified_symbol(ki, symbols) | ||
else: | ||
self._assert_is_specified_symbol(k, symbols) | ||
for sym in specifieds['symbols']: | ||
self._assert_is_specified_symbol(sym, symbols) | ||
|
||
# Each specified symbol can appear only once. | ||
if isinstance(k, tuple): | ||
for ki in k: | ||
self._assert_symbol_appears_multiple_times(ki, | ||
symbols_so_far) | ||
symbols_so_far.append(ki) | ||
else: | ||
self._assert_symbol_appears_multiple_times(k, symbols_so_far) | ||
symbols_so_far.append(k) | ||
for sym in specifieds['symbols']: | ||
self._assert_symbol_appears_multiple_times(sym, symbols_so_far) | ||
symbols_so_far.append(sym) | ||
|
||
# Must have provided all specifieds. | ||
for sym in self.specifieds_symbols: | ||
if sym not in specifieds['symbols']: | ||
raise ValueError( | ||
"Specified symbol {} is not provided.".format(sym)) | ||
|
||
else: | ||
|
||
for k, v in specifieds.items(): | ||
|
||
# The symbols must be specifieds. | ||
if isinstance(k, tuple): | ||
for ki in k: | ||
self._assert_is_specified_symbol(ki, symbols) | ||
else: | ||
self._assert_is_specified_symbol(k, symbols) | ||
|
||
# Each specified symbol can appear only once. | ||
if isinstance(k, tuple): | ||
for ki in k: | ||
self._assert_symbol_appears_multiple_times(ki, | ||
symbols_so_far) | ||
symbols_so_far.append(ki) | ||
else: | ||
self._assert_symbol_appears_multiple_times(k, symbols_so_far) | ||
symbols_so_far.append(k) | ||
|
||
def _symbol_is_in_specifieds_dict(self, symbol, specifieds_dict): | ||
for k in specifieds_dict.keys(): | ||
|
@@ -331,14 +379,18 @@ def generate_ode_function(self, generator='lambdify', **kwargs): | |
A function which evaluates the derivaties of the states. | ||
|
||
""" | ||
if self._specifieds_are_in_format_2(self.specifieds): | ||
specified_value = self.specifieds['symbols'] | ||
else: | ||
specified_value = self.specifieds_symbols | ||
self._evaluate_ode_function = generate_ode_function( | ||
# args: | ||
self.eom_method.mass_matrix_full, | ||
self.eom_method.forcing_full, | ||
self.constants_symbols, | ||
self.coordinates, self.speeds, | ||
# kwargs: | ||
specified=self.specifieds_symbols, | ||
specified=specified_value, | ||
generator=generator, | ||
**kwargs | ||
) | ||
|
@@ -378,23 +430,25 @@ def integrate(self, times): | |
initial_conditions_in_proper_order = \ | ||
[init_conds_dict[k] for k in self.states] | ||
|
||
if self._specifieds_are_in_format_2(self.specifieds): | ||
specified_value = self.specifieds['values'] | ||
else: | ||
specified_value = self._specifieds_padded_with_defaults() | ||
|
||
return self.ode_solver( | ||
self.evaluate_ode_function, | ||
initial_conditions_in_proper_order, | ||
times, | ||
args=({ | ||
'constants': self._constants_padded_with_defaults(), | ||
'specified': self._specifieds_padded_with_defaults(), | ||
'specified': specified_value, | ||
},) | ||
) | ||
|
||
def _Kane_inlist_insyms(self): | ||
"""TODO temporary.""" | ||
uaux = self.eom_method._uaux | ||
uauxdot = [diff(i, t) for i in uaux] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this should fail as diff isn't imported anywhere. |
||
# dictionary of auxiliary speeds & derivatives which are equal to zero | ||
subdict = dict( | ||
list(zip(uaux + uauxdot, [0] * (len(uaux) + len(uauxdot))))) | ||
|
||
# Checking for dynamic symbols outside the dynamic differential | ||
# equations; throws error if there is. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you make sure that your list of specifieds is the same order as those found from the kane object?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't. If the user is using the symbols/values format, they are in control of the order of symbols. See https://github.com/pydy/pydy/pull/81/files#diff-105304ef1b8479ef4d5d9e41b786cc34R383