From 7fe80e1490109fa68ac4521ebf2a08aef94683e0 Mon Sep 17 00:00:00 2001 From: ecasglez <29178639+ecasglez@users.noreply.github.com> Date: Wed, 18 Oct 2023 13:43:18 +0200 Subject: [PATCH] Fix parsing xml files when running openmc-plotter in regions with comma decimal separator (#2723) Co-authored-by: ecasglez Co-authored-by: Paul Romano --- src/initialize.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/initialize.cpp b/src/initialize.cpp index 2f448e804a..cafcd58284 100644 --- a/src/initialize.cpp +++ b/src/initialize.cpp @@ -1,5 +1,6 @@ #include "openmc/initialize.h" +#include #include #include // for getenv #include @@ -106,10 +107,25 @@ int openmc_init(int argc, char* argv[], const void* intracomm) // will be re-initialized later openmc::openmc_set_seed(DEFAULT_SEED); + // Copy previous locale and set locale to C. This is a workaround for an issue + // whereby when openmc_init is called from the plotter, the Qt application + // framework first calls std::setlocale, which affects how pugixml reads + // floating point numbers due to a bug: + // https://github.com/zeux/pugixml/issues/469 + std::string prev_locale = std::setlocale(LC_ALL, nullptr); + if (std::setlocale(LC_ALL, "C") == NULL) { + fatal_error("Cannot set locale to C."); + } + // Read XML input files if (!read_model_xml()) read_separate_xml_files(); + // Reset locale to previous state + if (std::setlocale(LC_ALL, prev_locale.c_str()) == NULL) { + fatal_error("Cannot reset locale."); + } + // Write some initial output under the header if needed initial_output();