PDEOptNonlinearPDEConstrainedObj.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 
00043 #include "PDEOptNonlinearPDEConstrainedObj.hpp"
00044 #include "Sundance.hpp"
00045 
00046 using namespace Teuchos;
00047 using namespace Playa;
00048 
00049 namespace Sundance
00050 {
00051 
00052 NonlinearPDEConstrainedObj::NonlinearPDEConstrainedObj(
00053   const Functional& lagrangian,
00054   const Expr& stateVars,
00055   const Expr& stateVarVals,
00056   const Expr& adjointVars,
00057   const Expr& adjointVarVals,
00058   const Expr& designVar,
00059   const Expr& designVarVal,
00060   const NOXSolver& solver,
00061   const LinearSolver<double>& adjSolver,
00062   int verb)
00063   : PDEConstrainedObjBase(lagrangian, tuple(stateVarVals),
00064     tuple(adjointVarVals), designVarVal, verb),
00065     stateProbs_(),
00066     adjointProbs_(),
00067     solver_(solver),
00068     adjSolver_(adjSolver)
00069 {
00070   init(tuple(stateVars), tuple(adjointVars), designVar);
00071 }
00072 
00073 
00074 NonlinearPDEConstrainedObj::NonlinearPDEConstrainedObj(
00075   const Functional& lagrangian,
00076   const Array<Expr>& stateVars,
00077   const Array<Expr>& stateVarVals,
00078   const Array<Expr>& adjointVars,
00079   const Array<Expr>& adjointVarVals,
00080   const Expr& designVar,
00081   const Expr& designVarVal,
00082   const NOXSolver& solver,
00083   const LinearSolver<double>& adjSolver,
00084   int verb)
00085   : PDEConstrainedObjBase(lagrangian, stateVarVals,
00086     adjointVarVals, designVarVal, verb),
00087     stateProbs_(),
00088     adjointProbs_(),
00089     solver_(solver),
00090     adjSolver_(adjSolver)
00091 {
00092   init(stateVars, adjointVars, designVar);
00093 }
00094 
00095 NonlinearPDEConstrainedObj::NonlinearPDEConstrainedObj(
00096   const Functional& lagrangian,
00097   const Expr& stateVars,
00098   const Expr& stateVarVals,
00099   const Expr& adjointVars,
00100   const Expr& adjointVarVals,
00101   const Expr& designVar,
00102   const Expr& designVarVal,
00103   const NOXSolver& solver,
00104   const LinearSolver<double>& adjSolver,
00105   const RCP<IterCallbackBase>& iterCallback,
00106   int verb)
00107   : PDEConstrainedObjBase(lagrangian, tuple(stateVarVals),
00108     tuple(adjointVarVals), designVarVal, iterCallback, verb),
00109     stateProbs_(),
00110     adjointProbs_(),
00111     solver_(solver),
00112     adjSolver_(adjSolver)
00113 {
00114   init(tuple(stateVars), tuple(adjointVars), designVar);
00115 }
00116 
00117 
00118 NonlinearPDEConstrainedObj::NonlinearPDEConstrainedObj(
00119   const Functional& lagrangian,
00120   const Array<Expr>& stateVars,
00121   const Array<Expr>& stateVarVals,
00122   const Array<Expr>& adjointVars,
00123   const Array<Expr>& adjointVarVals,
00124   const Expr& designVar,
00125   const Expr& designVarVal,
00126   const NOXSolver& solver,
00127   const LinearSolver<double>& adjSolver,
00128   const RCP<IterCallbackBase>& iterCallback,
00129   int verb)
00130   : PDEConstrainedObjBase(lagrangian, stateVarVals,
00131     adjointVarVals, designVarVal, iterCallback, verb),
00132     stateProbs_(),
00133     adjointProbs_(),
00134     solver_(solver),
00135     adjSolver_(adjSolver)
00136 {
00137   init(stateVars, adjointVars, designVar);
00138 }
00139 
00140 
00141 void NonlinearPDEConstrainedObj::initEquations(
00142   const Array<Expr>& stateVars,
00143   const Array<Expr>& adjointVars,
00144   const Array<Array<Expr> >& fixedVarsInStateEqns,
00145   const Array<Array<Expr> >& fixedVarsInStateEqnsVals,
00146   const Array<Array<Expr> >& fixedVarsInAdjointEqns,
00147   const Array<Array<Expr> >& fixedVarsInAdjointEqnsVals
00148   )
00149 {
00150   Tabs tab(0);
00151   PLAYA_MSG2(verb(), tab << "setting up nonlinear equations");
00152   
00153   for (int i=0; i<stateVars.size(); i++)
00154   {
00155     Tabs tab1;
00156     PLAYA_MSG3(verb(), tab1 << "setting up state equation #" << i);
00157     Expr fixedVars = new ListExpr(fixedVarsInStateEqns[i]);
00158     Expr fixedVarVals = new ListExpr(fixedVarsInStateEqnsVals[i]);
00159     PLAYA_MSG3(verb(), tab1 << "Fixed vars are: " << fixedVars);
00160     NonlinearProblem stateProb 
00161       = Lagrangian().nonlinearVariationalProb(adjointVars[i], 
00162         adjointVarVals(i),
00163         stateVars[i], stateVarVals(i),
00164         fixedVars, fixedVarVals);
00165                                    
00166     stateProbs_.append(stateProb);
00167   }
00168 
00169   for (int i=0; i<adjointVars.size(); i++)
00170   {
00171     Tabs tab1;
00172     PLAYA_MSG3(verb(), tab1 << "setting up adjoint equation #" << i);
00173     Expr fixedVars = new ListExpr(fixedVarsInAdjointEqns[i]);
00174     Expr fixedVarVals = new ListExpr(fixedVarsInAdjointEqnsVals[i]);
00175     PLAYA_MSG3(verb(), tab1 << "Fixed vars are: " << fixedVars);
00176     LinearProblem adjointProb 
00177       = Lagrangian().linearVariationalProb(stateVars[i], stateVarVals(i),
00178         adjointVars[i],
00179         fixedVars, fixedVarVals);
00180                                    
00181     adjointProbs_.append(adjointProb);
00182   }
00183 
00184   PLAYA_MSG2(verb(), tab << "done setting up nonlinear equations");
00185 }
00186 
00187 
00188 
00189 
00190 void NonlinearPDEConstrainedObj::solveState(const Vector<double>& x) const
00191 {
00192   Tabs tab(0);
00193   PLAYA_MSG2(verb(), tab << "solving state"); 
00194   PLAYA_MSG3(verb(), tab << "|x|=" << x.norm2()); 
00195   PLAYA_MSG5(verb(), tab << "x=" << endl << tab << x.norm2());
00196   setDiscreteFunctionVector(designVarVal(), x);
00197 
00198   /* solve the state equations in order */
00199   for (int i=0; i<stateProbs_.size(); i++)
00200   {
00201     PLAYA_MSG3(verb(), tab << "state eqn=" << i); 
00202     SolverState<double> status 
00203       = stateProbs_[i].solve(solver_);
00204     TEUCHOS_TEST_FOR_EXCEPTION(status.finalState() != SolveConverged,
00205       std::runtime_error,
00206       "state equation could not be solved: status="
00207       << status);
00208   }
00209 
00210   PLAYA_MSG2(verb(), tab << "done state solve"); 
00211   /* do postprocessing */
00212   statePostprocCallback();
00213 }
00214 
00215 
00216 
00217 void NonlinearPDEConstrainedObj
00218 ::solveStateAndAdjoint(const Vector<double>& x) const
00219 {
00220   Tabs tab(0);
00221   PLAYA_MSG2(verb(), tab << "solving state and adjoint"); 
00222   PLAYA_MSG3(verb(), tab << "|x|=" << x.norm2()); 
00223   PLAYA_MSG5(verb(), tab << "x=" << endl << tab << x.norm2()); 
00224 
00225   Tabs tab1;
00226   setDiscreteFunctionVector(designVarVal(), x);
00227 
00228   PLAYA_MSG3(verb(), tab1 << "solving state eqns");
00229   /* solve the state equations in order */
00230   for (int i=0; i<stateProbs_.size(); i++)
00231   {
00232     PLAYA_MSG3(verb(), tab << "state eqn=" << i); 
00233     SolverState<double> status 
00234       = stateProbs_[i].solve(solver_);
00235 
00236     /* if the solve failed, write out the design var and known state
00237      * variables */
00238     bool dumpBadSolve = false;
00239     if (dumpBadSolve && status.finalState() != SolveConverged)
00240     {
00241       FieldWriter w = new VTKWriter("badSolve");
00242       w.addMesh(Lagrangian().mesh());
00243       w.addField("designVar", new ExprFieldWrapper(designVarVal()));
00244       for (int j=0; j<i; j++)
00245       {
00246         Expr tmp = stateVarVals(j).flatten();
00247         for (int k=0; k<tmp.size(); k++)
00248         {
00249           w.addField("stateVar-"+Teuchos::toString(j)+"-"+Teuchos::toString(k),
00250             new ExprFieldWrapper(tmp[k]));
00251         }
00252       }
00253       w.write();
00254     }
00255     TEUCHOS_TEST_FOR_EXCEPTION(status.finalState() != SolveConverged,
00256       std::runtime_error,
00257       "state equation " << i 
00258       << " could not be solved: status="
00259       << status);
00260   }
00261 
00262   PLAYA_MSG3(verb(), tab1 << "done solving state eqns");
00263 
00264   /* do postprocessing */
00265   statePostprocCallback();
00266 
00267   PLAYA_MSG3(verb(), tab1 << "solving adjoint eqns");
00268 
00269   /* solve the adjoint equations in reverse order */
00270   for (int i=adjointProbs_.size()-1; i>=0; i--)
00271   {
00272     PLAYA_MSG3(verb(), tab << "adjoint eqn=" << i); 
00273     SolverState<double> status 
00274       = adjointProbs_[i].solve(adjSolver_, adjointVarVals(i));
00275 
00276     /* if the solve failed, write out the design var and known state
00277      * and adjoint variables */
00278     bool dumpBadSolve = false;
00279     if (dumpBadSolve && status.finalState() != SolveConverged)
00280     {
00281       FieldWriter w = new VTKWriter("badSolve");
00282       w.addMesh(Lagrangian().mesh());
00283       w.addField("designVar", new ExprFieldWrapper(designVarVal()));
00284       for (int j=0; j<stateProbs_.size(); j++)
00285       {
00286         Expr tmp = stateVarVals(j).flatten();
00287         for (int k=0; k<tmp.size(); k++)
00288         {
00289           w.addField("stateVar-"+Teuchos::toString(j)+"-"+Teuchos::toString(k),
00290             new ExprFieldWrapper(tmp[k]));
00291         }
00292       }
00293       for (int j=adjointProbs_.size()-1; j>i; j--)
00294       {
00295         Expr tmp = adjointVarVals(j).flatten();
00296         for (int k=0; k<tmp.size(); k++)
00297         {
00298           w.addField("adjointVar-"+Teuchos::toString(j)+"-"+Teuchos::toString(k),
00299             new ExprFieldWrapper(tmp[k]));
00300         }
00301 
00302       }
00303       w.write();
00304 
00305     }
00306     TEUCHOS_TEST_FOR_EXCEPTION(status.finalState() != SolveConverged,
00307       std::runtime_error,
00308       "adjoint equation " << i 
00309       << " could not be solved: status="
00310       << status.stateDescription());
00311   }
00312   PLAYA_MSG3(verb(), tab1 << "done solving adjoint eqns");
00313   PLAYA_MSG2(verb(), tab1 << "done solving state and adjoint eqns");
00314 }
00315 
00316   
00317 
00318 
00319 }
00320 
00321 
00322 
00323 
00324 
00325 
00326 
00327 
00328 
00329 

Site Contact