From ab325fe2f67f9fec71afae4a119d4f8702058a14 Mon Sep 17 00:00:00 2001 From: Mark Meredith Date: Thu, 16 Apr 2020 16:56:00 -0400 Subject: [PATCH] Add time --- .clang-format | 137 +++++++++++++++++++++++++++++++++++++++++++++ .gitmodules | 3 - src/geometry.cpp | 64 +++++++++++++++++++++ src/meson.build | 2 + src/solver.cpp | 62 +++----------------- src/solver.hpp | 11 ++++ src/time.cpp | 59 +++++++++++++++++++ subprojects/mfix | 1 - tests/solver.t.cpp | 23 +++++++- 9 files changed, 302 insertions(+), 60 deletions(-) create mode 100644 .clang-format create mode 100644 src/geometry.cpp create mode 100644 src/time.cpp delete mode 160000 subprojects/mfix diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..33bf2a3 --- /dev/null +++ b/.clang-format @@ -0,0 +1,137 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveMacros: false +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Right +AlignOperands: true +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + - Regex: '.*' + Priority: 1 + SortPriority: 0 +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentCaseLabels: false +IndentGotoLabels: true +IndentPPDirectives: None +IndentWidth: 2 +IndentWrappedFunctionNames: false +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Right +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +Standard: Latest +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseCRLF: false +UseTab: Never +... + diff --git a/.gitmodules b/.gitmodules index 25ed73a..3fe94d7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,9 +1,6 @@ [submodule "PEGTL"] path = subprojects/PEGTL url = https://github.com/taocpp/PEGTL -[submodule "mfix/subprojects/mfix"] - path = subprojects/mfix - url = ../mfix [submodule "subprojects/Catch2"] path = subprojects/Catch2 url = https://github.com/catchorg/Catch2 diff --git a/src/geometry.cpp b/src/geometry.cpp new file mode 100644 index 0000000..cdf2bd2 --- /dev/null +++ b/src/geometry.cpp @@ -0,0 +1,64 @@ +#include + +#include "solver.hpp" + +namespace solver { + +std::optional make_geometry(solver::InputInfo ii) { + solver::GeometrySettings geo; + std::string PROB_LO("geometry.prob_lo"); + std::string PROB_HI("geometry.prob_hi"); + std::string PERIODIC("geometry.is_periodic"); + std::string N_CELL("amr.n_cell"); + if (!ii.count(PROB_LO)) { + require(PROB_LO); + return std::nullopt; + } + if (!ii.count(PROB_HI)) { + require(PROB_HI); + return std::nullopt; + } + if (!ii.count(PERIODIC)) { + require(PERIODIC); + return std::nullopt; + } + if (!ii.count(N_CELL)) { + require(N_CELL); + return std::nullopt; + } + auto lows = std::get(ii[PROB_LO]); + auto highs = std::get(ii[PROB_HI]); + auto is_periodic = std::get(ii[PERIODIC]); + auto n_cell = std::get(ii[N_CELL]); + if (lows.size() != 3) { + std::cout << "prob_lo needs 3 elements " << std::endl; + return std::nullopt; + } + if (highs.size() != 3) { + std::cout << "prob_hi needs 3 elements " << std::endl; + return std::nullopt; + } + if (is_periodic.size() != 3) { + std::cout << "periodic needs 3 elements " << std::endl; + return std::nullopt; + } + if (n_cell.size() != 3) { + std::cout << "n_cell needs 3 elements " << std::endl; + return std::nullopt; + } + + std::get<0>(geo.axes).high = highs[0]; + std::get<0>(geo.axes).low = lows[0]; + std::get<0>(geo.axes).periodic = is_periodic[0]; + std::get<0>(geo.axes).n_cell = n_cell[0]; + std::get<1>(geo.axes).high = highs[1]; + std::get<1>(geo.axes).low = lows[1]; + std::get<1>(geo.axes).periodic = is_periodic[1]; + std::get<1>(geo.axes).n_cell = n_cell[1]; + std::get<2>(geo.axes).high = highs[2]; + std::get<2>(geo.axes).low = lows[2]; + std::get<2>(geo.axes).periodic = is_periodic[2]; + std::get<2>(geo.axes).n_cell = n_cell[2]; + return geo; +} +} // namespace solver diff --git a/src/meson.build b/src/meson.build index fadb805..fc2c2cd 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,6 +1,8 @@ lib_parser = static_library('mfix-parser', + 'geometry.cpp', 'parser.cpp', 'solver.cpp', + 'time.cpp', include_directories: tao_inc, install : true) diff --git a/src/solver.cpp b/src/solver.cpp index 800293a..9a441d4 100644 --- a/src/solver.cpp +++ b/src/solver.cpp @@ -4,7 +4,7 @@ #include "solver.hpp" -namespace { +namespace solver { void require(std::string key) { std::cout << "missing required key: " << key << std::endl; @@ -12,64 +12,20 @@ void require(std::string key) { std::optional do_make_solver(solver::InputInfo ii) { solver::SolverSettings ss; - std::string PROB_LO("geometry.prob_lo"); - std::string PROB_HI("geometry.prob_hi"); - std::string PERIODIC("geometry.is_periodic"); - std::string N_CELL("amr.n_cell"); - if (!ii.count(PROB_LO)) { - require(PROB_LO); - return std::nullopt; - } - if (!ii.count(PROB_HI)) { - require(PROB_HI); - return std::nullopt; - } - if (!ii.count(PERIODIC)) { - require(PERIODIC); - return std::nullopt; - } - if (!ii.count(N_CELL)) { - require(N_CELL); - return std::nullopt; - } - auto lows = std::get(ii[PROB_LO]); - auto highs = std::get(ii[PROB_HI]); - auto is_periodic = std::get(ii[PERIODIC]); - auto n_cell = std::get(ii[N_CELL]); - if (lows.size() != 3) { - std::cout << "prob_lo needs 3 elements " << std::endl; - return std::nullopt; - } - if (highs.size() != 3) { - std::cout << "prob_hi needs 3 elements " << std::endl; - return std::nullopt; - } - if (is_periodic.size() != 3) { - std::cout << "periodic needs 3 elements " << std::endl; + auto new_geo = make_geometry(ii); + if (!new_geo.has_value()) { + std::cout << "Problem making geometry" << std::endl; return std::nullopt; } - if (n_cell.size() != 3) { - std::cout << "n_cell needs 3 elements " << std::endl; + auto new_time = make_time(ii); + if (!new_time.has_value()) { + std::cout << "Problem making time" << std::endl; return std::nullopt; } - - std::get<0>(ss.geometry.axes).high = highs[0]; - std::get<0>(ss.geometry.axes).low = lows[0]; - std::get<0>(ss.geometry.axes).periodic = is_periodic[0]; - std::get<0>(ss.geometry.axes).n_cell = n_cell[0]; - std::get<1>(ss.geometry.axes).high = highs[1]; - std::get<1>(ss.geometry.axes).low = lows[1]; - std::get<1>(ss.geometry.axes).periodic = is_periodic[1]; - std::get<1>(ss.geometry.axes).n_cell = n_cell[1]; - std::get<2>(ss.geometry.axes).high = highs[2]; - std::get<2>(ss.geometry.axes).low = lows[2]; - std::get<2>(ss.geometry.axes).periodic = is_periodic[2]; - std::get<2>(ss.geometry.axes).n_cell = n_cell[2]; + ss.geometry = new_geo.value(); + ss.time = new_time.value(); return ss; } -} // namespace - -namespace solver { std::optional make_solver(solver::InputInfo ii) { auto maybe_state = do_make_solver(ii); diff --git a/src/solver.hpp b/src/solver.hpp index 41ea2c7..f70a1f8 100644 --- a/src/solver.hpp +++ b/src/solver.hpp @@ -25,12 +25,23 @@ struct GeometryAxis { struct GeometrySettings { std::array axes; }; +struct TimeSettings { + unsigned int max_step; + double dt_max; + double dt_min; + double tstop; +}; struct SolverSettings { GeometrySettings geometry; + TimeSettings time; }; std::optional make_solver(solver::InputInfo); +std::optional make_geometry(solver::InputInfo); +std::optional make_time(solver::InputInfo); std::string serialize(solver::SolverSettings); + +void require(std::string); } // namespace solver #endif diff --git a/src/time.cpp b/src/time.cpp new file mode 100644 index 0000000..2448307 --- /dev/null +++ b/src/time.cpp @@ -0,0 +1,59 @@ +#include + +#include "solver.hpp" + +namespace solver { + +std::optional make_time(solver::InputInfo ii) { + solver::TimeSettings time; + + std::string DT_MAX("mfix.dt_max"); + std::string DT_MIN("mfix.dt_min"); + std::string MAXSTEP("mfix.max_step"); + std::string TSTOP("mfix.stop_time"); + + if (!ii.count(DT_MAX)) { + require(DT_MAX); + return std::nullopt; + } + if (!ii.count(DT_MIN)) { + require(DT_MIN); + return std::nullopt; + } + if (!ii.count(MAXSTEP)) { + require(MAXSTEP); + return std::nullopt; + } + if (!ii.count(TSTOP)) { + require(TSTOP); + return std::nullopt; + } + auto dt_max = std::get(ii[DT_MAX]); + auto dt_min = std::get(ii[DT_MIN]); + auto maxstep = std::get(ii[MAXSTEP]); + auto tstop = std::get(ii[TSTOP]); + if (dt_max.size() != 1) { + std::cout << "dt_max is a scalar: " << dt_max.size() << std::endl; + return std::nullopt; + } + if (dt_min.size() != 1) { + std::cout << "dt_min is a scalar" << std::endl; + return std::nullopt; + } + if (maxstep.size() != 1) { + std::cout << "max_step is a scalar" << std::endl; + return std::nullopt; + } + if (tstop.size() != 1) { + std::cout << "tstop is a scalar" << std::endl; + return std::nullopt; + } + + time.dt_max = dt_max[0]; + time.dt_min = dt_min[0]; + time.max_step = maxstep[0]; + time.tstop = tstop[0]; + + return time; +} +} // namespace solver diff --git a/subprojects/mfix b/subprojects/mfix deleted file mode 160000 index e4aaa45..0000000 --- a/subprojects/mfix +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e4aaa45a48378bf8f220a359994a42066692f194 diff --git a/tests/solver.t.cpp b/tests/solver.t.cpp index 0d97270..839780f 100644 --- a/tests/solver.t.cpp +++ b/tests/solver.t.cpp @@ -4,10 +4,14 @@ #include -std::string PROB_LO("geometry.prob_lo"); -std::string PROB_HI("geometry.prob_hi"); -std::string PERIODIC("geometry.is_periodic"); +std::string DT_MAX("mfix.dt_max"); +std::string DT_MIN("mfix.dt_min"); +std::string MAXSTEP("mfix.max_step"); std::string N_CELL("amr.n_cell"); +std::string PERIODIC("geometry.is_periodic"); +std::string PROB_HI("geometry.prob_hi"); +std::string PROB_LO("geometry.prob_lo"); +std::string TSTOP("mfix.stop_time"); TEST_CASE("empty (invalid) map", "[]") { solver::InputInfo ii; @@ -21,6 +25,10 @@ TEST_CASE("from_origin", "[]") { ii[PROB_HI] = solver::NumberArray({0.004, 0.001, 0.001}); ii[PERIODIC] = solver::NumberArray({0, 0, 0}); ii[N_CELL] = solver::NumberArray({11, 22, 33}); + ii[DT_MAX] = solver::NumberArray({99.99}); + ii[DT_MIN] = solver::NumberArray({0.001}); + ii[MAXSTEP] = solver::NumberArray({39}); + ii[TSTOP] = solver::NumberArray({3.14}); CHECK(ii.count(PROB_LO)); CHECK(ii.count(PROB_HI)); CHECK(ii.count(PERIODIC)); @@ -42,6 +50,11 @@ TEST_CASE("from_origin", "[]") { CHECK(xx.n_cell == 11); CHECK(yy.n_cell == 22); CHECK(zz.n_cell == 33); + + CHECK(sv.time.dt_max == 99.99); + CHECK(sv.time.dt_min == 0.001); + CHECK(sv.time.max_step == 39); + CHECK(sv.time.tstop == 3.14); } TEST_CASE("negative_positive", "[]") { @@ -50,6 +63,10 @@ TEST_CASE("negative_positive", "[]") { ii[PROB_HI] = std::vector({11.1, 22.2, 33.3}); ii[PERIODIC] = solver::NumberArray({1, 0, 1}); ii[N_CELL] = solver::NumberArray({11, 22, 33}); + ii[DT_MAX] = solver::NumberArray({99.99}); + ii[DT_MIN] = solver::NumberArray({0.001}); + ii[MAXSTEP] = solver::NumberArray({39}); + ii[TSTOP] = solver::NumberArray({3.14}); auto maybe_sv = solver::make_solver(ii); -- GitLab