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

continuum_mechanics can now use units #14856

Merged
merged 1 commit into from Jul 18, 2018

Conversation

Projects
None yet
3 participants
@jashan498
Contributor

jashan498 commented Jul 1, 2018

Brief description of what is fixed or changed

In current master, if we try to use Beam class with units:

>>> from sympy import *
>>> from sympy.physics.units import meter, kilogram, newton, second
>>> from sympy.physics.continuum_mechanics.beam import Beam
>>> E, I = symbols('E, I')
>>> R1, R2 = symbols('R1, R2')
>>> b = Beam(8*meter, 200*newton/meter**2, 400*1000000*meter**4)
>>> b.apply_load(5*newton, 2*meter, -1)
>>> b.apply_load(R1, 0*meter, -1)
>>> b.apply_load(R2, 8*meter, -1)
>>> b.apply_load(10*newton/meter, 4*meter, 0, end=8*meter)
>>> b.bc_deflection = [(0*meter, 0*meter), (8*meter, 0*meter)]
>>> b.solve_for_reaction_loads(R1, R2)
Traceback (most recent call last):
  File "/home/jashan/sympy/sympy/core/compatibility.py", line 333, in as_int
    result = int(n)
  File "/home/jashan/sympy/sympy/core/expr.py", line 224, in __int__
    raise TypeError("can't convert symbols to int")
TypeError: can't convert symbols to int

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/jashan/sympy/sympy/core/containers.py", line 87, in __mul__
    n = as_int(other)
  File "/home/jashan/sympy/sympy/core/compatibility.py", line 337, in as_int
    raise ValueError('%s is not an integer' % (n,))
ValueError: meter is not an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/jashan/sympy/sympy/physics/continuum_mechanics/beam.py", line 692, in solve_for_reaction_loads
    moment_curve = limit(self.bending_moment(), x, l)
  File "/home/jashan/sympy/sympy/series/limits.py", line 68, in limit
    return Limit(e, z, z0, dir).doit(deep=False)
  File "/home/jashan/sympy/sympy/series/limits.py", line 213, in doit
    r = gruntz(e, z, z0, dir)
  File "/home/jashan/sympy/sympy/series/gruntz.py", line 658, in gruntz
    r = limitinf(e0, z)
  File "/home/jashan/sympy/sympy/series/gruntz.py", line 428, in limitinf
    c0, e0 = mrv_leadterm(e, x)
  File "/home/jashan/sympy/sympy/series/gruntz.py", line 513, in mrv_leadterm
    series = calculate_series(f, w, logx=logw)
  File "/home/jashan/sympy/sympy/series/gruntz.py", line 472, in calculate_series
    if t.simplify():
  File "/home/jashan/sympy/sympy/core/expr.py", line 3175, in simplify
    return simplify(self, ratio, measure)
  File "/home/jashan/sympy/sympy/simplify/simplify.py", line 607, in simplify
    expr = quantity_simplify(expr)
  File "/home/jashan/sympy/sympy/physics/units/util.py", line 164, in quantity_simplify
    return coeff*Mul.fromiter(other_pow)*Mul.fromiter(new_quantities)
  File "/home/jashan/sympy/sympy/core/containers.py", line 89, in __mul__
    raise TypeError("Can't multiply sequence by non-integer of type '%s'" % type(other))
TypeError: Can't multiply sequence by non-integer of type '<class 'sympy.physics.units.quantities.Quantity'>'

In the above case it was happening because this line passed 2*meter*(55*newton + 4*R1) into quantity_simplify method of physics.units.util, which created problem in it.
So I replaced expr with expr.expand() inside quantity_simplify, so that even if expression like above comes it get converted into simpler form.(like 110*meter*newton + 8*meter*R1 in our case)

Other comments

Another issue in using units with Beam class was Prefix objects (like kilo) gave ValueError on being passed into linsolve. So in this PR, anytime a quantity with units is passed, the Prefix object is removed from it.

@parsoyaarihant parsoyaarihant added the GSoC label Jul 2, 2018

@parsoyaarihant

This comment has been minimized.

Contributor

parsoyaarihant commented Jul 2, 2018

Should we rather create a sympy issue for linsolve to see if its possible to fix within linsolve.

This can be a temporary fix because when we have lot of modules like Beam in the future, we rather not remove Prefix object from all of them.

@jashan498

This comment has been minimized.

Contributor

jashan498 commented Jul 2, 2018

Created #14860 for that.

@jashan498 jashan498 changed the title from few changes in physics.units and continuum_mechanics to continuum_mechanics can use units now Jul 18, 2018

@jashan498

This comment has been minimized.

Contributor

jashan498 commented Jul 18, 2018

@parsoyaarihant @moorepants the error causing bug got fixed in #14865. I have added a test case for Beam using units in this PR.

@parsoyaarihant parsoyaarihant changed the title from continuum_mechanics can use units now to continuum_mechanics can now use units Jul 18, 2018

@parsoyaarihant parsoyaarihant merged commit 1b6c723 into sympy:master Jul 18, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment