Skip to content

Commit

Permalink
Merge branch 'add_python' of https://github.com/LLNL/units into add_p…
Browse files Browse the repository at this point in the history
…ython
  • Loading branch information
phlptp committed Dec 21, 2024
2 parents 785e2b2 + a41e749 commit 4a1330d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ build
*.pyd
*.egg-info
.vscode
.vs
.vs
27 changes: 20 additions & 7 deletions python/units_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ NB_MODULE(units_llnl_ext, mod)
"set_commodity",
[](units::precise_unit* unit, const char* commodity) {
return units::precise_unit(
unit->multiplier(),unit->base_units(), units::getCommodity(std::string(commodity)));
unit->multiplier(),
unit->base_units(),
units::getCommodity(std::string(commodity)));
})
.def(
"set_multiplier",
[](units::precise_unit* unit, double mult) {
return units::precise_unit(mult, unit->base_units(),unit->commodity());
return units::precise_unit(
mult, unit->base_units(), unit->commodity());
})
.def("inv", &units::precise_unit::inv)
.def(nb::self * nb::self)
Expand Down Expand Up @@ -194,11 +197,18 @@ NB_MODULE(units_llnl_ext, mod)
return units::precise_measurement(value, measurement->units());
})
.def("units", &units::precise_measurement::units)
.def("set_units", [](units::precise_measurement* measurement, const units::precise_unit& unit) {
return units::precise_measurement(measurement->value(), unit);
.def(
"set_units",
[](units::precise_measurement* measurement,
const units::precise_unit& unit) {
return units::precise_measurement(measurement->value(), unit);
})
.def("set_units", [](units::precise_measurement* measurement, const char *unit) {
return units::precise_measurement(measurement->value(),units::unit_from_string(std::string(unit)));
.def(
"set_units",
[](units::precise_measurement* measurement, const char* unit) {
return units::precise_measurement(
measurement->value(),
units::unit_from_string(std::string(unit)));
})
.def(
"value_as",
Expand Down Expand Up @@ -226,7 +236,10 @@ NB_MODULE(units_llnl_ext, mod)
return measurement.convert_to(
units::unit_from_string(std::string(units)));
})
.def("convert_to_base", &units::precise_measurement::convert_to_base,"convert a measurement to a measurement using the base si units")
.def(
"convert_to_base",
&units::precise_measurement::convert_to_base,
"convert a measurement to a measurement using the base si units")
.def("as_unit", &units::precise_measurement::as_unit)
.def(nb::self * nb::self)
.def(nb::self / nb::self)
Expand Down
16 changes: 8 additions & 8 deletions test/python/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ def test_convert_units():
u2=u.unit('cm')
v1=u.convert(10,u1,u2)
assert(v1==10*100)

v2=u.convert(unit_in=u1,unit_out=u2,value=20)
assert(v2==2000)

def test_convert_string():
v1=u.convert(10,'m','mm')
assert(v1==10*1000)

v2=u.convert(unit_out='mm',unit_in='m',value=20)
assert(v2==20000)

def test_convert_units_pu():
u1=u.unit('puMW')
u2=u.unit('kW')
v1=u.convert_pu(0.5,u1,u2,100)
assert(v1==50000)

v2=u.convert_pu(unit_in=u1,unit_out=u2,value=1.2,base=100)
assert(v2==120000)

def test_convert_string_pu():
v1=u.convert_pu(0.5,'puMW','kW',100)
assert(v1==50000)

v2=u.convert_pu(unit_in='puMW',unit_out='kW',value=1.2,base=100)
assert(v2==120000)
assert(v2==120000)
32 changes: 16 additions & 16 deletions test/python/test_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,74 +75,74 @@ def test_set_value():
assert(m1.value()==100)
m2=m1.set_value(14)
assert(m2.value()==14)

def test_set_units():
m1=u.measurement('100 months')
assert(m1.units()==u.unit('month'))
m2=m1.set_units(u.unit('day'))
assert(m2.units()==u.unit('day'))

def test_value_as():
m1=u.measurement('20 weeks')
assert(m1.value_as('day')==20*7)
u1=u.unit('hr')
assert(m1.value_as(u1)==20*7*24)

def test_convert_to():
m1=u.measurement('20 weeks')
m2=m1.convert_to('day')
assert(m2.value()==20*7)
u1=u.unit('hr')
m3=m1.convert_to('hr')
assert(m3.value()==20*7*24)

m4=m1.convert_to_base()
assert(m4.units()==u.unit('s'))
assert(m4.units().multiplier()==1.0)
assert(m4.value()==20*7*24*3600)

def test_as_unit():
m1=u.measurement('15 seconds')
m2=u.measurement(4,m1.as_unit())

assert(m2.value()==4)
assert(m2.value_as('s')==60)

def test_add_sub():
m1=u.measurement('15 seconds')
m2=u.measurement(1,'minute')
m3=m2-m1
assert(m3.value()==0.75)

m4=m3+m2+m1
assert(m4==u.measurement(120,'second'))

def test_mult():
m1=u.measurement('2 meters')
m2=u.measurement(3,'meters')
m3=m2*m1
assert(m3.value()==6)

m4=3*m3
assert(m4==u.measurement(18,'meters squared'))

m5=m3*3
assert(m5==u.measurement(18,'meters squared'))

def test_div():
m1=u.measurement('10 meters')
m2=u.measurement(2,'seconds')
m3=m1/m2
assert(m3.value()==5)

m4=10/m3
assert(m4==u.measurement(2,'s/m'))

m5=m3/2.5
assert(m5==u.measurement(2,'m/s'))

def test_string():
m1=u.measurement('10 lb')
assert(m1.to_string()=='10 lb')
s3=f"the measurement is {m1}"
assert(s3=="the measurement is 10 lb")
assert(s3=="the measurement is 10 lb")
14 changes: 7 additions & 7 deletions test/python/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ def test_conditions():
assert(u3.is_valid())
assert(not u3.is_finite())
assert(u3.isinf())

def test_root():
u1=u.unit('m^6 per second squared')
u2=u1.root(2)
u3=u1.sqrt()
u4=u.unit('m*m*m/s')
assert(u2==u3)
assert(u3==u4)

def test_base():
u1=u.unit('3 ft')
u2 = u.unit('yd')
Expand All @@ -53,7 +53,7 @@ def test_base():
assert(u1==u2)
assert(u1.has_same_base(u2))
assert(u2.has_same_base(u3))

def test_multiplier():
u1=u.unit('nm')
assert(u1.multiplier()==1e-9)
Expand All @@ -68,25 +68,25 @@ def test_commodity():
assert(u1.commodity()=='gold')
u2 = u1.set_commodity('silver')
assert(u2.commodity()=='silver')

def test_string():
u1=u.unit('lb')
assert(u1.to_string()=='lb')
s3=f"the unit is {u1}"
assert(s3=="the unit is lb")

def test_inv():
u1=u.unit('s')
assert(u1.inv()==u.unit('Hz'))
u3=u1.inv().inv()
assert(u3==u1)

def test_float_mult():
u1=u.unit('m')
m3=10*u1
assert(type(m3).__name__=='measurement')
assert(m3.value()==10)

m4=u1*12
assert(type(m4).__name__=='measurement')
assert(m4.value()==12)

0 comments on commit 4a1330d

Please sign in to comment.