From 9ba9bdcc94a0e97846ed3a217add04f1cf6af806 Mon Sep 17 00:00:00 2001 From: Adam Washington Date: Thu, 30 Nov 2023 15:16:22 +0000 Subject: [PATCH 1/5] Enable TOML export --- src/main.cpp | 10 ++++++++++ src/main/cli.cpp | 4 ++++ src/main/cli.h | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index a6a19a7648..58484e2445 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,6 +54,16 @@ int main(int args, char **argv) return 1; } + if (options.toTomlFile()) { + Messenger::print("Saving input file to '{}'...\n", options.toTomlFile().value()); + auto toml = dissolve.serialise(); + std::ofstream outfile(options.toTomlFile().value()); + outfile << toml; + outfile.close(); + + return 0; + } + // Save input file to new output filename and quit? if (options.writeInputFilename()) { diff --git a/src/main/cli.cpp b/src/main/cli.cpp index 613fc657b9..a5c3db7609 100644 --- a/src/main/cli.cpp +++ b/src/main/cli.cpp @@ -50,6 +50,7 @@ int CLIOptions::parse(const int args, char **argv, bool isGUI, bool isParallel) app.add_option("-f,--frequency", restartFileFrequency_, "Frequency at which to write restart file (default = 10)") ->group("Output Files"); app.add_flag("-x,--no-restart-file", noRestartFile_, "Don't write restart file at all")->group("Output Files"); + app.add_option("--to-toml", toTomlFile_, "Convert input file into TOML format")->group("Output Files"); // Add GUI-specific options - if this is not the GUI, make the input file a required parameter if (!isGUI) @@ -108,3 +109,6 @@ bool CLIOptions::ignoreRestartFile() const { return ignoreRestartFile_; } // Return whether to prevent writing of the restart file bool CLIOptions::noRestartFile() const { return noRestartFile_; }; + +// Return output destination for TOML conversion +std::optional CLIOptions::toTomlFile() const {return toTomlFile_;} diff --git a/src/main/cli.h b/src/main/cli.h index ee558c5ef2..90525ea7c4 100644 --- a/src/main/cli.h +++ b/src/main/cli.h @@ -37,6 +37,8 @@ class CLIOptions bool ignoreRestartFile_{false}; // Whether to prevent writing of the restart file bool noRestartFile_{false}; + // File for TOML conversion + std::optional toTomlFile_; public: // Parse Result enum @@ -66,4 +68,6 @@ class CLIOptions bool ignoreRestartFile() const; // Return whether to prevent writing of the restart file bool noRestartFile() const; + // Return output destination for TOML conversion + std::optional toTomlFile() const; }; From 661a81a073a97bce3c6f5905cb3487ca580f0186 Mon Sep 17 00:00:00 2001 From: Adam Washington Date: Thu, 30 Nov 2023 15:32:03 +0000 Subject: [PATCH 2/5] Merge logic in with old rewrite code --- src/main.cpp | 36 +++++++++++++++++++----------------- src/main/cli.cpp | 2 +- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 58484e2445..c15ce72082 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,33 +54,35 @@ int main(int args, char **argv) return 1; } - if (options.toTomlFile()) { - Messenger::print("Saving input file to '{}'...\n", options.toTomlFile().value()); - auto toml = dissolve.serialise(); - std::ofstream outfile(options.toTomlFile().value()); - outfile << toml; - outfile.close(); - - return 0; - } - // Save input file to new output filename and quit? - if (options.writeInputFilename()) + if (options.writeInputFilename() || options.toTomlFile()) { - Messenger::print("Saving input file to '{}'...\n", options.writeInputFilename().value()); + std::string filename = options.writeInputFilename().value_or(options.toTomlFile().value()); + Messenger::print("Saving input file to '{}'...\n", filename); bool result; if (dissolve.worldPool().isMaster()) { - result = dissolve.saveInput(options.writeInputFilename().value()); - if (result) - dissolve.worldPool().decideTrue(); + if (options.writeInputFilename()) + { + result = dissolve.saveInput(options.writeInputFilename().value()); + if (result) + dissolve.worldPool().decideTrue(); + else + dissolve.worldPool().decideFalse(); + } else - dissolve.worldPool().decideFalse(); + { + auto toml = dissolve.serialise(); + std::ofstream outfile(options.toTomlFile().value()); + outfile << toml; + outfile.close(); + result = 1; + } } else result = dissolve.worldPool().decision(); if (!result) - Messenger::error("Failed to save input file to '{}'.\n", options.writeInputFilename().value()); + Messenger::error("Failed to save input file to '{}'.\n", filename); // Reload the written file and continue? if (options.writeInputAndReload()) diff --git a/src/main/cli.cpp b/src/main/cli.cpp index a5c3db7609..3002ab38da 100644 --- a/src/main/cli.cpp +++ b/src/main/cli.cpp @@ -41,6 +41,7 @@ int CLIOptions::parse(const int args, char **argv, bool isGUI, bool isParallel) app.add_option("-w,--write-input", writeInputFilename_, "Write out the current simulation input to the file specified and then quit") ->group("Input Files"); + app.add_option("--to-toml", toTomlFile_, "Convert input file into TOML format")->group("Output Files"); app.add_flag("--reload-after-write", writeInputAndReload_, "Reload the input generated by --write-input and continue instead of quitting") ->group("Input Files"); @@ -50,7 +51,6 @@ int CLIOptions::parse(const int args, char **argv, bool isGUI, bool isParallel) app.add_option("-f,--frequency", restartFileFrequency_, "Frequency at which to write restart file (default = 10)") ->group("Output Files"); app.add_flag("-x,--no-restart-file", noRestartFile_, "Don't write restart file at all")->group("Output Files"); - app.add_option("--to-toml", toTomlFile_, "Convert input file into TOML format")->group("Output Files"); // Add GUI-specific options - if this is not the GUI, make the input file a required parameter if (!isGUI) From b75bb5065efe74529fdc70f88a063a46ca303ab4 Mon Sep 17 00:00:00 2001 From: Adam Washington Date: Thu, 30 Nov 2023 15:41:16 +0000 Subject: [PATCH 3/5] Fix formatting --- src/main/cli.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/cli.cpp b/src/main/cli.cpp index 3002ab38da..c7350ebb86 100644 --- a/src/main/cli.cpp +++ b/src/main/cli.cpp @@ -41,7 +41,7 @@ int CLIOptions::parse(const int args, char **argv, bool isGUI, bool isParallel) app.add_option("-w,--write-input", writeInputFilename_, "Write out the current simulation input to the file specified and then quit") ->group("Input Files"); - app.add_option("--to-toml", toTomlFile_, "Convert input file into TOML format")->group("Output Files"); + app.add_option("--to-toml", toTomlFile_, "Convert input file into TOML format")->group("Output Files"); app.add_flag("--reload-after-write", writeInputAndReload_, "Reload the input generated by --write-input and continue instead of quitting") ->group("Input Files"); @@ -111,4 +111,4 @@ bool CLIOptions::ignoreRestartFile() const { return ignoreRestartFile_; } bool CLIOptions::noRestartFile() const { return noRestartFile_; }; // Return output destination for TOML conversion -std::optional CLIOptions::toTomlFile() const {return toTomlFile_;} +std::optional CLIOptions::toTomlFile() const { return toTomlFile_; } From 610b40aa4bb5eb3b853f3685e1278034ec2ff939 Mon Sep 17 00:00:00 2001 From: Adam Washington Date: Fri, 1 Dec 2023 12:58:24 +0000 Subject: [PATCH 4/5] Update src/main.cpp Co-authored-by: Tristan Youngs --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index c15ce72082..cbff71e645 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -76,7 +76,7 @@ int main(int args, char **argv) std::ofstream outfile(options.toTomlFile().value()); outfile << toml; outfile.close(); - result = 1; + result = true; } } else From 7d625943940faf78b867400e2f4c7e0d735bffed Mon Sep 17 00:00:00 2001 From: Adam Washington Date: Fri, 1 Dec 2023 15:13:02 +0000 Subject: [PATCH 5/5] Fix filename merging --- src/main.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index cbff71e645..52d152f73a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,7 +57,10 @@ int main(int args, char **argv) // Save input file to new output filename and quit? if (options.writeInputFilename() || options.toTomlFile()) { - std::string filename = options.writeInputFilename().value_or(options.toTomlFile().value()); + // This should be options.writeInputFilename().or_else(options.totomlFile.value()) + // but that will require C++23 + std::string filename = + options.writeInputFilename() ? options.writeInputFilename().value() : options.toTomlFile().value(); Messenger::print("Saving input file to '{}'...\n", filename); bool result; if (dissolve.worldPool().isMaster())