Skip to content

Instantly share code, notes, and snippets.

@Ricyteach
Last active October 19, 2021 21:48
Show Gist options
  • Save Ricyteach/41bd75d51c96f8ed9cd8b76d8b7da65c to your computer and use it in GitHub Desktop.
Save Ricyteach/41bd75d51c96f8ed9cd8b76d8b7da65c to your computer and use it in GitHub Desktop.
Basic Load Combination API v0.3
"""API for implemenating the concept of civil engineering load combinations.
Compose a string representing a load combination expression:
>>> expr = "1.6*D & 1.2*L & 0.5*(S | Lr | W)"
Create a `Combination` object using the load combination expression, and generate a multiplication matrix:
>>> from ceng.load import Combination
>>> combo_obj = Combination(expr)
>>> combo_obj.matrix
array([[1.6, 1.2, 0.5, 0. , 0. ],
[1.6, 1.2, 0. , 0.5, 0. ],
[1.6, 1.2, 0. , 0. , 0.5]])
The load factor columns are in order of the symbols in the expression; in this case, D, L, S, Lr, W.
Specify a set of loads (D, L, S, Lr, W):
>>> import numpy as np
>>> loads = np.array([1, 2, 3, 4, 5])
...and Calculate the load combination using the expression matrix:
>>> combination = combo_obj.matrix @ loads
>>> combination
array([5.5, 6. , 6.5])
The maximum combination is easily obtained:
>>> np.max(combination)
6.5
Apply the `Combination.function` decorator to execute load combination computation automatically,
without bothering with manual matrix multiplication. This method also support `numpy` broadcasting.
>>> @Combination("1.6*D & 1.2*L & 0.5*(S | Lr | W)").function
... def primary_live_load(D, L, S, Lr, W):
... ... # implemented automatically by decorator
...
>>> np.max(primary_live_load(1, 2, 3, 4, 5))
6.5
There are also `Combination.method`, `Combination.staticmethod`, and `Combination.classmethod` decorators for methods.
>>> class Strength:
... @Combination("1.6*D & 1.2*L & 0.5*(S | Lr | W)").method
... def primary_live_load(self, D, L, S, Lr, W):
... ... # implemented automatically by decorator
...
>>> str = Strength()
>>> np.max(str.primary_live_load(1, 2, 3, 4, 5))
6.5
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment