From f7e20c72523382f509ee6ea992468379a0fb6e85 Mon Sep 17 00:00:00 2001 From: steigemann Date: Sat, 1 Feb 2014 07:57:30 +0000 Subject: [PATCH] added a new flag to ParameterHandler::print_parameters_section for printing all higher subsection elements needed for XML output git-svn-id: https://svn.dealii.org/trunk@32363 0785d39b-7218-0410-832d-ea1e28bc413d --- deal.II/doc/news/changes.h | 11 ++ .../include/deal.II/base/parameter_handler.h | 14 +- deal.II/source/base/parameter_handler.cc | 128 ++++++++++++++++-- 3 files changed, 137 insertions(+), 16 deletions(-) diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index f935735aa8..73d5cb20e9 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -102,6 +102,17 @@ inconvenience this causes.

Specific improvements

    +
  1. New/fixed: The ParameterHandler::print_parameters_section + method not worked for XML output. There is now a flag + include_top_level_elements which prints all higher + subsection elements, default is false. + For XML output setting this flag to true is required + to ensure that the output is a valid XML document, + starting with one root element ParameterHandler and + compatible with read_input_from_xml and the parameterGUI. +
    + (Martin Steigemann, 2014/02/01) +
  2. New: There is now a method to copy the content from a PETScWrappers::MPI::Vector and TrilinosWrappers::MPI::Vector to deal.II's parallel distributed vector. diff --git a/deal.II/include/deal.II/base/parameter_handler.h b/deal.II/include/deal.II/base/parameter_handler.h index 2e1ce3b072..a45d621ef1 100644 --- a/deal.II/include/deal.II/base/parameter_handler.h +++ b/deal.II/include/deal.II/base/parameter_handler.h @@ -1769,7 +1769,7 @@ public: /** * Print all parameters with the given style to out. Presently - * only Text and LaTeX are implemented. + * only Text, LaTeX and XML are implemented. * * In Text format, the output is formatted in such a way that it * is possible to use it for later input again. This is most useful to @@ -1782,6 +1782,10 @@ public: * well as the documenting string given to the declare_entry() function * if available. * + * In XML format, the output starts with one root element + * ParameterHandler in order to get a valid XML document + * and all subsections under it. + * * In Text format, the output contains the same information but * in a format so that the resulting file can be input into a latex * document such as a manual for the code for which this object handles @@ -1817,12 +1821,18 @@ public: * by entering and leaving subsections through the enter_subsection() and * leave_subsection() functions. * + * If include_top_level_elements is true, also + * the higher subsection elements are printed. In XML format + * this is required to get a valid XML document and output starts + * with one root element ParameterHandler. + * * In most cases, you will not want to use this function directly, but * have it called recursively by the previous function. */ void print_parameters_section (std::ostream &out, const OutputStyle style, - const unsigned int indent_level); + const unsigned int indent_level, + const bool include_top_level_elements = false); /** * Print parameters to a logstream. This function allows to print all diff --git a/deal.II/source/base/parameter_handler.cc b/deal.II/source/base/parameter_handler.cc index 8742ec4c08..f448fa2b50 100644 --- a/deal.II/source/base/parameter_handler.cc +++ b/deal.II/source/base/parameter_handler.cc @@ -1858,18 +1858,82 @@ ParameterHandler::print_parameters (std::ostream &out, void ParameterHandler::print_parameters_section (std::ostream &out, const OutputStyle style, - const unsigned int indent_level) + const unsigned int indent_level, + const bool include_top_level_elements) { AssertThrow (out, ExcIO()); const boost::property_tree::ptree ¤t_section = entries->get_child (get_current_path()); + unsigned int overall_indent_level = indent_level; + switch (style) { + case XML: + { + if (include_top_level_elements) + { + // call the writer + // function and exit as + // there is nothing + // further to do down in + // this function + // + // XML has a requirement that + // there can only be one + // single top-level entry, + // but a section has multiple + // entries and sections. we + // work around this by + // creating a tree just for + // this purpose with the + // single top-level node + // "ParameterHandler" and + // assign the full path of + // down to the current section + // under it + boost::property_tree::ptree single_node_tree; + + // if there is no subsection selected, + // add the whole tree of entries, + // otherwise add a root element + // and the selected subsection under it + if (subsection_path.size() == 0) + { + single_node_tree.add_child("ParameterHandler", + *entries); + } + else + { + std::string path ("ParameterHandler"); + + single_node_tree.add_child(path, + boost::property_tree::ptree()); + + path += path_separator + get_current_path (); + single_node_tree.add_child (path, current_section); + }; + + write_xml (out, single_node_tree); + } + else + Assert (false, ExcNotImplemented()); + + break; + } case Text: case ShortText: { + // if there are top level elements to print, do it + if (include_top_level_elements && (subsection_path.size() > 0)) + for (unsigned int i=0; i doc_lines = Utilities:: break_text_into_lines (p->second.get("documentation"), - 78 - indent_level*2 - 2); + 78 - overall_indent_level*2 - 2); for (unsigned int i=0; ifirst) << std::setw(longest_name-demangle(p->first).length()+1) << " " @@ -2043,6 +2107,15 @@ ParameterHandler::print_parameters_section (std::ostream &out, case Description: { + // if there are top level elements to print, do it + if (include_top_level_elements && (subsection_path.size() > 0)) + for (unsigned int i=0; isecond.get("value"); // print name and value - out << std::setw(indent_level*2) << "" + out << std::setw(overall_indent_level*2) << "" << "set " << demangle(p->first) << std::setw(longest_name-demangle(p->first).length()+1) << " " @@ -2076,12 +2149,12 @@ ParameterHandler::print_parameters_section (std::ostream &out, const std::vector description_str = Utilities::break_text_into_lines (p->second.get ("pattern_description"), - 78 - indent_level*2 - 2, '|'); + 78 - overall_indent_level*2 - 2, '|'); if (description_str.size() > 1) { out << std::endl; for (unsigned int i=0; isecond.get("documentation").length() != 0) - out << std::setw(indent_level*2 + longest_name + 10) << "" + out << std::setw(overall_indent_level*2 + longest_name + 10) << "" << "(" << p->second.get("documentation") << ")" << std::endl; } @@ -2109,6 +2182,7 @@ ParameterHandler::print_parameters_section (std::ostream &out, // sections to come, put two newlines // between the last entry and the first // subsection + if (style != XML) { unsigned int n_parameters = 0; unsigned int n_sections = 0; @@ -2128,7 +2202,6 @@ ParameterHandler::print_parameters_section (std::ostream &out, && (n_sections != 0)) out << std::endl << std::endl; - } // now traverse subsections tree, // in alphabetical order @@ -2143,7 +2216,7 @@ ParameterHandler::print_parameters_section (std::ostream &out, case Text: case Description: case ShortText: - out << std::setw(indent_level*2) << "" + out << std::setw(overall_indent_level*2) << "" << "subsection " << demangle(p->first) << std::endl; break; case LaTeX: @@ -2177,7 +2250,7 @@ ParameterHandler::print_parameters_section (std::ostream &out, // then the contents of the // subsection enter_subsection (demangle(p->first)); - print_parameters_section (out, style, indent_level+1); + print_parameters_section (out, style, overall_indent_level+1); leave_subsection (); switch (style) { @@ -2186,14 +2259,14 @@ ParameterHandler::print_parameters_section (std::ostream &out, // subsection. one // blank line after // each subsection - out << std::setw(indent_level*2) << "" + out << std::setw(overall_indent_level*2) << "" << "end" << std::endl << std::endl; // if this is a toplevel // subsection, then have two // newlines - if (indent_level == 0) + if (overall_indent_level == 0) out << std::endl; break; @@ -2202,7 +2275,7 @@ ParameterHandler::print_parameters_section (std::ostream &out, case ShortText: // write end of // subsection. - out << std::setw(indent_level*2) << "" + out << std::setw(overall_indent_level*2) << "" << "end" << std::endl; break; case LaTeX: @@ -2213,6 +2286,33 @@ ParameterHandler::print_parameters_section (std::ostream &out, } } + // close top level elements, if there are any + switch (style) + { + case XML: + case LaTeX: + case Description: + break; + case Text: + case ShortText: + { + if (include_top_level_elements && (subsection_path.size() > 0)) + for (unsigned int i=0; i