From: Luca Heltai Date: Tue, 28 Apr 2020 21:09:39 +0000 (+0200) Subject: Make all possible Short combinations of output work. X-Git-Tag: v9.2.0-rc1~158^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2974a1d87905a484d712dd68556ccabc8fae91ef;p=dealii.git Make all possible Short combinations of output work. --- diff --git a/include/deal.II/base/parameter_handler.h b/include/deal.II/base/parameter_handler.h index e91a3517bc..80bdcebdca 100644 --- a/include/deal.II/base/parameter_handler.h +++ b/include/deal.II/base/parameter_handler.h @@ -852,20 +852,13 @@ public: * - format options: PRM, LaTeX, Description, XML, JSON * - stylistic options: Short, KeepDeclarationOrder * - * A valid combination of options is given in the following table: + * Only one format option may be specified at the time. Any function that + * accepts an OutputStyle as an option will throw if you specify more than + * one. * - * Stylistic options | Default | PRM | LaTeX | Description | XML | JSON - * -------------------- | ------- | --- | ----- | ----------- | --- | ---- - * Default | X | X | X | X | X | X - * Short | X | X | - | X | X | X - * KeepDeclarationOrder | X | X | X | X | X | X - * - * Valid combinations are checked at the beginning of the functions that - * accept an OutputStyle as an input argument. - * - * Furthermore, a number of shortcuts of commonly used option combinations are - * provided. E.g., ShortPRM prints the parameters in the PRM - * format, while skipping the documentation. + * A number of shortcuts of commonly used option combinations are provided. + * E.g., ShortPRM prints the parameters in the PRM format, while skipping the + * documentation. */ enum OutputStyle { @@ -929,13 +922,13 @@ public: JSON = 0x0100, /** - * Write input for ParameterHandler without comments or changed default + * Write the content of ParameterHandler without comments or changed default * values. */ ShortPRM = PRM | Short, /** - * Write input for ParameterHandler without comments or changed default + * Write the content of ParameterHandler without comments or changed default * values. * * @deprecated Use `ShortPRM` instead of `ShortText`. @@ -943,16 +936,22 @@ public: ShortText = ShortPRM, /** - * Write input for ParameterHandler without comments or changed default + * Write the content of ParameterHandler without comments or changed default * values as a XML file. */ ShortXML = XML | Short, /** - * Write input for ParameterHandler without comments or changed default + * Write the content of ParameterHandler without comments or changed default * values as a JSON file. */ ShortJSON = JSON | Short, + + /** + * Write the content of ParameterHandler without comments or changed default + * values as a LaTeX file. + */ + ShortLaTeX = LaTeX | Short, }; @@ -1422,10 +1421,10 @@ public: * available. * * By using the flag Short in combination with PRM, - * XML, or JSON (or by using the shortcuts - * ShortPRM, ShortXML, or ShortJSON), a reduced - * output can be generated, only containing the values and skipping the - * documentation. + * XML, JSON, or LaTeX (or by using the shortcuts + * ShortPRM, ShortXML, ShortJSON, or + * ShortLaTeX), a reduced output can be generated, only containing + * the values and skipping the documentation. * * In XML format, the output starts with one root element * ParameterHandler in order to get a valid XML document and all diff --git a/source/base/parameter_handler.cc b/source/base/parameter_handler.cc index 07716b8826..022c918210 100644 --- a/source/base/parameter_handler.cc +++ b/source/base/parameter_handler.cc @@ -342,11 +342,8 @@ namespace ((style & ParameterHandler::Description) != 0) + ((style & ParameterHandler::LaTeX) != 0)) == 1, ExcMessage( - "You have chosen either no or multiple style formats. You can choose between: PRM, Description, LaTeX, XML, JSON.")); - - AssertThrow(((style & ParameterHandler::LaTeX) && - (style & ParameterHandler::Short)) == false, - ExcMessage("It is not possible to request short LaTex.")); + "You have chosen either no or multiple style formats. You can choose " + "between: PRM, Description, LaTeX, XML, JSON.")); } } // namespace @@ -534,10 +531,7 @@ ParameterHandler::parse_input(const std::string &filename, const bool assert_mandatory_entries_are_found) { std::ifstream is(filename); - AssertThrow(is, - ExcMessage("Invalid filename " + filename + - " provided. File does not exist or " - "can not be read from.")); + AssertThrow(is, PathSearch::ExcFileNotFound(filename, "ParameterHandler")); std::string file_ending = filename.substr(filename.find_last_of('.') + 1); boost::algorithm::to_lower(file_ending); @@ -672,8 +666,7 @@ namespace // // This function is strongly based on read_xml_recursively(). void - recursively_remove_documentation_from_tree( - boost::property_tree::ptree &source) + recursively_compress_tree(boost::property_tree::ptree &source) { for (auto &p : source) { @@ -691,7 +684,7 @@ namespace else { // it must be a subsection - recursively_remove_documentation_from_tree(p.second); + recursively_compress_tree(p.second); } } } @@ -1272,11 +1265,11 @@ ParameterHandler::print_parameters(std::ostream & out, // done recursively in our own code. take care of the two special formats // first - // explicity eliminate the documentation from the tree if requested - if (style & Short) + // explicity compress the tree if requested + if ((style & Short) && (style & (XML | JSON))) { // modify the copy of the tree - recursively_remove_documentation_from_tree(current_entries); + recursively_compress_tree(current_entries); } if (style & XML) @@ -1436,8 +1429,6 @@ ParameterHandler::recursively_print_parameters( << "# " << doc_line << '\n'; } - - // print name and value of this entry out << std::setw(overall_indent_level * 2) << "" << "set " << demangle(p.first) @@ -1525,20 +1516,22 @@ ParameterHandler::recursively_print_parameters( // if there is a documenting string, print it as well but // don't escape to allow formatting/formulas - if (!(style & Short)) - if (!p.second.get("documentation").empty()) - out << "{\\it Description:} " - << p.second.get("documentation") << "\n\n" - << '\n'; - - // also output possible values, do not escape because the - // description internally will use LaTeX formatting - const unsigned int pattern_index = - p.second.get("pattern"); - const std::string desc_str = - patterns[pattern_index]->description( - Patterns::PatternBase::LaTeX); - out << "{\\it Possible values:} " << desc_str << '\n'; + if (!is_short && + !p.second.get("documentation").empty()) + out << "{\\it Description:} " + << p.second.get("documentation") << "\n\n" + << '\n'; + if (!is_short) + { + // also output possible values, do not escape because the + // description internally will use LaTeX formatting + const unsigned int pattern_index = + p.second.get("pattern"); + const std::string desc_str = + patterns[pattern_index]->description( + Patterns::PatternBase::LaTeX); + out << "{\\it Possible values:} " << desc_str << '\n'; + } } else if (is_alias_node(p.second) == true) { @@ -1630,7 +1623,8 @@ ParameterHandler::recursively_print_parameters( out << '\n'; // if there is a documenting string, print it as well - if (p.second.get("documentation").length() != 0) + if (!is_short && + p.second.get("documentation").length() != 0) out << std::setw(overall_indent_level * 2 + longest_name + 10) << "" << "(" << p.second.get("documentation") << ")" @@ -1726,11 +1720,11 @@ ParameterHandler::recursively_print_parameters( if (overall_indent_level == 0) out << '\n'; } - else if (style == Description) + else if (style & Description) { // nothing to do } - else if (style == LaTeX) + else if (style & LaTeX) { // nothing to do } diff --git a/tests/parameter_handler/parameter_acceptor_05.output b/tests/parameter_handler/parameter_acceptor_05.output index 1ce6b94efc..7357ac8dba 100644 --- a/tests/parameter_handler/parameter_acceptor_05.output +++ b/tests/parameter_handler/parameter_acceptor_05.output @@ -9,4 +9,4 @@ DEAL:parameters:Second Class::Second int: 5 DEAL:parameters:Second Class::Second string: bye bye DEAL::reading used_parameter_acceptor_05.xml DEAL:: -DEAL::truetrue2[Bool]27.77.71[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]1330[Integer range -2147483648...2147483647 (inclusive)]0hellohello3[Anything]3falsefalse6[Bool]69.99.95[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]5554[Integer range -2147483648...2147483647 (inclusive)]4bye byebye bye7[Anything]7 +DEAL::true7.73hellofalse9.95bye bye diff --git a/tests/parameter_handler/parameter_acceptor_06.output b/tests/parameter_handler/parameter_acceptor_06.output index 0da52b1697..9391c61279 100644 --- a/tests/parameter_handler/parameter_acceptor_06.output +++ b/tests/parameter_handler/parameter_acceptor_06.output @@ -30,7 +30,6 @@ DEAL:: DEAL::{\it Default:} true DEAL:: DEAL:: -DEAL::{\it Possible values:} A boolean value (true or false) DEAL::\item {\it Parameter name:} {\tt First double} DEAL::\phantomsection\label{parameters:First Class/First double} DEAL::\label{parameters:First_20Class/First_20double} @@ -41,10 +40,9 @@ DEAL::\index[prmindexfull]{First Class!First double} DEAL::{\it Value:} 7.7 DEAL:: DEAL:: -DEAL::{\it Default:} 7.700000 +DEAL::{\it Default:} 7.7 DEAL:: DEAL:: -DEAL::{\it Possible values:} A floating point number $v$ such that $-\text{MAX\_DOUBLE} \leq v \leq \text{MAX\_DOUBLE}$ DEAL::\item {\it Parameter name:} {\tt First int} DEAL::\phantomsection\label{parameters:First Class/First int} DEAL::\label{parameters:First_20Class/First_20int} @@ -58,7 +56,6 @@ DEAL:: DEAL::{\it Default:} 3 DEAL:: DEAL:: -DEAL::{\it Possible values:} An integer $n$ such that $-2147483648\leq n \leq 2147483647$ DEAL::\item {\it Parameter name:} {\tt First string} DEAL::\phantomsection\label{parameters:First Class/First string} DEAL::\label{parameters:First_20Class/First_20string} @@ -72,7 +69,6 @@ DEAL:: DEAL::{\it Default:} hello DEAL:: DEAL:: -DEAL::{\it Possible values:} Any string DEAL::\end{itemize} DEAL:: DEAL::\subsection{Parameters in section \tt Second Class} @@ -92,7 +88,6 @@ DEAL:: DEAL::{\it Default:} false DEAL:: DEAL:: -DEAL::{\it Possible values:} A boolean value (true or false) DEAL::\item {\it Parameter name:} {\tt Second double} DEAL::\phantomsection\label{parameters:Second Class/Second double} DEAL::\label{parameters:Second_20Class/Second_20double} @@ -103,10 +98,9 @@ DEAL::\index[prmindexfull]{Second Class!Second double} DEAL::{\it Value:} 9.9 DEAL:: DEAL:: -DEAL::{\it Default:} 9.900000 +DEAL::{\it Default:} 9.9 DEAL:: DEAL:: -DEAL::{\it Possible values:} A floating point number $v$ such that $-\text{MAX\_DOUBLE} \leq v \leq \text{MAX\_DOUBLE}$ DEAL::\item {\it Parameter name:} {\tt Second int} DEAL::\phantomsection\label{parameters:Second Class/Second int} DEAL::\label{parameters:Second_20Class/Second_20int} @@ -120,7 +114,6 @@ DEAL:: DEAL::{\it Default:} 5 DEAL:: DEAL:: -DEAL::{\it Possible values:} An integer $n$ such that $-2147483648\leq n \leq 2147483647$ DEAL::\item {\it Parameter name:} {\tt Second string} DEAL::\phantomsection\label{parameters:Second Class/Second string} DEAL::\label{parameters:Second_20Class/Second_20string} @@ -134,5 +127,4 @@ DEAL:: DEAL::{\it Default:} bye bye DEAL:: DEAL:: -DEAL::{\it Possible values:} Any string DEAL::\end{itemize} diff --git a/tests/parameter_handler/parameter_acceptor_07.expect=run.output b/tests/parameter_handler/parameter_acceptor_07.expect=run.output index e69de29bb2..8b13789179 100644 --- a/tests/parameter_handler/parameter_acceptor_07.expect=run.output +++ b/tests/parameter_handler/parameter_acceptor_07.expect=run.output @@ -0,0 +1 @@ + diff --git a/tests/parameter_handler/parameter_acceptor_09.cc b/tests/parameter_handler/parameter_acceptor_09.cc new file mode 100644 index 0000000000..152e77e189 --- /dev/null +++ b/tests/parameter_handler/parameter_acceptor_09.cc @@ -0,0 +1,62 @@ +//----------------------------------------------------------- +// +// Copyright (C) 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +//----------------------------------------------------------- + +// Test that ParameterAcceptor::initialize() works as expected + + +#include +#include + +#include "../tests.h" + +// Test subsectioning + +class Test : public ParameterAcceptor +{ +public: + Test() + { + add_parameter("A point", a_point); + }; + +private: + Point<3> a_point; +}; + +void +test_ext(const std::string &ext) +{ + Test a; + + deallog << "Generate and read input." << ext << std::endl; + try + { + ParameterAcceptor::initialize("input." + ext); + } + catch (...) + { + // The above call must have created a file named input.ext + cat_file(("input." + ext).c_str()); + } +} + +int +main() +{ + initlog(); + test_ext("prm"); + test_ext("xml"); + test_ext("json"); +} diff --git a/tests/parameter_handler/parameter_acceptor_09.output b/tests/parameter_handler/parameter_acceptor_09.output new file mode 100644 index 0000000000..f2d1ef7108 --- /dev/null +++ b/tests/parameter_handler/parameter_acceptor_09.output @@ -0,0 +1,30 @@ + +DEAL::Generate and read input.prm +# Listing of Parameters +# --------------------- +subsection Test + set A point = 0, 0, 0 +end + + + +DEAL::Generate and read input.xml + +0, 0, 00, 0, 01[List of <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]> of length 3...3 (inclusive)]0,1 + +DEAL::Generate and read input.json +{ + "Test": + { + "A_20point": + { + "value": "0, 0, 0", + "default_value": "0, 0, 0", + "documentation": "", + "pattern": "2", + "pattern_description": "[List of <[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]> of length 3...3 (inclusive)]", + "actions": "0,1,2" + } + } +} + diff --git a/tests/parameter_handler/parameter_handler_25.output b/tests/parameter_handler/parameter_handler_25.output index 469de72374..773966bd7c 100644 --- a/tests/parameter_handler/parameter_handler_25.output +++ b/tests/parameter_handler/parameter_handler_25.output @@ -4,10 +4,10 @@ DEAL::successful DEAL:: -------------------------------------------------------- An error occurred in file in function - void dealii::ParameterHandler::set(const string&, const string&) -The violated condition was: + void dealii::ParameterHandler::set(const std::string &, const std::string &) +The violated condition was: false -Additional information: +Additional information: You can't ask for entry you have not yet declared. -------------------------------------------------------- @@ -15,10 +15,10 @@ DEAL:: -------------------------------------------------------- An error occurred in file in function void dealii::ParameterHandler::assert_that_entries_have_been_set() const -The violated condition was: +The violated condition was: entries_wrongly_not_set.size() == 0 -Additional information: - Not all entries of the parameter handler that were declared with `has_to_be_set = true` have been set. The following parameters +Additional information: + Not all entries of the parameter handler that were declared with `has_to_be_set = true` have been set. The following parameters General.Precision General.dim diff --git a/tests/parameter_handler/parameter_handler_27.cc b/tests/parameter_handler/parameter_handler_27.cc new file mode 100644 index 0000000000..b92f38f4ef --- /dev/null +++ b/tests/parameter_handler/parameter_handler_27.cc @@ -0,0 +1,64 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// test ParameterHandler::print_parameters for different formats + +#include + +#include "../tests.h" + +int +main() +{ + initlog(); + + double a = 1.0; + bool some_bool = true; + std::string a_string = "Ciao"; + + ParameterHandler prm; + prm.enter_subsection("Testing"); + prm.add_parameter("A double", a); + prm.add_parameter("A bool", some_bool); + prm.add_parameter("A string", a_string); + prm.leave_subsection(); + + deallog << "PRM format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.prm"); + cat_file("output.prm"); + + deallog << "XML format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.xml"); + cat_file("output.xml"); + + deallog << "JSON format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.json"); + cat_file("output.json"); + + deallog << "LaTeX format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.tex"); + cat_file("output.tex"); + + deallog << "Description format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.dsc", ParameterHandler::Description); + cat_file("output.dsc"); +} diff --git a/tests/parameter_handler/parameter_handler_27.output b/tests/parameter_handler/parameter_handler_27.output new file mode 100644 index 0000000000..ddc73783bd --- /dev/null +++ b/tests/parameter_handler/parameter_handler_27.output @@ -0,0 +1,117 @@ + +DEAL::PRM format: +DEAL::======================================== +# Listing of Parameters +# --------------------- +subsection Testing + set A bool = true + set A double = 1 + set A string = Ciao +end + + + +DEAL::XML format: +DEAL::======================================== + +truetrue1[Bool]1110[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]0CiaoCiao2[Anything]2 + +DEAL::JSON format: +DEAL::======================================== +{ + "Testing": + { + "A_20bool": + { + "value": "true", + "default_value": "true", + "documentation": "", + "pattern": "1", + "pattern_description": "[Bool]", + "actions": "1" + }, + "A_20double": + { + "value": "1", + "default_value": "1", + "documentation": "", + "pattern": "0", + "pattern_description": "[Double -MAX_DOUBLE...MAX_DOUBLE (inclusive)]", + "actions": "0" + }, + "A_20string": + { + "value": "Ciao", + "default_value": "Ciao", + "documentation": "", + "pattern": "2", + "pattern_description": "[Anything]", + "actions": "2" + } + } +} + +DEAL::LaTeX format: +DEAL::======================================== +\subsection{Global parameters} +\label{parameters:global} + + + +\subsection{Parameters in section \tt Testing} +\label{parameters:Testing} + +\begin{itemize} +\item {\it Parameter name:} {\tt A bool} +\phantomsection\label{parameters:Testing/A bool} +\label{parameters:Testing/A_20bool} + + +\index[prmindex]{A bool} +\index[prmindexfull]{Testing!A bool} +{\it Value:} true + + +{\it Default:} true + + +{\it Possible values:} A boolean value (true or false) +\item {\it Parameter name:} {\tt A double} +\phantomsection\label{parameters:Testing/A double} +\label{parameters:Testing/A_20double} + + +\index[prmindex]{A double} +\index[prmindexfull]{Testing!A double} +{\it Value:} 1 + + +{\it Default:} 1 + + +{\it Possible values:} A floating point number $v$ such that $-\text{MAX\_DOUBLE} \leq v \leq \text{MAX\_DOUBLE}$ +\item {\it Parameter name:} {\tt A string} +\phantomsection\label{parameters:Testing/A string} +\label{parameters:Testing/A_20string} + + +\index[prmindex]{A string} +\index[prmindexfull]{Testing!A string} +{\it Value:} Ciao + + +{\it Default:} Ciao + + +{\it Possible values:} Any string +\end{itemize} + +DEAL::Description format: +DEAL::======================================== +Listing of Parameters: + +subsection Testing + set A bool = A boolean value (true or false) + set A double = A floating point number v such that -MAX_DOUBLE <= v <= MAX_DOUBLE + set A string = Any string + diff --git a/tests/parameter_handler/parameter_handler_28.cc b/tests/parameter_handler/parameter_handler_28.cc new file mode 100644 index 0000000000..2972407453 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_28.cc @@ -0,0 +1,65 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2020 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// test ParameterHandler::print_parameters for different formats in +// combination with Short + +#include + +#include "../tests.h" + +int +main() +{ + initlog(1); + + double a = 1.0; + double b = 2.0; + + ParameterHandler prm; + prm.add_parameter("A double", a, "Documentation for a."); + prm.enter_subsection("Section two"); + prm.add_parameter("Another double", b, "Documentation for b."); + prm.leave_subsection(); + + const auto style = ParameterHandler::Short; + + deallog << "ShortPRM format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.prm", style); + cat_file("output.prm"); + + deallog << "ShortXML format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.xml", style); + cat_file("output.xml"); + + deallog << "ShortJSON format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.json", style); + cat_file("output.json"); + + deallog << "ShortLaTeX format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.tex", style); + cat_file("output.tex"); + + deallog << "Short | Description format: " << std::endl + << "========================================" << std::endl; + prm.print_parameters("output.dsc", ParameterHandler::Description | style); + cat_file("output.dsc"); +} diff --git a/tests/parameter_handler/parameter_handler_28.output b/tests/parameter_handler/parameter_handler_28.output new file mode 100644 index 0000000000..d880f73f25 --- /dev/null +++ b/tests/parameter_handler/parameter_handler_28.output @@ -0,0 +1,73 @@ + +DEAL::ShortPRM format: +DEAL::======================================== +set A double = 1 +subsection Section two + set Another double = 2 +end + +DEAL::ShortXML format: +DEAL::======================================== + +12 + +DEAL::ShortJSON format: +DEAL::======================================== +{ + "A_20double": "1", + "Section_20two": { + "Another_20double": "2" + } +} + +DEAL::ShortLaTeX format: +DEAL::======================================== +\subsection{Global parameters} +\label{parameters:global} + + +\begin{itemize} +\item {\it Parameter name:} {\tt A double} +\phantomsection\label{parameters:A double} +\label{parameters:A_20double} + + +\index[prmindex]{A double} +\index[prmindexfull]{A double} +{\it Value:} 1 + + +{\it Default:} 1 + + +\end{itemize} + + + +\subsection{Parameters in section \tt Section two} +\label{parameters:Section_20two} + +\begin{itemize} +\item {\it Parameter name:} {\tt Another double} +\phantomsection\label{parameters:Section two/Another double} +\label{parameters:Section_20two/Another_20double} + + +\index[prmindex]{Another double} +\index[prmindexfull]{Section two!Another double} +{\it Value:} 2 + + +{\it Default:} 2 + + +\end{itemize} + +DEAL::Short | Description format: +DEAL::======================================== +Listing of Parameters: + +set A double = A floating point number v such that -MAX_DOUBLE <= v <= MAX_DOUBLE +subsection Section two + set Another double = A floating point number v such that -MAX_DOUBLE <= v <= MAX_DOUBLE +