From: Wolfgang Bangerth Date: Tue, 3 Jan 2006 17:39:04 +0000 (+0000) Subject: Add ParameterHandler::set X-Git-Tag: v8.0.0~12718 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=844aaa28d82ea0c296d607c071704fa466ea6e8d;p=dealii.git Add ParameterHandler::set git-svn-id: https://svn.dealii.org/trunk@11941 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/parameter_handler.h b/deal.II/base/include/base/parameter_handler.h index 11e1e5a3c4..81223ececa 100644 --- a/deal.II/base/include/base/parameter_handler.h +++ b/deal.II/base/include/base/parameter_handler.h @@ -2,7 +2,7 @@ // $Id$ // Version: $Name$ // -// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 by the deal.II authors +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -1368,20 +1368,76 @@ class ParameterHandler /** * Return value of entry - * entry_string as + * entry_name as * double. */ - double get_double (const std::string &entry_string) const; + double get_double (const std::string &entry_name) const; /** * Return value of entry - * entry_string as bool. + * entry_name as bool. * The entry may be "true" or "yes" * for true, "false" or * "no" for false respectively. */ - bool get_bool (const std::string &entry_string) const; + bool get_bool (const std::string &entry_name) const; + + /** + * Change the value presently stored for + * entry_name to the one given + * in the second argument. + * + * The parameter must already exist in + * the present subsection. + */ + void set (const std::string &entry_name, + const std::string &new_value); + void set (const std::string &entry_name, + const char *new_value); + + /** + * Change the value presently stored for + * entry_name to the one given + * in the second argument. + * + * The parameter must already exist in + * the present subsection. + */ + void set (const std::string &entry_name, + const long int &new_value); + + /** + * Change the value presently stored for + * entry_name to the one given + * in the second argument. + * + * The parameter must already exist in + * the present subsection. + * + * For internal purposes, the new value + * needs to be converted to a + * string. This is done using 16 digits + * of accuracy, so the set value and the + * one you can get back out using + * get_double() may differ in the 16th + * digit. + */ + void set (const std::string &entry_name, + const double &new_value); + + /** + * Change the value presently stored for + * entry_name to the one given + * in the second argument. + * + * The parameter must already exist in + * the present subsection. + */ + void set (const std::string &entry_name, + const bool &new_value); + + /** * Print all parameters with the * given style to diff --git a/deal.II/base/source/parameter_handler.cc b/deal.II/base/source/parameter_handler.cc index 604f9ff79d..ccb3a5eeeb 100644 --- a/deal.II/base/source/parameter_handler.cc +++ b/deal.II/base/source/parameter_handler.cc @@ -2,7 +2,7 @@ // $Id$ // Version: $Name$ // -// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 by the deal.II authors +// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -727,13 +727,14 @@ bool ParameterHandler::leave_subsection () -const std::string & ParameterHandler::get (const std::string &entry_string) const +const std::string & +ParameterHandler::get (const std::string &entry_string) const { const Section* pd = get_present_defaults_subsection (); const Section* pc = get_present_changed_subsection (); - // assert that the according entry is already - // declared in the defaults tree + // assert that the according entry is + // already declared in the defaults tree Assert (pd->entries.find (entry_string) != pd->entries.end(), ExcEntryUndeclared(entry_string)); @@ -790,7 +791,9 @@ bool ParameterHandler::get_bool (const std::string &entry_string) const { std::string s = get(entry_string); - AssertThrow ((s=="true") || (s=="false") || (s=="yes") || (s=="no"), ExcConversionError(s)); + AssertThrow ((s=="true") || (s=="false") || + (s=="yes") || (s=="no"), + ExcConversionError(s)); if (s=="true" || s=="yes") return true; else @@ -799,6 +802,101 @@ bool ParameterHandler::get_bool (const std::string &entry_string) const +void +ParameterHandler::set (const std::string &entry_string, + const std::string &new_value) +{ + const Section* pd = get_present_defaults_subsection (); + Section* pc = get_present_changed_subsection (); + + // assert that the according entry is + // already declared in the defaults tree + Assert (pd->entries.find (entry_string) != pd->entries.end(), + ExcEntryUndeclared(entry_string)); + + // entry exists; now set it (if the entry + // in pc hasn't existed yet, create it and + // set the pattern to the null pointer + // since it isn't used) + if (pc->entries.find (entry_string) == pc->entries.end()) + pc->entries[entry_string].pattern = 0; + pc->entries[entry_string].value = new_value; +} + + +void +ParameterHandler::set (const std::string &entry_string, + const char *new_value) +{ + // simply forward + set (entry_string, std::string(new_value)); +} + + +void +ParameterHandler::set (const std::string &entry_string, + const double &new_value) +{ +#ifdef HAVE_STD_STRINGSTREAM + std::ostringstream s; +#else + std::ostrstream s; +#endif + s << std::setprecision(16); + s << new_value; +#ifndef HAVE_STD_STRINGSTREAM + s << std::ends; +#endif + + // hand this off to the function that + // actually sets the value as a string + set (entry_string, s.str()); +} + + + +void +ParameterHandler::set (const std::string &entry_string, + const long int &new_value) +{ +#ifdef HAVE_STD_STRINGSTREAM + std::ostringstream s; +#else + std::ostrstream s; +#endif + s << new_value; +#ifndef HAVE_STD_STRINGSTREAM + s << std::ends; +#endif + + // hand this off to the function that + // actually sets the value as a string + set (entry_string, s.str()); +} + + + +void +ParameterHandler::set (const std::string &entry_string, + const bool &new_value) +{ +#ifdef HAVE_STD_STRINGSTREAM + std::ostringstream s; +#else + std::ostrstream s; +#endif + s << new_value; +#ifndef HAVE_STD_STRINGSTREAM + s << std::ends; +#endif + + // hand this off to the function that + // actually sets the value as a string + set (entry_string, s.str()); +} + + + std::ostream & ParameterHandler::print_parameters (std::ostream &out, const OutputStyle style) diff --git a/deal.II/doc/news/changes.html b/deal.II/doc/news/changes.html index 8fc34c6c77..fc91f2879b 100644 --- a/deal.II/doc/news/changes.html +++ b/deal.II/doc/news/changes.html @@ -5,7 +5,7 @@ The deal.II news page - + @@ -150,6 +150,14 @@ inconvenience this causes.

base

    +
  1. + New: There are now functions + ParameterHandler::set that allow to set the value of a + parameter to something different later on. +
    + (Marc Secanell, WB 2006/1/2) +

    +
  2. New: There are new functions Utilities::match_at_string_start and diff --git a/tests/bits/parameter_handler_6.cc b/tests/bits/parameter_handler_6.cc new file mode 100644 index 0000000000..31e0c3d408 --- /dev/null +++ b/tests/bits/parameter_handler_6.cc @@ -0,0 +1,88 @@ +//---------------------------- parameter_handler_3.cc --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2003, 2004, 2005, 2006 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- parameter_handler_3.cc --------------------------- + + +// test ParameterHandler::set(Text) + +#include "../tests.h" +#include +#include +#include +#include + + +int main () +{ + try + { + std::ofstream logfile("parameter_handler_6/output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + // same as parameter_handler_3 + ParameterHandler prm; + prm.enter_subsection ("Testing"); + prm.declare_entry ("string list", + "a", + Patterns::List(Patterns::Selection("a|b|c|d|e|f|g|h")), + "docs 1"); + prm.declare_entry ("int", + "1", + Patterns::Integer()); + prm.declare_entry ("double", + "3.1415926", + Patterns::Double(), + "docs 3"); + prm.leave_subsection (); + + prm.read_input("parameter_handler_3.prm"); + + // now set some of the entries to + // different values + prm.enter_subsection ("Testing"); + prm.set ("string list", "a, c, b"); + prm.set ("int", "5"); + prm.set ("double", "2.71828"); + prm.leave_subsection (); + + // then write + prm.print_parameters (logfile, ParameterHandler::Text); + } + catch (std::exception &exc) + { + std::cerr << std::endl << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Exception on processing: " << std::endl + << exc.what() << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + + return 1; + } + catch (...) + { + std::cerr << std::endl << std::endl + << "----------------------------------------------------" + << std::endl; + std::cerr << "Unknown exception!" << std::endl + << "Aborting!" << std::endl + << "----------------------------------------------------" + << std::endl; + return 1; + }; + + return 0; +} diff --git a/tests/bits/parameter_handler_6/cmp/generic b/tests/bits/parameter_handler_6/cmp/generic new file mode 100644 index 0000000000..db354315f8 --- /dev/null +++ b/tests/bits/parameter_handler_6/cmp/generic @@ -0,0 +1,13 @@ + +# Listing of Parameters +# --------------------- +subsection Testing + # docs 3 + set double = 2.71828 # default: 3.1415926 + set int = 5 # default: 1 + + # docs 1 + set string list = a, c, b # default: a +end + +