/*!\file MinVzx
 */

#include "./MinVzx.h"

#include "../../shared/shared.h"
#include "../../include/include.h"
#include "../../toolkits/toolkits.h"
#include "../../EnumDefinitions/EnumDefinitions.h"

void MinVzx( double* pminvz, Elements* elements,Nodes* nodes, Vertices* vertices, Loads* loads, Materials* materials,Parameters* parameters,bool process_units){
	
	int i;
	double minvz;
	double node_minvz;
	double element_minvz;

	/*Go through elements, and request velocity: */
	for(i=0;i<elements->Size();i++){
		Element* element=(Element*)elements->GetObjectByOffset(i);
		element->MinVz(&element_minvz,process_units); //go pick up the minimum velocity in the inputs
	
		if(i==0)minvz=element_minvz; //initialize minvz
		else{
			if(element_minvz<minvz)minvz=element_minvz;
		}
	}
	/*A safeguard in case: */
	if(elements->Size()==0){
		minvz=INFINITY;
	}

	/*Figure out minimum across the cluster: */
	#ifdef _HAVE_MPI_
	MPI_Reduce (&minvz,&node_minvz,1,MPI_DOUBLE,MPI_MIN,0,MPI_COMM_WORLD );
	MPI_Bcast(&node_minvz,1,MPI_DOUBLE,0,MPI_COMM_WORLD);   
	minvz=node_minvz;
	#endif

	/*Assign output pointers:*/
	*pminvz=minvz;
}
