SundanceEFDEEvaluator.cpp
Go to the documentation of this file.
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 #include "SundanceEFDEEvaluator.hpp"
00043 #include "SundanceEvalManager.hpp"
00044 #include "PlayaExceptions.hpp"
00045 #include "SundanceSet.hpp"
00046 #include "PlayaTabs.hpp"
00047 #include "SundanceOut.hpp"
00048 
00049 using namespace Sundance;
00050 using namespace Sundance;
00051 
00052 using namespace Sundance;
00053 using namespace Teuchos;
00054 
00055 
00056 EFDEEvaluator::EFDEEvaluator(
00057   const ExplicitFunctionalDerivativeElement* expr, 
00058   const EvalContext& context
00059   )
00060   : UnaryEvaluator<ExplicitFunctionalDerivativeElement>(expr, context),
00061     constValIndexToArgIndexMap_(),
00062     varValIndexToArgIndexMap_()
00063 {
00064 
00065   Tabs tabs;
00066   SUNDANCE_VERB_LOW(tabs << "initializing EFDE evaluator for " 
00067                     << expr->toString());
00068   SUNDANCE_VERB_MEDIUM(tabs << "return sparsity " << std::endl << *(this->sparsity)());
00069 
00070   /* 
00071    * This evaluator requires no calculations. All that is done is to
00072    * map derivatives (md, fd) in the argument's result arrays to 
00073    * derivatives (md) in this expression's result arrays. 
00074    */
00075   
00076 
00077   int vecResultIndex = 0;
00078   int constResultIndex = 0;
00079 
00080   const Deriv& fd = expr->fd();
00081 
00082   for (int i=0; i<this->sparsity()->numDerivs(); i++)
00083     {
00084       const MultipleDeriv& d = this->sparsity()->deriv(i);
00085       const DerivState& myState = this->sparsity()->state(i);
00086 
00087       if (myState==ConstantDeriv)
00088       {
00089         Tabs tab2;
00090         SUNDANCE_VERB_HIGH(tab2 
00091           << "deriv is constant, will be stored at index "
00092           << constResultIndex << " in the const result array");
00093         addConstantIndex(i, constResultIndex++);
00094       }
00095       else
00096       {
00097         Tabs tab2;
00098         SUNDANCE_VERB_HIGH(tab2 
00099           << "deriv is variable, will be stored at index "
00100           << vecResultIndex << " in the var result array");
00101         addVectorIndex(i, vecResultIndex++);
00102       }
00103       
00104       MultipleDeriv dArg = d;
00105       dArg.put(fd);
00106 
00107       int argIndex = argSparsitySuperset()->getIndex(dArg);
00108 
00109       
00110       TEUCHOS_TEST_FOR_EXCEPTION(argIndex==-1, std::runtime_error,
00111         "Derivative " << dArg << " expected in argument but not found");
00112 
00113       
00114       const DerivState& argState = argSparsitySuperset()->state(argIndex);
00115       TEUCHOS_TEST_FOR_EXCEPTION(argState != myState, std::logic_error, 
00116         "mismatched states");
00117 
00118       if (argState==ConstantDeriv)
00119       {
00120         int constArgIndex = argEval()->constantIndexMap().get(argIndex);
00121         constValIndexToArgIndexMap_.append(constArgIndex);
00122       }
00123       else
00124       {
00125         int vectorArgIndex = argEval()->vectorIndexMap().get(argIndex);
00126         varValIndexToArgIndexMap_.append(vectorArgIndex);
00127       }
00128     }
00129   
00130   SUNDANCE_VERB_HIGH(tabs 
00131     << " constant index map " 
00132     << constValIndexToArgIndexMap_ << std::endl 
00133     << " vector index map " 
00134     << varValIndexToArgIndexMap_
00135     );
00136 }
00137 
00138 
00139 
00140 void EFDEEvaluator::internalEval(const EvalManager& mgr,
00141   Array<double>& constantResults,
00142   Array<RCP<EvalVector> >& vectorResults) const 
00143 {
00144   TimeMonitor timer(efdeEvalTimer());
00145   Tabs tabs(0);
00146 
00147   SUNDANCE_MSG1(mgr.verb(), tabs << "EFDEEvaluator::eval() expr=" 
00148     << expr()->toString());
00149 
00150   SUNDANCE_MSG3(mgr.verb(), tabs << "sparsity = " << std::endl 
00151     << *(this->sparsity)());
00152 
00153   constantResults.resize(constValIndexToArgIndexMap_.size());
00154   vectorResults.resize(varValIndexToArgIndexMap_.size());
00155 
00156   /* evaluate the argument */
00157   Array<RCP<EvalVector> > argVectorResults;
00158   Array<double> argConstantResults;
00159 
00160   evalOperand(mgr, argConstantResults, argVectorResults);
00161 
00162 
00163   if (mgr.verb() > 2)
00164     {
00165       Tabs tab1;
00166       Out::os() << tab1 << "EFDE operand results" << std::endl;
00167       mgr.showResults(Out::os(), argSparsitySuperset(), argVectorResults,
00168           argConstantResults);
00169     }
00170 
00171 
00172   for (int i=0; i<constantResults.size(); i++)
00173   {
00174     constantResults[i] = argConstantResults[constValIndexToArgIndexMap_[i]];
00175   }
00176 
00177   
00178   for (int i=0; i<vectorResults.size(); i++)
00179   {
00180     vectorResults[i] = mgr.popVector();
00181     const RCP<EvalVector>& v = argVectorResults[varValIndexToArgIndexMap_[i]];
00182     vectorResults[i]->setTo_V(v.get());
00183   }
00184 
00185   
00186   
00187 
00188   if (mgr.verb() > 2)
00189   {
00190     Tabs tab1;
00191     Out::os() << tab1 << "EFDE results " << std::endl;
00192     mgr.showResults(Out::os(), this->sparsity(), vectorResults,
00193       constantResults);
00194   }
00195 }
00196 

Site Contact