Sacado Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Sacado_ParameterFamilyBaseImp.hpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Sacado Package
5// Copyright (2006) Sandia Corporation
6//
7// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8// the U.S. Government retains certain rights in this software.
9//
10// This library is free software; you can redistribute it and/or modify
11// it under the terms of the GNU Lesser General Public License as
12// published by the Free Software Foundation; either version 2.1 of the
13// License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23// USA
24// Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25// (etphipp@sandia.gov).
26//
27// ***********************************************************************
28// @HEADER
29
30#include "Teuchos_Assert.hpp"
31
32template <typename EntryBase, typename EntryType>
34ParameterFamilyBase(const std::string& name_,
35 bool supports_ad_,
36 bool supports_analytic_) :
37 family(),
38 name(name_),
39 supports_ad(supports_ad_),
40 supports_analytic(supports_analytic_)
41{
42}
43
44template <typename EntryBase, typename EntryType>
49
50template <typename EntryBase, typename EntryType>
51std::string
58template <typename EntryBase, typename EntryType>
59bool
61supportsAD() const
62{
63 return supports_ad;
64}
65
66template <typename EntryBase, typename EntryType>
67bool
69supportsAnalytic() const
71 return supports_analytic;
72}
73
74template <typename EntryBase, typename EntryType>
75template <class EvalType>
76bool
78hasType() const
79{
81 // Convert typename EvalType to string
82 std::string evalTypeString = getTypeName<EvalType>();
83
84 // Find entry corresponding to this EvalType
85 const_iterator it = family.find(evalTypeString);
86 if (it == family.end())
87 return false;
88
89 return true;
90}
92template <typename EntryBase, typename EntryType>
93template <class EvalType>
94bool
96addEntry(const Teuchos::RCP< typename Sacado::mpl::apply<EntryType,EvalType>::type >& entry,
97 const bool allow_overwrite)
99 // Get string representation of EvalType
100 std::string evalTypeString = getTypeName<EvalType>();
101
102 // Determine if entry already exists for parameter and type
103 iterator it = family.find(evalTypeString);
104
105 // If it does not, add it
106 if (it == family.end()) {
107 family.insert(std::pair<std::string,
108 Teuchos::RCP<EntryBase> >(evalTypeString, entry));
109 }
110 else if (allow_overwrite) {
111 (*it).second = entry;
113 else {
114 return false;
115 }
116
117 return true;
118}
119
120template <typename EntryBase, typename EntryType>
121template <class EvalType>
122Teuchos::RCP< typename Sacado::mpl::apply<EntryType,EvalType>::type >
124getEntry() {
125
126 // Convert typename EvalType to string
127 std::string evalTypeString = getTypeName<EvalType>();
128
129 // Find entry corresponding to this EvalType
130 iterator it = family.find(evalTypeString);
131 TEUCHOS_TEST_FOR_EXCEPTION(it == family.end(),
132 std::logic_error,
133 std::string("Sacado::ParameterFamilyBase::getEntry(): ")
134 + "Parameter entry " + name
135 + " does not have a parameter of type"
136 + evalTypeString);
137
138 // Cast entry to LOCA::Parameter::Entry<EvalType>
139 Teuchos::RCP< typename Sacado::mpl::apply<EntryType,EvalType>::type > entry = Teuchos::rcp_dynamic_cast< typename Sacado::mpl::apply<EntryType,EvalType>::type >((*it).second);
140 TEUCHOS_TEST_FOR_EXCEPTION(entry == Teuchos::null,
141 std::logic_error,
142 std::string("Sacado::ParameterFamilyBase::getEntry(): ")
143 + "Parameter entry " + name
144 + " of type" + evalTypeString
145 + " has incorrect entry type");
146
147 return entry;
148}
149
150template <typename EntryBase, typename EntryType>
151template <class EvalType>
152Teuchos::RCP< const typename Sacado::mpl::apply<EntryType,EvalType>::type >
154getEntry() const {
155
156 // Convert typename EvalType to string
157 std::string evalTypeString = getTypeName<EvalType>();
158
159 // Find entry corresponding to this EvalType
160 const_iterator it = family.find(evalTypeString);
161 TEUCHOS_TEST_FOR_EXCEPTION(it == family.end(),
162 std::logic_error,
163 std::string("Sacado::ParameterFamilyBase::getEntry(): ")
164 + "Parameter entry " + name
165 + " does not have a parameter of type"
166 + evalTypeString);
167
168 // Cast entry to LOCA::Parameter::Entry<EvalType>
169 Teuchos::RCP< const typename Sacado::mpl::apply<EntryType,EvalType>::type > entry = Teuchos::rcp_dynamic_cast< const typename Sacado::mpl::apply<EntryType,EvalType>::type >((*it).second);
170 TEUCHOS_TEST_FOR_EXCEPTION(entry == Teuchos::null,
171 std::logic_error,
172 std::string("Sacado::ParameterFamilyBase::getEntry(): ")
173 + "Parameter entry " + name
174 + " of type" + evalTypeString
175 + " has incorrect entry type");
176
177 return entry;
178}
179
180template <typename EntryBase, typename EntryType>
181void
183print(std::ostream& os, bool print_values) const
184{
185 os << "\t" << name << ": Supports AD = " << supports_ad
186 << ", Supports_Analytic = " << supports_analytic << std::endl;
187 if (print_values) {
188 for (const_iterator it = family.begin(); it != family.end(); it++) {
189 os << "\t\t" << (*it).first << " = ";
190 (*it).second->print(os);
191 os << std::endl;
192 }
193 }
194
195}
196
197template <typename EntryBase, typename EntryType>
198template <class EvalType>
199std::string
201getTypeName() const {
202 return typeid(EvalType).name();
203}
std::string getTypeName() const
Returns a string representation of type EntryType.
Teuchos::RCP< typename Sacado::mpl::apply< EntryType, EvalType >::type > getEntry()
Gets the entry corresponding to type EvalType.
bool hasType() const
Determine if family has an entry for the given type EvalType.
bool supportsAD() const
Indicates whether parameter supports AD derivatives.
std::string getName() const
Get the name of the family.
ParameterFamilyBase(const std::string &name, bool supports_ad, bool supports_analytic)
Constructor.
void print(std::ostream &os, bool print_values=false) const
Print the family.
bool addEntry(const Teuchos::RCP< typename Sacado::mpl::apply< EntryType, EvalType >::type > &entry, const bool allow_overwrite=false)
Add a new parameter using custom entry.
bool supportsAnalytic() const
Indicates whether parameter supports analytic derivatives.
std::string name_
Definition gtest.cc:2817
F::template apply< A1, A2, A3, A4, A5 >::type type