void 
twoD_diffusion_problem<Scalar,MeshScalar,BasisScalar,LocalOrdinal,GlobalOrdinal,
		       Node>::
computeResponse(const Tpetra_Vector& x,
		const Tpetra_Vector& p,
		Tpetra_Vector& g)
{
  // g = average of x
  Teuchos::ArrayRCP<Scalar> g_view = g.get1dViewNonConst();
  x.meanValue(g_view());
  g_view[0] *= Scalar(x.getGlobalLength()) / Scalar(mesh.size());
}
void
Albany::SolutionAverageResponseFunction::
evaluateResponseT(const double current_time,
		 const Tpetra_Vector* xdotT,
		 const Tpetra_Vector* xdotdotT,
		 const Tpetra_Vector& xT,
		 const Teuchos::Array<ParamVec>& p,
		 Tpetra_Vector& gT)
{
  ST mean = xT.meanValue();
  Teuchos::ArrayRCP<ST> gT_nonconstView = gT.get1dViewNonConst();
  gT_nonconstView[0] = mean; 
}
void
Albany::SolutionAverageResponseFunction::
evaluateTangentT(const double alpha, 
		const double beta,
		const double omega,
		const double current_time,
		bool sum_derivs,
		const Tpetra_Vector* xdotT,
		const Tpetra_Vector* xdotdotT,
		const Tpetra_Vector& xT,
		const Teuchos::Array<ParamVec>& p,
		ParamVec* deriv_p,
		const Tpetra_MultiVector* VxdotT,
		const Tpetra_MultiVector* VxdotdotT,
		const Tpetra_MultiVector* VxT,
		const Tpetra_MultiVector* VpT,
		Tpetra_Vector* gT,
		Tpetra_MultiVector* gxT,
		Tpetra_MultiVector* gpT)
{
  // Evaluate response g
  if (gT != NULL) {
    ST mean = xT.meanValue();
    Teuchos::ArrayRCP<ST> gT_nonconstView = gT->get1dViewNonConst();
    gT_nonconstView[0] = mean; 
  }

  // Evaluate tangent of g = dg/dx*Vx + dg/dxdot*Vxdot + dg/dp*Vp
  // If Vx == NULL, Vx is the identity
  if (gxT != NULL) {
    Teuchos::ArrayRCP<ST> gxT_nonconstView;
    if (VxT != NULL) {
       Teuchos::Array<ST> means; 
       means.resize(VxT->getNumVectors());
       VxT->meanValue(means());  
      for (int j=0; j<VxT->getNumVectors(); j++) {  
        gxT_nonconstView = gxT->getDataNonConst(j); 
        gxT_nonconstView[0] = means[j];  
      }
    }
    else {
      gxT->putScalar(1.0/xT.getGlobalLength());
    }
    gxT->scale(alpha);
  }
  
  if (gpT != NULL)
    gpT->putScalar(0.0);
}
void
Albany::SolutionAverageResponseFunction::
evaluateGradientT(const double current_time,
		 const Tpetra_Vector* xdotT,
		 const Tpetra_Vector* xdotdotT,
		 const Tpetra_Vector& xT,
		 const Teuchos::Array<ParamVec>& p,
		 ParamVec* deriv_p,
		 Tpetra_Vector* gT,
		 Tpetra_MultiVector* dg_dxT,
		 Tpetra_MultiVector* dg_dxdotT,
		 Tpetra_MultiVector* dg_dxdotdotT,
		 Tpetra_MultiVector* dg_dpT)
{

  // Evaluate response g
  if (gT != NULL) {
    ST mean = xT.meanValue();
    Teuchos::ArrayRCP<ST> gT_nonconstView = gT->get1dViewNonConst();
    gT_nonconstView[0] = mean;
  }

  // Evaluate dg/dx
  if (dg_dxT != NULL)
    dg_dxT->putScalar(1.0 / xT.getGlobalLength());

  // Evaluate dg/dxdot
  if (dg_dxdotT != NULL)
    dg_dxdotT->putScalar(0.0);
  if (dg_dxdotdotT != NULL)
    dg_dxdotdotT->putScalar(0.0);

  // Evaluate dg/dp
  if (dg_dpT != NULL)
    dg_dpT->putScalar(0.0);
}