connorferster / forallpeople

Python SI units library: your 'daily driver' for calculations.
Apache License 2.0
299 stars 40 forks source link

Changing auto-prefixed values at the end of a sum #28

Closed tmcgonagle closed 3 years ago

tmcgonagle commented 3 years ago

Hi there, I am a structural engineer with plans to start using 'handcalcs' and 'forallpeople' more than my standard excel calc pad type stuff. I am very impressed with how it all works but I am struggling to understand if it is possible to change the unit of a result after calculation? The help files are great but i definitely have a (large) gap in my understanding.

For example I may have a kNm moment over a mm3 section modulus, and the output is in kPa, however ideally i would like to display this in MPa. I have read through the help files and it talks about auto-prefixing and the use of a 'to' function? I am using the structural environment.

%%render 3 F_Ed = (1214N) lever = (1000mm) M_Ed = F_Ed*lever

%%render sigma_ULS = M_Ed/W_el

The sigma_ULS is displayed in kPa, but ideally i would like to display it in a different unit.

I then tried all different permutations of the line below, to no avail:

sigma_ULS.to(sigma_ULS, unit_name: str = "")

I am really confused by how to use the methods? I have tried to find help online but since I am only just getting started with Python, i find asking the right question pretty difficult in itself!

Thanks in advance, Tom

tmcgonagle commented 3 years ago

Apologies - I found the answer in the 'closed' section which i didn't see previously!

connorferster commented 3 years ago

Glad to hear it! Yes, you can use .prefix() to do this :)

tmcgonagle commented 3 years ago

Thanks Connor,

This is most likely a very basic question, but until i can figure out what to type into google, I am struggling to understand how to actually use the .prefix() function, or any of the methods that you mention in the help files. I used .to() when i found the answer in the closed section, but it would appear that this function permits conversion into different units rather than a factor within the same unit (i.e. mm to inch rather than m to mm) which i will definitely make use of in other parts of my code!

I have a simple bit of code:

%%render

Imaj = (I_maj*mm*4) y_slab=y_slabmm y_slab=y_slab.prefix('mm') <-- this bit is the part i am stuck with Wel_slab = Imaj/y_slab

and originally the y_slab value would be displayed as metres, when Ideally i would like it to be shown in millimetres. There is an easy way to do this using your code but I am really missing the mark with it!

Any advice would be much appreciated!

connorferster commented 3 years ago

Hi @tmcgonagle,

You are rightly calling out an under-documented feature that I have implemented. Forgive me, I have been distracted recently with a major contribution to https://github.com/robbievanleeuwen/section-properties.

The .prefix() method accepts an SI prefix as input (currently only one of the prefixes that are factors of 1000) and returns a new Physical that is forced into using that prefix.

e.g.

import forallpeople
forallpeople.environment('structural', top_level=True)

a = 1e6*m
a.prefix('m') # 'm' for 'milli'
>>> 1000000000.000 mm

a.prefix('M') # 'M' for 'mega'
>>> 1.000 Mm

b = 10e3 * N/m
b.prefix('m')
>>> 10000000.000 mN/m

b.prefix('M")
>>> 0.010 MN/m

Does that make sense? My work has had me using more pandas and less handcalcs/forallpeople recently (sadly) and I have not been updating it as frequently as I was 6 months ago. However, I have been working with it more now and my To Do list is growing :)

Thank you for giving this workflow a try. Let me know if there is anything else that you are getting hung up on or if you have any specific feature requests for either handcalcs or forallpeople.

tmcgonagle commented 3 years ago

That is perfect - thank you for your very helpful reply!