From 7fe4979c4663cc5eece3b71428c21968289c958c Mon Sep 17 00:00:00 2001 From: David Andrs Date: Mon, 22 Apr 2024 13:08:27 -0600 Subject: [PATCH] Adding fprops::from_name --- include/fprops/fprops.h | 17 +++++++++++++++++ src/fprops.cpp | 37 +++++++++++++++++++++++++++++++++++++ test/src/fprops_test.cpp | 24 ++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 include/fprops/fprops.h create mode 100644 src/fprops.cpp create mode 100644 test/src/fprops_test.cpp diff --git a/include/fprops/fprops.h b/include/fprops/fprops.h new file mode 100644 index 0000000..3b77ab5 --- /dev/null +++ b/include/fprops/fprops.h @@ -0,0 +1,17 @@ +// SPDX-FileCopyrightText: 2024 David Andrs +// SPDX-License-Identifier: MIT + +#pragma once + +#include "SinglePhaseFluidProperties.h" +#include + +namespace fprops { + +/// Construct fluid properties from a fluid name +/// +/// @param name Fluid name +/// @return Fluid properties +SinglePhaseFluidProperties * from_name(const std::string & name); + +} // namespace fprops diff --git a/src/fprops.cpp b/src/fprops.cpp new file mode 100644 index 0000000..5c57188 --- /dev/null +++ b/src/fprops.cpp @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2024 David Andrs +// SPDX-License-Identifier: MIT + +#include "fprops/fprops.h" +#include "fprops/Air.h" +#include "fprops/CarbonDioxide.h" +#include "fprops/Helium.h" +#include "fprops/Nitrogen.h" +#include "fprops/Exception.h" + +namespace fprops { + +std::string +to_lower(const std::string & name) +{ + std::string lower(name); + std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower); + return lower; +} + +SinglePhaseFluidProperties * +from_name(const std::string & name) +{ + auto n = to_lower(name); + if (n == "air") + return new Air(); + else if (n == "carbon_dioxide" || n == "co2") + return new CarbonDioxide(); + else if (n == "helium" || n == "he") + return new Helium(); + else if (n == "nitrogen" || n == "n2") + return new Nitrogen(); + else + throw Exception("Unknown fluid name '{}'", name); +} + +} // namespace fprops diff --git a/test/src/fprops_test.cpp b/test/src/fprops_test.cpp new file mode 100644 index 0000000..4f24627 --- /dev/null +++ b/test/src/fprops_test.cpp @@ -0,0 +1,24 @@ +#include "gmock/gmock.h" +#include "fprops/fprops.h" +#include "fprops/Air.h" +#include "fprops/CarbonDioxide.h" +#include "fprops/Helium.h" +#include "fprops/Nitrogen.h" + +using namespace fprops; +using namespace testing; + +TEST(FpropsTest, from_name) +{ + auto air = fprops::from_name("air"); + EXPECT_THAT(dynamic_cast(air), NotNull()); + + auto co2 = fprops::from_name("co2"); + EXPECT_THAT(dynamic_cast(co2), NotNull()); + + auto he = fprops::from_name("helium"); + EXPECT_THAT(dynamic_cast(he), NotNull()); + + auto n2 = fprops::from_name("nitrogen"); + EXPECT_THAT(dynamic_cast(n2), NotNull()); +}