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

Units from declarative macro instead of manually coded definitions #24

Merged
merged 37 commits into from
Jul 27, 2022

Conversation

Vindaar
Copy link
Member

@Vindaar Vindaar commented Jul 19, 2022

Further progress towards #8.

After cleaning this up (especially the duplicated parsing logic & partially duplicated types in main & for definition), we are essentially ready to replace most of the current logic based on UnitKind etc. by a more type aware logic based on DefinedUnit elements. This will significantly clean up how individual units are added and maintained.

edit: making good progress...
edit 2: it is done.

With this now complete, #8 should essentially be solved now. This means in theory we can define any unit system as follows:

declareQuantities:
  Base:
    Time
    Length
    Mass
    ....
  Derived:                 # derived quantities are those based on a 'linear combination' of base quantities
    Frequency:             [(Time, -1)]
    Velocity:              [(Length, 1), (Time, -1)]
    Acceleration:          [(Length, 1), (Time, -2)]
    ...

declareUnits:
  BaseUnits:               # define the base units of the system. Must correspond to `Base` quantities
    Gram:                  # the long name
      short: g             # the short name
      prefix: siKilo       # can be used to define a different prefix as the base type
      quantity: Mass       # the quantity it corresponds to
    Meter:
      short: m
      quantity: Length
    Second:
      short: s
      quantity: Time

  Derived:                 # derived units are those corresponding to derived quantities
    Newton:
      short: N
      quantity: Force      # generate SI based derivative based on Quantity & quantity dimensionality
    Joule:
      short: J
      quantity: Energy
    ...
    Radian:
      short: rad
      quantity: Angle
      autoConvert: false   # autoConvert can be used to forbid conversions
    ...
                           # multiple units for one quantity can be defined
    Gauss:
      short: G
      quantity: MagneticFieldStrength
      conversion: 1e-4.T   # their conversion to the main unit of that quantity must be defined
    ...

                           # natural unit conversions from given units can be defined as such:
  NaturalUnits:
    Gram: 1.7826627e-33.eV # relative to g and not kg!
    Meter: 1.9732705e-7.eV⁻¹
    ...

                           # generate all units with SI prefixes of the base
generateSiPrefixedUnits:
  (m, Meter)
  (s, Second)
  ...
  (T, Tesla) exclude [f]   # individual prefixes can be excluded if ambiguous

So feel free to define the CGS or whatever you please.

Also adds a CT table storing all the quantities generated.
This moves all the unit parsing logic into a separate module (not used
as of this commit)
All units are now defined using a declarative macro. All logic is
built on that. This means it should (gotta try first to know, huh?)
now be trivial to define other unit systems etc. by just replacing the
declarations.
Provide a debug message if compiled with -d:debugUnits
Instead we now simply depend on the fact that all units are defined on
top of our known distinct types. We simply resolve until we find a
known distinct or nothing.
`api.nim` imports and exports the whole user facing API. `ct_api` as
the name suggests provides the compile time API to declare quantities
and units.
This is just to showcase how one defines custom quantities and unit systems.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant