void
SurfaceDiffusionResidual<EvalT, Traits>::evaluateFields(
    typename Traits::EvalData workset)
{
  for (int cell(0); cell < workset.numCells; ++cell) {
    for (int node(0); node < numPlaneNodes; ++node) {
      scalarResidual(cell, node) = 0;
      for (int pt = 0; pt < numQPs; ++pt) {
        scalarResidual(cell, node) += refValues(node, pt) *
                                      scalarJump(cell, pt) * thickness *
                                      refArea(cell, pt);
      }
    }
  }
}
  void SurfaceL2ProjectionResidual<EvalT, Traits>::
  evaluateFields(typename Traits::EvalData workset)
  {
    // THESE NEED TO BE REMOVED!!!
    typedef Intrepid::FunctionSpaceTools FST;
    typedef Intrepid::RealSpaceTools<ScalarT> RST;

    ScalarT tau(0);

   // Initialize the residual
    for (int cell(0); cell < workset.numCells; ++cell) {
      for (int node(0); node < numPlaneNodes; ++node) {
        int topNode = node + numPlaneNodes;
        	 projection_residual_(cell, node) = 0;
        	 projection_residual_(cell, topNode) = 0;
      }
    }

    for (int cell(0); cell < workset.numCells; ++cell) {
      for (int node(0); node < numPlaneNodes; ++node) {
        int topNode = node + numPlaneNodes;
        	 for (int pt=0; pt < numQPs; ++pt) {
        		 tau = 0.0;
 
                 for (int dim=0; dim <numDims; ++dim){
                	 tau += detF_(cell,pt)*Cauchy_stress_(cell, pt, dim, dim)/numDims;
                 }

        	  projection_residual_(cell, node) += refValues(node,pt)*
                	       	                            (projected_tau_(cell,pt) -  tau)*
                                                               refArea(cell,pt);

        	 }
        	 projection_residual_(cell, topNode) =  projection_residual_(cell, node);

      }
    }

  }
  void SurfaceTLPoroMassResidual<EvalT, Traits>::
  evaluateFields(typename Traits::EvalData workset)
  {
    typedef Intrepid::FunctionSpaceTools FST;
    typedef Intrepid::RealSpaceTools<ScalarT> RST;

    Albany::MDArray porePressureold = (*workset.stateArrayPtr)[porePressureName];
    Albany::MDArray Jold;
    if (haveMech) {
      Jold = (*workset.stateArrayPtr)[JName];
    }

    ScalarT dt = deltaTime(0);

        // Compute pore fluid flux
       if (haveMech) {
       	// Put back the permeability tensor to the reference configuration
    	    RST::inverse(F_inv, defGrad);
           RST::transpose(F_invT, F_inv);
           FST::scalarMultiplyDataData<ScalarT>(JF_invT, J, F_invT);
           FST::scalarMultiplyDataData<ScalarT>(KJF_invT, kcPermeability, JF_invT);
           FST::tensorMultiplyDataData<ScalarT>(Kref, F_inv, KJF_invT);
           FST::tensorMultiplyDataData<ScalarT> (flux, Kref, scalarGrad); // flux_i = k I_ij p_j
       } else {
           FST::scalarMultiplyDataData<ScalarT> (flux, kcPermeability, scalarGrad); // flux_i = kc p_i
       }

       for (std::size_t cell=0; cell < workset.numCells; ++cell){
             for (std::size_t qp=0; qp < numQPs; ++qp) {
               for (std::size_t dim=0; dim <numDims; ++dim){

                 fluxdt(cell, qp, dim) = -flux(cell,qp,dim)*dt*refArea(cell,qp)*thickness;
               }
             }
       }
          FST::integrate<ScalarT>(poroMassResidual, fluxdt,
          		surface_Grad_BF, Intrepid::COMP_CPP, false); // "true" sums into

    for (std::size_t cell(0); cell < workset.numCells; ++cell) {
      for (std::size_t node(0); node < numPlaneNodes; ++node) {
        // initialize the residual
        int topNode = node + numPlaneNodes;

        for (std::size_t pt=0; pt < numQPs; ++pt) {

          // If there is no diffusion, then the residual defines only on the mid-plane value

          // Local Rate of Change volumetric constraint term
           poroMassResidual(cell, node) -=
                         refValues(node,pt)*(
                         std::log(J(cell,pt)/Jold(cell, pt))*
                         biotCoefficient(cell,pt) +
                          (porePressure(cell, pt) - porePressureold(cell, pt))/ biotModulus(cell,pt)
                          ) *refArea(cell,pt)*thickness;

           poroMassResidual(cell, topNode) -=
        		           refValues(node,pt)*(
        		           std::log(J(cell,pt)/Jold(cell, pt))*
        		           biotCoefficient(cell,pt) +
        		           (porePressure(cell, pt) - porePressureold(cell, pt))/ biotModulus(cell,pt)
        		           ) *refArea(cell,pt)*thickness;



        } // end integrartion point loop
      } //  end plane node loop

      // Stabilization term (if needed)
    } // end cell loop




  }
  void SurfaceTLPoroMassResidual<EvalT, Traits>::
  evaluateFields(typename Traits::EvalData workset)
  {
    typedef Intrepid::FunctionSpaceTools FST;
    typedef Intrepid::RealSpaceTools<ScalarT> RST;

    Albany::MDArray porePressureold = (*workset.stateArrayPtr)[porePressureName];
    Albany::MDArray Jold;
    if (haveMech) {
      Jold = (*workset.stateArrayPtr)[JName];
    }

    ScalarT dt = deltaTime(0);

    // THE INTREPID REALSPACE TOOLS AND FUNCTION SPACE TOOLS NEED TO BE REMOVED!!!
    // Compute pore fluid flux
    if (haveMech) {
      // Put back the permeability tensor to the reference configuration
      RST::inverse(F_inv, defGrad);
      RST::transpose(F_invT, F_inv);
       FST::scalarMultiplyDataData<ScalarT>(JF_invT, J, F_invT);
       FST::scalarMultiplyDataData<ScalarT>(KJF_invT, kcPermeability, JF_invT);
      FST::tensorMultiplyDataData<ScalarT>(Kref, F_inv, KJF_invT);
      FST::tensorMultiplyDataData<ScalarT> (flux, Kref, scalarGrad); // flux_i = k I_ij p_j
    } else {
       FST::scalarMultiplyDataData<ScalarT> (flux, kcPermeability, scalarGrad); // flux_i = kc p_i
    }

    for (int cell(0); cell < workset.numCells; ++cell) {
      for (int node(0); node < numPlaneNodes; ++node) {
        // initialize the residual
        int topNode = node + numPlaneNodes;
        poroMassResidual(cell, topNode)  = 0.0;
        poroMassResidual(cell, node)  = 0.0;
      }
    }
 
    for (int cell(0); cell < workset.numCells; ++cell) {
      for (int node(0); node < numPlaneNodes; ++node) {
        int topNode = node + numPlaneNodes;

        for (int pt=0; pt < numQPs; ++pt) {

          // If there is no diffusion, then the residual defines only on the mid-plane value

          // Local Rate of Change volumetric constraint term
          poroMassResidual(cell, node) -= refValues(node,pt)*
            (std::log(J(cell,pt)/Jold(cell, pt))*
                     biotCoefficient(cell,pt) +
                     (porePressure(cell, pt) - porePressureold(cell, pt))/
                     biotModulus(cell,pt))*refArea(cell,pt);

          poroMassResidual(cell, topNode) -= refValues(node,pt)*
            (std::log(J(cell,pt)/Jold(cell, pt))*
                     biotCoefficient(cell,pt) +
                     (porePressure(cell, pt) - porePressureold(cell, pt))/
                     biotModulus(cell,pt))*refArea(cell,pt);

        } // end integrartion point loop
      } //  end plane node loop
    } // end cell loop


    for (int cell(0); cell < workset.numCells; ++cell) {
      for (int node(0); node < numPlaneNodes; ++node) {

        int topNode = node + numPlaneNodes;

        for (int pt=0; pt < numQPs; ++pt) {
          for (int dim=0; dim <numDims; ++dim){

            poroMassResidual(cell,node) -=  flux(cell, pt, dim)*dt*
              surface_Grad_BF(cell, node, pt, dim)*
              refArea(cell,pt);

            poroMassResidual(cell, topNode) -= flux(cell, pt, dim)*dt*
              surface_Grad_BF(cell, topNode, pt, dim)*
              refArea(cell,pt);
          }
        }
      }
    }

  }