openPMD/openPMD-api

First Pint Support

开放

#475 创建于 2019年2月16日

 (0 条评论) (0 个反应) (0 位负责人)C++ (57 个派生)auto 404
frontend: Python3good first issuehelp wanted

仓库指标

星标
 (160 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

Simple bindings to pint would be wonderful for dimensional analysis or records.

as drafted here

TODO: add pretty handlers, maybe some lookup DBs for typical quanitites, make useful.

import pint

ureg = pint.UnitRegistry()

# [E] = M L^2 T^-3 I^-1
E_pint = 1 * E_unitDim[api.Unit_Dimension.L] * ureg.m + \
E_unitDim[api.Unit_Dimension.M] * ureg.kg + \
E_unitDim[api.Unit_Dimension.T] * ureg.s + \
E_unitDim[api.Unit_Dimension.I] * ureg.A + \
E_unitDim[api.Unit_Dimension.theta] * ureg.K + \
E_unitDim[api.Unit_Dimension.N] * ureg.mol + \
E_unitDim[api.Unit_Dimension.J] * ureg.cd

# length L, mass M, time T, electric current I, thermodynamic temperature theta, amount of substance N, luminous intensity J

E_pint.check('[length]')
# False
E_pint.check('[length] * [mass] / [current] / [time]^3')
# True
E_pint.check('V/m')
# True

E_x.dimensionality
# <UnitsContainer({'[current]': -1.0, '[length]': 1.0, '[mass]': 1.0, '[time]': -3.0})>
# ^-- access just like a regular dictionary

# create:
pint.context.UnitsContainer({'[current]': -1.0, '[length]': 1.0, '[mass]': 1.0, '[time]': -3.0})
# <UnitsContainer({'[current]': -1.0, '[length]': 1.0, '[mass]': 1.0, '[time]': -3.0})>

As a first step, RecordComponent.unit_dimension should probably just return a dictionary that fits exactly those pint defaults:

{
  '[length]': L,
  '[mass]': M,
  '[time]': T,
  '[current]': I,
  '[temperature]': theta,
  '[substance]': N,
  '[luminosity]': J
}

# easy to pass:
pint.context.UnitsContainer(E_x.unit_dimensions)

https://github.com/hgrecco/pint/blob/master/pint/default_en.txt https://github.com/hgrecco/pint/blob/master/pint/babel_names.py

贡献者指南