00001 /* @HEADER@ */ 00002 // ************************************************************************ 00003 // 00004 // Sundance 00005 // Copyright 2011 Sandia Corporation 00006 // 00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, 00008 // the U.S. Government retains certain rights in this software. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu) 00038 // 00039 00040 /* @HEADER@ */ 00041 00042 #ifndef SUNDANCE_EVALCONTEXT_H 00043 #define SUNDANCE_EVALCONTEXT_H 00044 00045 00046 #include "SundanceDefs.hpp" 00047 #include "SundanceRegionQuadCombo.hpp" 00048 #include "SundanceSet.hpp" 00049 #include "Teuchos_Utils.hpp" 00050 #include <algorithm> 00051 00052 00053 namespace Sundance 00054 { 00055 using namespace Teuchos; 00056 using namespace Sundance; 00057 using Sundance::Set; 00058 00059 /** 00060 * Different contexts might require the same expression to be 00061 * evaluated to different orders of functional differentiation; for 00062 * example, in setting up a linear system, second-order derivatives 00063 * are required, but in evaluating a functional only zeroth derivs 00064 * are required. 00065 * An EvaluationContext is used as a key to associate an evaluator and 00066 * its corresponding set of 00067 * functional derivatives with a context. 00068 * 00069 * They key consists of three parts: first, an integer identifier 00070 * indicating the caller, e.g., an assembler or functional evaluator, 00071 * second, a set indicating which orders of 00072 * differentiation are required by the top level caller, and third, 00073 a region-quadrature combination. 00074 */ 00075 class EvalContext 00076 { 00077 public: 00078 /** Empty ctor */ 00079 EvalContext() : setupVerbosity_(0), evalSetupVerbosity_(0), 00080 maxDiffOrder_(0), 00081 data_() {;} 00082 00083 /** Construct with a region-quadrature combination and 00084 * an identifier of the construcing context. */ 00085 EvalContext(const RegionQuadCombo& rqc, 00086 const Set<int>& needsDiffOrder, 00087 int contextID) 00088 : setupVerbosity_(0), evalSetupVerbosity_(0), 00089 maxDiffOrder_(*std::max_element(needsDiffOrder.begin(), needsDiffOrder.end())), 00090 data_(rcp(new OrderedTriple<Set<int>, int, RegionQuadCombo>(needsDiffOrder, contextID, rqc))) 00091 {} 00092 00093 /** Set the verbosity level to be used during preprocessing 00094 * of expressions in this context */ 00095 void setSetupVerbosity(int v) const {setupVerbosity_ = v;} 00096 00097 /** Return the verbosity level to be used during preprocessing 00098 * of expressions in this context */ 00099 int setupVerbosity() const {return setupVerbosity_;} 00100 00101 /** Set the verbosity level to be used during setup of evaluators 00102 * for expressions in this context */ 00103 void setEvalSetupVerbosity(int v) const {evalSetupVerbosity_ = v;} 00104 00105 /** Get the verbosity level to be used during setup of evaluators 00106 * for expressions in this context */ 00107 int evalSetupVerbosity() const {return evalSetupVerbosity_;} 00108 00109 /** Comparison operator for use in maps */ 00110 bool operator<(const EvalContext& other) const 00111 {return *data_ < *other.data_;} 00112 00113 /** Write to a std::string */ 00114 std::string toString() const 00115 {return "EvalContext[diffOrder=" 00116 + Teuchos::toString(data_->a()) 00117 + ", id=" 00118 + Teuchos::toString(data_->b()) 00119 + ", " + data_->c().toString() + "]";} 00120 00121 /** Write a short description to a std::string */ 00122 std::string brief() const 00123 {return "EvalContext[diffOrder=" 00124 + Teuchos::toString(data_->a()) 00125 + ", id=" 00126 + Teuchos::toString(data_->b()) 00127 + "]";} 00128 00129 /** */ 00130 int topLevelDiffOrder() const {return maxDiffOrder_;} 00131 00132 /** Indicate whether or not a given order of differentiation 00133 * is needed in this context */ 00134 bool needsDerivOrder(int order) const {return data_->a().contains(order);} 00135 00136 00137 /** Return a unique context ID */ 00138 static int nextID() {static int rtn=0; return rtn++;} 00139 private: 00140 mutable int setupVerbosity_; 00141 mutable int evalSetupVerbosity_; 00142 int maxDiffOrder_; 00143 RCP<OrderedTriple<Set<int>, int, RegionQuadCombo> > data_; 00144 }; 00145 00146 } 00147 00148 00149 namespace std 00150 { 00151 /** \relates Sundance::EvalContext */ 00152 inline ostream& operator<<(std::ostream& os, 00153 const Sundance::EvalContext& c) 00154 { 00155 os << c.toString(); 00156 return os; 00157 } 00158 } 00159 00160 namespace Teuchos 00161 { 00162 /** \relates Sundance::EvalContext */ 00163 inline std::string toString(const Sundance::EvalContext& h) 00164 {return h.toString();} 00165 00166 } 00167 00168 00169 #endif