Open Davide-sd opened 1 year ago
However, Numpy has no idea what
SingularityFunction
is, hence it fails. Then, the plotting module evaluates the expression with SymPy, which is slow.
Maybe lambdify
should convert to Piecewise.
The singularity functions should always be able to be rewritten as piecewise. These functions are a shorthand for piecewise functions, in some sense.
Maybe lambdify should convert to Piecewise.
This would be awesome.
The
sympy.physics.continuum_mechanics.beam
uses theSingularityFunction
to create symbolic expression for the loads. The plotting module converts the symbolic expression to a lambda function to be evaluated with NumPy. However, Numpy has no idea whatSingularityFunction
is, hence it fails. Then, the plotting module evaluates the expression with SymPy, which is slow.Converting the symbolic expression to a
Piecewise
allows the plotting module to use NumPy (potentially better performance and better quality). I wonder if it's always possible to convert aSingularityFunction
to aPiecewise
in the context of the continuum mechanics module.Here is a little benchmark:
I'm going to generate numerical data for plotting using
adaptive=True
andadaptive=False
, for both expressions. For an ordinary Matplotlib plot, the width in pixels covered by a curve is between 500-600px. For the uniform meshing strategy, I use n=1000 discretization points. It means there will be (on average) 1000points/550px = 1.818181 points/px in the horizontal direction, which is more than enough to properly capture discontinuities (jumps in the y-direction).These are the results (computed with a modern high end cpu):
The last and first timings show that it would be possible to plot it with
adaptive=False
and Numpy at roughly the same "speed" asadaptive=True
with SymPy, but potentially increasing the quality of the plot. The second timing shows that evaluating an expression containingSingularityFunction
with the uniform meshing strategy is out of the question: it would take well over one second to generate a plot with all the loads.For example, a plot of an expression containing
SingularityFunction
withadaptive=True
evaluated with SymPy:There is a discontinuity at
x=4
and it would appear there is another discontinuity at roughlyx=4.2
: that's an artifact of the adaptive algorithm, which has reached its maximum depth of recursion. Note that by running this command again, the artifact could disappear because the adaptive algorithm uses random numbers. One could be tempted to use a seed in the adaptive algorithm in order to always get the same results. However, that's not going to resolve the depth of recursion limit. Usingadaptive=False
eliminates the artifacts, producing a better plot taking the same computational time:No artifacts at
x=4.2
.