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

Site Contact