Skip to content
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

feat: handle units #205

Draft
wants to merge 10 commits into
base: dev
Choose a base branch
from
Draft

feat: handle units #205

wants to merge 10 commits into from

Conversation

mortenengen
Copy link
Member

@mortenengen mortenengen commented Nov 8, 2024

Description

An attempt to handle units. Note: this is still a very immature draft!

Two classes are added to core: UnitSet and UnitConverter. UnitSet is a dataclass responsible for storing a set of units intended to be used by a Material class. UnitConverter uses two UnitSets to convert between them.

Example

This example shows how two sets of units are selected and passed to the unit converter. Furthermore, it shows how to convert stress, lengeth and force with the unit converter.

from structuralcodes.core._units import UnitSet, UnitConverter

from_units = UnitSet(length='mm', force='N')
to_units = UnitSet(length='m', force='kN')

converter = UnitConverter(
    from_units=from_units,
    to_units=to_units,
)

converter.convert_stress_backwards(35e3)
converter.convert_length_forwards(30)
converter.convert_force_forwards(4e3)

To do

  • Add unit tests for UnitSet and UnitConverter.
  • Handle situation where a user specifies a unit that is not available in the UnitSets.
  • Add more basic units to get good coverage for SI and Imperial units.
  • Add optional keyword arguments in base material class.
  • Add default UnitSets in code specific material classes.
  • Use convert-methods in methods of material classes. Remember to store attributes in user units, and always convert to default units when calling code equations.
  • Add tests for material classes.
  • Add units to concrete and reinforcement factories.
  • How to deal with fracture energy which is not given in units consistent with stress and stiffness. Create a converter on the fly that converts from the units in the code to the user units on the material. Call the property fracture_energy_unit or similar.
  • How to deal with densities? Always use seconds for time.
  • Where to describe in API docs?
  • Suggest to also add a property for modulus_unit or similar?

@mortenengen mortenengen added the enhancement New feature or request label Nov 8, 2024
This was linked to issues Nov 26, 2024
@mortenengen
Copy link
Member Author

mortenengen commented Feb 1, 2025

@connorferster, please let me elaborate on your comment to #105 here.

In our group we have been discussing how to handle units for a while. We do not want to create an advanced library for unit handling, since these already exist, like e.g. pint or forallpeople which of course are very well aware of 😃 However, we are investigating how we can make it easier for our users to perform calculations with our material, geometry and section classes using different units, without introducing too much overhead for users and maintainers.

As you mention in your comment, it could be a good idea to leave unit handling outside of structuralcodes, let a well-established library deal with it, and instead make structuralcodes compatible with that library. But from our initial investigations, we realized that a minimum of internal implementation would be to handle length and force units and derive stress units from these, and we could do this on our material class.

In my first attempt to implement this, I actually used your forallpeople library. However, since we only needed some relation between length units and force units, we decided to simply hardcode a dict with these relations, and create a simple unit converter that could be used in our material classes. Of course, as we proceed, we see that there are other units to consider as well, and without a clear direction ahead, my progress on this topic stopped a couple of months ago.

In your original comment, you suggested to make structuralcodes compatible with pint and let users handle their own units. I am not too familiar with that library, so please excuse my question, but what would be required from structuralcodes to be compatible with pint as you see it? Thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: New
Development

Successfully merging this pull request may close these issues.

Improved docstrings with units Make units consistent
1 participant