Enforce algebraic relationships between object attributes with SymPy

34 views
Skip to first unread message

Brett Ables

unread,
Mar 7, 2014, 6:17:31 PM3/7/14
to sy...@googlegroups.com
I'm interested in using SymPy to model engineering systems.  I'd like to establish an object oriented model of the system in which the physical parameters are symbolically linked based on their real-world constraints.  Next I'd like the user to be able to specify values for various parameters and let the model calculate the unknowns, based on the information provided.

For example, if I have a block which has length, height, width and volume, one relationship would be volume = length * height * width.  The block might also have mass and density, which provides another relationship, density = mass / volume. Finally, Suppose the block separates into 8 subBlocks, each of which have the same types of properties of the block (length, height, width, volume, mass, and density). The mass and volume equations exist for each subBlock, as well as an equation that the sum of the masses of all 8 subBlocks equals the mass of the big block.  Other relationships might also be defineable like saying all 8 blocks are identical, and that each subBlock dimension is half that of the big block, etc. You get the idea - I'm looking at modeling physical systems with lots of inter-related properties, and with parent-child or system-subsystem relationships as well.

To see some of the ideas I've toyed with check out my StackOverflow post.  One idea has a single equation with a balance() function called whenever properties are set.  Another idea is to have a Balanced object which handles the equation and auto-generated properties for each parameter.  The HasMass object then inherits from Balanced and gives Balanced the equation and parameters governing mass.  Extending this idea another class Rectangular might specify how the dimensions relate to volume.  Finally Block would inherit from HasMass and Rectangular, giving it the parameters length, height, volume, mass and density, and a system of 2 equations. Getting this inheritance to work correctly to create a system of balanced equations and corresponding object properties is what I haven't figured out how to implement.

Since drafting that post I've also worked more with systems of equations attempting to define a rocket with 2 stages and define relationships for the masses of the rocket and each stage.  I'm trying to set it up so that it's straightforward to assign values (mass of each stage...) and then request solved values (mass of vehicle), but haven't arrived at an elegant way to keep track of the multiple equations and knowns such that an unknown can be queried and solved for.  I'm thinking I might need a Model class which is owned by the root object (the Vehicle) which can traverse the child hierarchy and collect all relationships and known values from the object structure (traversing systems and subsystems of the vehicle). Finally the Model object could solve the system of equations for any queried value, returning either a numeric solution if possible or a symbolic one if the vehicle is under-constrained.  The parent child system concept isn't inherently incompatible with the property subclassing concept mentioned above, but would certainly complicate it.

There are several implementation options here that could change the behavior.  I could, whenever a value is queried that wasn't previously set to the vehicle as a value, set the value.  This has the positive effect that my model completes itself numerically if I provide just enough information, then query the remaining values.  The Model could also auto-balance the equations whenever a value is added, attempting to solve all remaining unknowns and setting values as known whenever a solution is found. The problem becomes determining what parameter to modify to satisfy the relationship.  If my system is fully defined and I change length, do I want height to change to conserve mass? Or do I was mass re-calculated based on the new volume?  A system of locking, prioritizing, or specifying independent and dependent variables would be needed to clarify behavior in this case.  Another option would be to only allow calculation of unknowns and once the system is fully defined, no changes can be made that break the rules (this would almost certainly be unusable for a dynamic model, though).

Another route I haven't gone is to create a Parameter class which can hold a symbol and a known/assigned value, as well as other properties like default values, constraints, units, etc. This might help with differentiating assigned values from symbols.  Assigned values could be stored as a simple value which is later substituted, or as an equality constraint.

Feel free to discuss this here or on Stack Overflow.

Anyone have any thoughts?  Thanks!
Reply all
Reply to author
Forward
0 new messages