/*!\file Reduceloadx
 * \brief reduce loads (wring out boundary conditions)
 */

#ifdef HAVE_CONFIG_H
	#include <config.h>
#else
#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
#endif

#include "./Reduceloadx.h"
#include "../../io/io.h"

void	Reduceloadx( Vector* pf, Matrix* Kfs, Vector* y_s,bool flag_ys0){

	/*intermediary*/
	Vector*     y_s0   = NULL;
	Vector*     Kfsy_s = NULL;
	int         Kfsm,Kfsn;
	int         global_m,global_n;
	bool        fromlocalsize = true;
	int         verbose;

	if(VerboseModule()) _pprintLine_("   Dirichlet lifting applied to load vector");

	Kfs->GetSize(&global_m,&global_n);
	if(pf && global_m*global_n){

		/*Some checks in debugging mode*/
		_assert_(y_s);

		/*pf = pf - Kfs * y_s;*/
		Kfs->GetLocalSize(&Kfsm,&Kfsn);
		Kfsy_s=new Vector(Kfsm,fromlocalsize);
		if (flag_ys0){

			/*Create y_s0, full of 0: */
			y_s0=y_s->Duplicate();
			y_s0->Set(0.0);
			y_s0->Assemble();

			Kfs->MatMult(y_s0,Kfsy_s);
		}
		else{
			Kfs->MatMult(y_s,Kfsy_s);
		}

		pf->AXPY(Kfsy_s,-1.);
	}


	/*Free ressources and return*/
	xdelete(&y_s0);
	xdelete(&Kfsy_s);

}
