Skip to content

Commit

Permalink
combine_identical_point_sources
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkvdb committed Aug 28, 2024
1 parent e8376e0 commit c0f6330
Show file tree
Hide file tree
Showing 20 changed files with 273 additions and 103 deletions.
20 changes: 20 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "cmake",
"label": "CMake: build",
"command": "build",
"targets": [
"all"
],
"preset": "${command:cmake.activeBuildPresetName}",
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [],
"detail": "CMake template build task"
}
]
}
51 changes: 28 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ include(GNUInstallDirs)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_OSX_DEPLOYMENT_TARGET 11.3)
set(CMAKE_OSX_DEPLOYMENT_TARGET 14.0)

option(INFRA_INSOURCE "Use submodule version of infra" ON)
option(GDX_INSOURCE "Use submodule version of geodynamix" ON)
set(PACKAGE_VERSION_COMMITHASH "dev" CACHE STRING "git commit hash")
if (PACKAGE_VERSION_COMMITHASH STREQUAL "dev")

if(PACKAGE_VERSION_COMMITHASH STREQUAL "dev")
set(EMAP_DEV_BUILD 1)
else ()
else()
set(EMAP_DEV_BUILD 0)
if (CMAKE_COMPILER_IS_GNUCXX)

if(CMAKE_COMPILER_IS_GNUCXX)
add_link_options($<$<CONFIG:RELEASE>:-s>) # strip dist binaries
endif ()
endif ()
endif()
endif()

if (WIN32)
if(WIN32)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
set(CMAKE_VS_JUST_MY_CODE_DEBUGGING ON)

Expand All @@ -54,24 +56,24 @@ if (WIN32)
$<$<CXX_COMPILER_ID:MSVC>:/await>
)

if (MSVC)
if(MSVC)
# Fixes warning when linking against release c library in debug mode
# Fixes warning when linking against release c library in debug mode
add_link_options("$<$<CONFIG:Debug>:/NODEFAULTLIB:MSVCRT;/NODEFAULTLIB:LIBCMT>")
endif ()
else ()
endif()
else()
add_compile_options(
-Wall -Wextra -Wpedantic -Wfatal-errors -Wno-unknown-pragmas
$<$<CXX_COMPILER_ID:GNU>:-Wno-maybe-uninitialized>
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
$<$<CXX_COMPILER_ID:Clang>:-fcolor-diagnostics>
)
endif ()
endif()

set (LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set (EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})

if (INFRA_INSOURCE)
if(INFRA_INSOURCE)
set(INFRA_LOGGING ON)
set(INFRA_GDAL ON)
set(INFRA_PROJDB_DIR "data")
Expand All @@ -84,34 +86,37 @@ if (INFRA_INSOURCE)
set(INFRA_TESTUTIL_MAIN_PROJDB_PATH ${INFRA_PROJDB_DIR})

add_subdirectory(deps/infra)
else ()
else()
find_package(Infra CONFIG COMPONENTS testutil REQUIRED)
set_target_properties(fmt::fmt PROPERTIES MAP_IMPORTED_CONFIG_RELWITHDEBINFO Release)
set_target_properties(spdlog::spdlog PROPERTIES MAP_IMPORTED_CONFIG_RELWITHDEBINFO Release)
set_target_properties(Infra::infra PROPERTIES MAP_IMPORTED_CONFIG_RELWITHDEBINFO Release)
endif ()
endif()

if (GDX_INSOURCE)
if(GDX_INSOURCE)
set(GDX_PYTHON_BINDINGS OFF)
set(GDX_ENABLE_TESTS OFF)
set(GDX_ENABLE_TEST_UTILS ${BUILD_TESTING})
set(GDX_ENABLE_TOOLS OFF)
set(GDX_ENABLE_SIMD ON)
IF(NOT "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "arm" AND NOT "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "arm64")

IF(NOT "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "arm" AND NOT "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "arm64")
set(GDX_AVX2 ON)
endif ()
endif()

add_subdirectory(deps/geodynamix)
else ()
else()
find_package(Geodynamix CONFIG REQUIRED)
endif ()
endif()

add_subdirectory(logic)
add_subdirectory(cli)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_BINARY_DIR}/emapconfig.h)
if (WIN32)

if(WIN32)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/version.rc.in ${CMAKE_CURRENT_BINARY_DIR}/version.rc @ONLY)
endif ()
endif()

install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/README.md
Expand Down
18 changes: 14 additions & 4 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@
"inherits": "vcpkg-base",
"hidden": true,
"cacheVariables": {
"VCPKG_OSX_DEPLOYMENT_TARGET": "11.3",
"VCPKG_OSX_DEPLOYMENT_TARGET": "14.0",
"VCPKG_CHAINLOAD_TOOLCHAIN_FILE": "${sourceDir}/deps/vcpkg/triplets/toolchain-osx-appleclang.cmake",
"VCPKG_CXX_FLAGS": "-fvisibility=hidden -fvisibility-inlines-hidden"
}
},
{
"name": "osx-homebrew-base",
"inherits": "vcpkg-base",
"hidden": true,
"cacheVariables": {
"VCPKG_OSX_DEPLOYMENT_TARGET": "14.0",
"VCPKG_CHAINLOAD_TOOLCHAIN_FILE": "${sourceDir}/deps/vcpkg/triplets/toolchain-osx-homebrew.cmake",
"VCPKG_CXX_FLAGS": "-fvisibility=hidden -fvisibility-inlines-hidden"
}
},
{
"name": "windows-base",
"inherits": "vcpkg-base",
Expand Down Expand Up @@ -69,14 +79,14 @@
},
{
"name": "arm64-osx",
"inherits": "osx-base",
"inherits": "osx-homebrew-base",
"displayName": "OSX arm64",
"description": "Vcpkg build for arm64 on mac",
"cacheVariables": {
"VCPKG_TARGET_TRIPLET": "arm64-osx",
"VCPKG_TARGET_TRIPLET": "arm64-osx-homebrew",
"CMAKE_OSX_ARCHITECTURES": "arm64",
"CMAKE_BUILD_TYPE": "Release",
"VCPKG_INSTALLED_DIR": "${sourceDir}/vcpkgs-arm64-osx",
"VCPKG_INSTALLED_DIR": "${sourceDir}/vcpkgs-arm64-osx-homebrew",
"VCPKG_ALLOW_SYSTEM_LIBS": "ON"
}
},
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ This section configures the model run
- `included_pollutants` List of pollutants to include in the model run, this setting is optional, if it is not present all the configured pollutants will be included in the run.
- `scenario` First search for emission input files with the `_scenario` suffix before using the default emission input files, allows easy creation of scenarios with modified input files. For point sources `emap_{scenario}_{pollutant}_{year}_*.csv` is checked before `emap_{pollutant}_{year}_*.csv`
- `point_source_rescale_threshold` The threshold for allowing automatic rescaling of point sources when they exceed the reported total emissions [0 - 100]
- `combine_identical_point_sources` combine the emission of the point sources with the same location and properties. The emissions will be summed and only one entry will be present in the output file. (default=true)

### Output section
This section configures the output of the model
Expand Down
10 changes: 5 additions & 5 deletions cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <windows.h>
#endif

using inf::Log;
using namespace inf;

static inf::Log::Level log_level_from_value(int32_t value)
{
Expand Down Expand Up @@ -75,7 +75,7 @@ int main(int argc, char** argv)
lyra::opt(options.debugGrids)["-d"]["--debug"]("Dumps internal grid usages") |
lyra::opt(options.config, "path")["-c"]["--config"]("The e-map run configuration").required();

if (argc == 2 && fs::is_regular_file(fs::u8path(argv[1]))) {
if (argc == 2 && fs::is_regular_file(file::u8path(argv[1]))) {
// simplified cli invocation, assume argument is config file
options.config = argv[1];
} else {
Expand All @@ -91,7 +91,7 @@ int main(int argc, char** argv)

try {
inf::gdal::RegistrationConfig gdalCfg;
gdalCfg.projdbPath = fs::u8path(argv[0]).parent_path() / "data";
gdalCfg.projdbPath = file::u8path(argv[0]).parent_path() / "data";
inf::gdal::Registration reg(gdalCfg);
inf::gdal::set_log_handler(inf::Log::Level::Debug);

Expand All @@ -105,10 +105,10 @@ int main(int argc, char** argv)
}

if (options.debugGrids) {
return emap::debug_grids(fs::u8path(options.config), log_level_from_value(options.logLevel));
return emap::debug_grids(file::u8path(options.config), log_level_from_value(options.logLevel));
} else {
return emap::run_model(
fs::u8path(options.config), log_level_from_value(options.logLevel), options.concurrency, [&](const emap::ModelProgress::Status& info) {
file::u8path(options.config), log_level_from_value(options.logLevel), options.concurrency, [&](const emap::ModelProgress::Status& info) {
if (progressBar) {
progressBar->set_progress(info.progress());
progressBar->set_postfix_text(info.payload().to_string());
Expand Down
2 changes: 1 addition & 1 deletion deps/geodynamix
2 changes: 1 addition & 1 deletion deps/infra
Submodule infra updated 1 files
+6 −3 include/infra/chrono.h
2 changes: 2 additions & 0 deletions logic/configurationparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ static RunConfiguration parse_run_configuration_impl(std::string_view configCont

const auto grid = read_grid(model.section["grid"].value<std::string_view>());
const auto scenario = read_string(model, "scenario", "");
const auto combinePointSources = model.section["combine_identical_point_sources"].value<bool>().value_or(true);
const double rescaleThreshold = model.section["point_source_rescale_threshold"].value<double>().value_or(100.0);
const auto year = read_year(model.section["year"]);
const auto reportYear = read_year(model.section["report_year"]);
Expand Down Expand Up @@ -644,6 +645,7 @@ static RunConfiguration parse_run_configuration_impl(std::string_view configCont
year,
reportYear,
scenario,
combinePointSources,
rescaleThreshold,
std::move(includedPollutants),
std::move(sectorInventory),
Expand Down
4 changes: 3 additions & 1 deletion logic/emissionscollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ void EmissionsCollector::add_emissions(const CountryCellCoverage& countryInfo, c
}

for (auto& entry : pointEmissions) {
_outputBuilder->add_point_output_entry(entry);
if (entry.value().amount() > 0.0) {
_outputBuilder->add_point_output_entry(entry);
}
}

if (diffuseEmissions.empty() && !pointEmissions.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion logic/include/emap/emissioninventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class EmissionInventoryEntry
{
std::vector<EmissionEntry> result;
result.reserve(_pointEmissionEntries.size());
std::transform(_pointEmissionEntries.begin(), _pointEmissionEntries.end(), std::back_inserter(result), [=](const EmissionEntry& entry) {
std::transform(_pointEmissionEntries.begin(), _pointEmissionEntries.end(), std::back_inserter(result), [this](const EmissionEntry& entry) {
auto scaledEntry = entry;
scaledEntry.set_value(entry.value() * _pointAutoScaling * _pointUserScaling);
return scaledEntry;
Expand Down
5 changes: 5 additions & 0 deletions logic/include/emap/runconfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class RunConfiguration
date::year year,
date::year reportYear,
std::string_view scenario,
bool combineIdenticalPointSources,
double rescaleThreshold,
std::vector<Pollutant> includedPollutants,
SectorInventory sectors,
Expand Down Expand Up @@ -95,6 +96,9 @@ class RunConfiguration

std::string_view scenario() const noexcept;

bool combine_identical_point_sources() const noexcept;
void set_combine_identical_point_sources(bool enabled) noexcept;

double point_source_rescale_threshold() const noexcept;

void set_max_concurrency(std::optional<int32_t> concurrency) noexcept;
Expand Down Expand Up @@ -128,6 +132,7 @@ class RunConfiguration
date::year _year;
date::year _reportYear;
std::string _scenario;
bool _combineIdenticalPointSources;
double _pointRescaleThreshold;
std::vector<Pollutant> _includedPollutants;
SectorInventory _sectorInventory;
Expand Down
15 changes: 15 additions & 0 deletions logic/include/emap/sector.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,19 @@ struct hash<emap::NfrId> : type_safe::hashable<emap::NfrId>
{
};

template <>
struct hash<emap::EmissionSector>
{
std::size_t operator()(const emap::EmissionSector& sector) const
{
std::size_t seed = 0;
if (sector.type() == emap::EmissionSector::Type::Nfr) {
seed = std::hash<emap::NfrId>{}(sector.nfr_sector().id());
} else {
seed = std::hash<emap::GnfrId>{}(sector.gnfr_sector().id());
}

return seed;
}
};
}
Loading

0 comments on commit c0f6330

Please sign in to comment.