
/*
	OutputDiagnostic.c: output model results for diagnostic solution.
*/
#undef __FUNCT__ 
#define __FUNCT__ "OutputDiagnostic"

#include "../toolkits/toolkits.h"
#include "../shared/shared.h"
#include "../io/io.h"
#include "../objects/objects.h"

void OutputDiagnostic(Vec u_g,Vec partition,char* filename,NodeSets* nodesets,char* analysis_type){

	/* error handling: */
	int i;

	extern int my_rank;
	
	/* output: */
	FILE* fid=NULL;

	/* standard output: */
	double* serial_partition=NULL;
	int     serial_partition_rows;
	int    analysis_size;

	double* serial_u_g=NULL;
	int     serial_u_g_rows;
	int     dummy=1;
	int     gsize;

	/*serialize outputs: */
	VecShift(partition,1.0); //matlab indexing
	VecGetSize(partition,&serial_partition_rows);
	VecToMPISerial(&serial_partition,partition);

	VecGetSize(u_g,&serial_u_g_rows);
	VecToMPISerial(&serial_u_g,u_g);

	/* Open output file to write raw binary data: */
	if(my_rank==0){
		fid=fopen(filename,"wb");
		if(fid==NULL)throw ErrorException(__FUNCT__,exprintf("%s%s%s","could not open file ",filename," for binary writing."));

		/*Write solution type: */
		WriteDataToDisk(analysis_type,NULL,NULL,"String",fid);

		/*Write uset.gsize: */
		gsize=nodesets->GetGSize();
		WriteDataToDisk(&gsize,NULL,NULL,"Integer",fid);

		/*Write partition: */
		WriteDataToDisk(serial_partition,&serial_partition_rows,&dummy,"Mat",fid);
		
		/*Write solution to disk: */
		WriteDataToDisk(serial_u_g,&serial_u_g_rows,&dummy,"Mat",fid);
	
		/*Close file: */
		if(fclose(fid)!=0)throw ErrorException(__FUNCT__,exprintf("%s%s","could not close file ",filename));
	}

}	
