From 1b72b878d0868de4c3438a657861fd3fa0bf5a33 Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Tue, 10 Dec 2024 11:45:19 -0500 Subject: [PATCH] Add `STANDARD_GRAVITY` Constant (#348) It took me a little while to figure out exactly what I wanted to do here. Initially, I was going to _deprecate_ the standard gravity unit. Then I realized we need it to define certain other units, such as the correspondence between "pounds" as a force and a mass --- and I didn't want to introduce a circular dependency between `:units` and `:constants`. Then I was going to deprecate only the _quantity maker_, `standard_gravity`. But then I realized it would be weird to create a situation where certain units have quantity makers, and certain other ones don't. In the end, I think the right move is to just provide standard gravity as a new constant. This is the preferred way to interact with it, but the old ways will still be there for the foreseeable future. Follow-on to #90. --- au/code/au/CMakeLists.txt | 2 + au/code/au/constants/standard_gravity.hh | 22 ++++++++++ .../constants/test/standard_gravity_test.cc | 44 +++++++++++++++++++ docs/reference/constant.md | 1 + 4 files changed, 69 insertions(+) create mode 100644 au/code/au/constants/standard_gravity.hh create mode 100644 au/code/au/constants/test/standard_gravity_test.cc diff --git a/au/code/au/CMakeLists.txt b/au/code/au/CMakeLists.txt index ca41ca31..edb965c9 100644 --- a/au/code/au/CMakeLists.txt +++ b/au/code/au/CMakeLists.txt @@ -51,6 +51,7 @@ header_only_library( constants/planck_constant.hh constants/reduced_planck_constant.hh constants/speed_of_light.hh + constants/standard_gravity.hh stdx/experimental/is_detected.hh stdx/functional.hh stdx/type_traits.hh @@ -239,6 +240,7 @@ gtest_based_test( constants/test/planck_constant_test.cc constants/test/reduced_planck_constant_test.cc constants/test/speed_of_light_test.cc + constants/test/standard_gravity_test.cc DEPS au testing diff --git a/au/code/au/constants/standard_gravity.hh b/au/code/au/constants/standard_gravity.hh new file mode 100644 index 00000000..3936ee20 --- /dev/null +++ b/au/code/au/constants/standard_gravity.hh @@ -0,0 +1,22 @@ +// Copyright 2024 Aurora Operations, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "au/constant.hh" +#include "au/units/standard_gravity.hh" + +namespace au { + +constexpr auto STANDARD_GRAVITY = make_constant(StandardGravity{}); + +} // namespace au diff --git a/au/code/au/constants/test/standard_gravity_test.cc b/au/code/au/constants/test/standard_gravity_test.cc new file mode 100644 index 00000000..da4cc8f1 --- /dev/null +++ b/au/code/au/constants/test/standard_gravity_test.cc @@ -0,0 +1,44 @@ +// Copyright 2024 Aurora Operations, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "au/constants/standard_gravity.hh" + +#include "au/testing.hh" +#include "au/units/meters.hh" +#include "au/units/seconds.hh" +#include "gtest/gtest.h" + +namespace au { +namespace { + +using ::testing::StrEq; + +TEST(StandardGravity, HasExpectedValue) { + // g_0 = 9.80665 m/s^2 + + // Test approximate value (guard against powers-of-10 type errors). + constexpr auto defining_units = meters / pow<2>(seconds); + constexpr auto val = defining_units(9.80665); + constexpr auto err = defining_units(0.00001); + EXPECT_THAT(STANDARD_GRAVITY.as(defining_units), IsNear(val, err)); + + // Test exact value. + EXPECT_THAT(STANDARD_GRAVITY.in(defining_units / pow<5>(mag<10>())), + SameTypeAndValue(980'665)); +} + +TEST(StandardGravity, HasExpectedLabel) { EXPECT_THAT(unit_label(STANDARD_GRAVITY), StrEq("g_0")); } + +} // namespace +} // namespace au diff --git a/docs/reference/constant.md b/docs/reference/constant.md index 61b19b33..3b66ff40 100644 --- a/docs/reference/constant.md +++ b/docs/reference/constant.md @@ -53,6 +53,7 @@ Here are the constants that we include with Au: | Planck constant | $h$ | $6.626\,070\,15 \times 10^{-34}\,\, \text{J} \cdot \text{s}$ | `planck_constant.hh` | `PLANCK_CONSTANT` | | Reduced Planck constant | $\hbar$ | $1.054\,571\,817 \times 10^{-34}\,\, \text{J} \cdot \text{s}$ | `reduced_planck_constant.hh` | `REDUCED_PLANCK_CONSTANT` | | Speed of light | $c$ | $299\,792\,458\,\, \text{m} / \text{s}$ | `speed_of_light.hh` | `SPEED_OF_LIGHT` | +| Standard Gravity | $g_0$ | $9.806\,65\,\, \text{m} / \text{s}^2$ | `standard_gravity.hh` | `STANDARD_GRAVITY` | Our policy is to include only exactly defined constants with the library. This rules out many useful constants, such as the universal gravitational constant $G$, the _new_ (post-2019) permeability