Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 1 addition & 33 deletions include/mechanism_configuration/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#pragma once

#include <mechanism_configuration/format_compat.hpp>

#include <ostream>
#include <string>
#include <utility>
Expand Down Expand Up @@ -64,34 +62,4 @@ namespace mechanism_configuration
}

using Errors = std::vector<std::pair<ErrorCode, std::string>>;
} // namespace mechanism_configuration

#ifdef MECH_CONFIG_USE_FMT
template<>
struct fmt::formatter<mechanism_configuration::ErrorLocation>
{
constexpr auto parse(fmt::format_parse_context& ctx) const
{
return ctx.begin();
}
template<class FormatContext>
auto format(const mechanism_configuration::ErrorLocation& loc, FormatContext& ctx) const
{
return fmt::format_to(ctx.out(), "{}:{}", loc.line, loc.column);
}
};
#else
template<>
struct std::formatter<mechanism_configuration::ErrorLocation>
{
constexpr auto parse(std::format_parse_context& ctx) const
{
return ctx.begin();
}
template<class FormatContext>
auto format(const mechanism_configuration::ErrorLocation& loc, FormatContext& ctx) const
{
return std::format_to(ctx.out(), "{}:{}", loc.line, loc.column);
}
};
#endif
} // namespace mechanism_configuration
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#pragma once

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>
#include <mechanism_configuration/mechanism.hpp>
#include <mechanism_configuration/parse.hpp>
#include <mechanism_configuration/types.hpp>
Expand Down
2 changes: 2 additions & 0 deletions include/mechanism_configuration/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <expected>
#include <filesystem>
#include <string>

namespace mechanism_configuration
{
Expand All @@ -17,4 +18,5 @@ namespace mechanism_configuration
/// or a directory of version-0 CAMP files.
/// @return The parsed Mechanism, or all structural and semantic errors encountered.
std::expected<Mechanism, Errors> Parse(const std::filesystem::path& config_path);
std::expected<Mechanism, Errors> ParseFromString(const std::string& config);
} // namespace mechanism_configuration
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ add_library(musica::mechanism_configuration ALIAS mechanism_configuration)
target_compile_features(mechanism_configuration PUBLIC cxx_std_23)

if(MECH_CONFIG_USE_FMT)
target_compile_definitions(mechanism_configuration PUBLIC MECH_CONFIG_USE_FMT)
target_link_libraries(mechanism_configuration PUBLIC fmt::fmt)
target_compile_definitions(mechanism_configuration PRIVATE MECH_CONFIG_USE_FMT)
target_link_libraries(mechanism_configuration PRIVATE fmt::fmt)
endif()

set_target_properties(mechanism_configuration PROPERTIES
Expand Down
3 changes: 1 addition & 2 deletions src/check_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

#include "detail/check_schema.hpp"

#include <mechanism_configuration/format_compat.hpp>
#include "detail/error_format.hpp"

#include <iostream>

Expand Down
41 changes: 41 additions & 0 deletions src/detail/error_format.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2023–2026 University Corporation for Atmospheric Research
// University of Illinois at Urbana-Champaign
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>

// Formatter for ErrorLocation. Kept internal (not in the public errors.hpp) so that
// fmt / <format> is not pulled into the library's public interface. Only the .cpp
// files that build error-message strings need this, so it lives under src/detail.
#ifdef MECH_CONFIG_USE_FMT
template<>
struct fmt::formatter<mechanism_configuration::ErrorLocation>
{
constexpr auto parse(fmt::format_parse_context& ctx) const
{
return ctx.begin();
}
template<class FormatContext>
auto format(const mechanism_configuration::ErrorLocation& loc, FormatContext& ctx) const
{
return fmt::format_to(ctx.out(), "{}:{}", loc.line, loc.column);
}
};
#else
template<>
struct std::formatter<mechanism_configuration::ErrorLocation>
{
constexpr auto parse(std::format_parse_context& ctx) const
{
return ctx.begin();
}
template<class FormatContext>
auto format(const mechanism_configuration::ErrorLocation& loc, FormatContext& ctx) const
{
return std::format_to(ctx.out(), "{}:{}", loc.line, loc.column);
}
};
#endif
26 changes: 25 additions & 1 deletion src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// University of Illinois at Urbana-Champaign
// SPDX-License-Identifier: Apache-2.0

#include "detail/error_format.hpp"
#include "detail/v0/parser.hpp"
#include "detail/v1/parser.hpp"

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>
#include <mechanism_configuration/mechanism.hpp>
#include <mechanism_configuration/parse.hpp>

Expand Down Expand Up @@ -90,4 +90,28 @@ namespace mechanism_configuration
}
}

std::expected<Mechanism, Errors> ParseFromString(const std::string& config)
{
// only v1 supports parsing from a string, so we must first detect the version from the string
YAML::Node object;
try
{
object = YAML::Load(config);
if (!object["version"])
{
return std::unexpected(Errors{ { ErrorCode::InvalidVersion, "error: Unsupported version number '0.0.0'." } });
}
const Version version(object["version"].as<std::string>());
if (version.major != 1)
{
return std::unexpected(Errors{ { ErrorCode::InvalidVersion, mc_fmt::format("error: Unsupported version number '{}'.", version.to_string()) } });
}
return v1::Parser{}.Parse(config);
}
catch (const YAML::Exception& e)
{
return std::unexpected(Errors{ { ErrorCode::UnexpectedError, mc_fmt::format("Failed to parse configuration string: {}", e.what()) } });
}
}

} // namespace mechanism_configuration
9 changes: 4 additions & 5 deletions src/v0/emission_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ namespace mechanism_configuration::v0
{
std::string species = object[keys::SPECIES].as<std::string>();
YAML::Node products_object{};
std::vector<types::ReactionComponent> reactants;
std::vector<types::ReactionComponent> products;
products.push_back({ .name = species, .coefficient = 1.0 });
double scaling_factor = object[keys::SCALING_FACTOR] ? object[keys::SCALING_FACTOR].as<double>() : 1.0;

std::string name = "EMIS." + object[keys::MUSICA_NAME].as<std::string>();
types::UserDefined user_defined = {
.scaling_factor = scaling_factor, .reactants = reactants, .products = products, .name = name
std::string name = object[keys::MUSICA_NAME].as<std::string>();
types::Emission user_defined = {
.scaling_factor = scaling_factor, .products = products, .name = name
};
mechanism.reactions.user_defined.push_back(user_defined);
mechanism.reactions.emission.push_back(user_defined);
}

return errors;
Expand Down
8 changes: 4 additions & 4 deletions src/v0/first_order_loss_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ namespace mechanism_configuration::v0
reactants.push_back({ .name = species, .coefficient = 1.0 });
double scaling_factor = object[keys::SCALING_FACTOR] ? object[keys::SCALING_FACTOR].as<double>() : 1.0;

std::string name = "LOSS." + object[keys::MUSICA_NAME].as<std::string>();
types::UserDefined user_defined = {
.scaling_factor = scaling_factor, .reactants = reactants, .products = products, .name = name
std::string name = object[keys::MUSICA_NAME].as<std::string>();
types::FirstOrderLoss user_defined = {
.scaling_factor = scaling_factor, .reactants = reactants[0], .products = products, .name = name
};
mechanism.reactions.user_defined.push_back(user_defined);
mechanism.reactions.first_order_loss.push_back(user_defined);
}

return errors;
Expand Down
2 changes: 1 addition & 1 deletion src/v0/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ namespace mechanism_configuration::v0

// all species in version 0 are in the gas phase
types::Phase gas_phase;
gas_phase.name = "GAS";
gas_phase.name = "gas";
for (auto& species : mechanism.species)
{
types::PhaseSpecies phase_species;
Expand Down
13 changes: 8 additions & 5 deletions src/v0/photolysis_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ namespace mechanism_configuration::v0

double scaling_factor = object[keys::SCALING_FACTOR] ? object[keys::SCALING_FACTOR].as<double>() : 1.0;

std::string name = "PHOTO." + object[keys::MUSICA_NAME].as<std::string>();
types::UserDefined user_defined = {
.scaling_factor = scaling_factor, .reactants = reactants, .products = products, .name = name
};
mechanism.reactions.user_defined.push_back(user_defined);
if (!reactants.empty())
{
std::string name = object[keys::MUSICA_NAME].as<std::string>();
types::Photolysis user_defined = {
.scaling_factor = scaling_factor, .reactants = reactants[0], .products = products, .name = name
};
mechanism.reactions.photolysis.push_back(user_defined);
}
}

return errors;
Expand Down
2 changes: 1 addition & 1 deletion src/v0/user_defined_reaction_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace mechanism_configuration::v0

double scaling_factor = object[keys::SCALING_FACTOR] ? object[keys::SCALING_FACTOR].as<double>() : 1.0;

std::string name = "USER." + object[keys::MUSICA_NAME].as<std::string>();
std::string name = object[keys::MUSICA_NAME].as<std::string>();

types::UserDefined user_defined = {
.scaling_factor = scaling_factor, .reactants = reactants, .products = products, .name = name
Expand Down
2 changes: 1 addition & 1 deletion src/v1/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include "detail/v1/parser.hpp"

#include "detail/check_schema.hpp"
#include "detail/error_format.hpp"
#include "detail/v1/keys.hpp"
#include "detail/v1/type_parsers.hpp"
#include "detail/v1/type_schema.hpp"
#include "detail/v1/utils.hpp"

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>
#include <mechanism_configuration/mechanism.hpp>

#include <yaml-cpp/yaml.h>
Expand Down
2 changes: 1 addition & 1 deletion src/v1/reactions/arrhenius.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// SPDX-License-Identifier: Apache-2.0

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>
#include <mechanism_configuration/types.hpp>

#include <detail/check_schema.hpp>
#include <detail/constants.hpp>
#include <detail/error_format.hpp>
#include <detail/v1/reaction_parsers.hpp>
#include <detail/v1/type_parsers.hpp>
#include <detail/v1/type_schema.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/v1/reactions/first_order_loss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// SPDX-License-Identifier: Apache-2.0

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>
#include <mechanism_configuration/types.hpp>

#include <detail/check_schema.hpp>
#include <detail/error_format.hpp>
#include <detail/v1/reaction_parsers.hpp>
#include <detail/v1/type_parsers.hpp>
#include <detail/v1/type_schema.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/v1/reactions/photolysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// SPDX-License-Identifier: Apache-2.0

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>
#include <mechanism_configuration/types.hpp>

#include <detail/check_schema.hpp>
#include <detail/error_format.hpp>
#include <detail/v1/reaction_parsers.hpp>
#include <detail/v1/type_parsers.hpp>
#include <detail/v1/type_schema.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/v1/reactions/surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// SPDX-License-Identifier: Apache-2.0

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>
#include <mechanism_configuration/types.hpp>

#include <detail/check_schema.hpp>
#include <detail/error_format.hpp>
#include <detail/v1/reaction_parsers.hpp>
#include <detail/v1/type_parsers.hpp>
#include <detail/v1/type_schema.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/v1/reactions/taylor_series.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// SPDX-License-Identifier: Apache-2.0

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>
#include <mechanism_configuration/types.hpp>

#include <detail/check_schema.hpp>
#include <detail/constants.hpp>
#include <detail/error_format.hpp>
#include <detail/v1/reaction_parsers.hpp>
#include <detail/v1/type_parsers.hpp>
#include <detail/v1/type_schema.hpp>
Expand Down
2 changes: 1 addition & 1 deletion src/v1/type_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include "detail/v1/type_schema.hpp"

#include "detail/check_schema.hpp"
#include "detail/error_format.hpp"
#include "detail/v1/keys.hpp"
#include "detail/v1/reaction_parsers.hpp"
#include "detail/v1/utils.hpp"

#include <mechanism_configuration/errors.hpp>
#include <mechanism_configuration/format_compat.hpp>

#include <string>
#include <vector>
Expand Down
3 changes: 2 additions & 1 deletion src/validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// University of Illinois at Urbana-Champaign
// SPDX-License-Identifier: Apache-2.0

#include <mechanism_configuration/format_compat.hpp>
#include <mechanism_configuration/validate.hpp>

#include "detail/error_format.hpp"

#include <optional>
#include <string>
#include <string_view>
Expand Down
Loading
Loading