mat2grp.c
/* Convert an input grouped file into an output matrix file. */
Convert an input grouped file into an output matrix file.
Convert an input grouped file into an output matrix file .
int grp2mat (FILE *grpfile, FILE *matfile, int *mtrunc) {
	int msize[3], grphdr[5], bpgvol, rrow, bpg;
	long i, grpslab, matslab;
	cplx *grpvals, *slabvals;
	fread (grphdr, sizeof(int), 5, grpfile);
	bpg = grphdr[4];
	bpgvol = bpg * bpg * bpg;
	msize[0] = grphdr[1] * bpg;
	msize[1] = grphdr[2] * bpg;
	msize[2] = grphdr[3] * bpg;
	if (!mtrunc) mtrunc = msize;
	matslab = mtrunc[0] * mtrunc[1];
	grpslab = grphdr[1] * grphdr[2] * bpgvol;
	grpvals = malloc (grpslab * sizeof(cplx));
	slabvals = malloc (bpg * matslab * sizeof(cplx));
	fwrite (mtrunc, sizeof(int), 3, matfile);
	for (i = 0; i < mtrunc[2]; i += bpg) {
		rrow = MIN(bpg, MAX(0, mtrunc[2] - i));
		fread (grpvals, sizeof(cplx), grpslab, grpfile);
		memset (slabvals, 0, rrow * matslab * sizeof(cplx));
#pragma omp parallel default(shared)
{
		long l, j, k;
		int gidx[3], bidx[3];
#pragma omp for
		for (j = 0; j < grpslab; ++j) {
			l = j / bpgvol;
			k = j % bpgvol;
			GRID(gidx, l, grphdr[1], grphdr[2]);
			gidx[0] *= bpg;
			gidx[1] *= bpg;
			GRID(bidx, k, bpg, bpg);
			bidx[0] += gidx[0];
			bidx[1] += gidx[1];
			if (bidx[0] >= mtrunc[0] || bidx[1] >= mtrunc[1]
					|| bidx[2] >= rrow) continue;
			k = bidx[0] + mtrunc[0] * (bidx[1] + mtrunc[1] * bidx[2]);
			slabvals[k] = grpvals[j];
		}
}
		fwrite (slabvals, sizeof(cplx), rrow * matslab, matfile);
	}
	free (grpvals);
	free (slabvals);
	return mtrunc[0] * mtrunc[1] * mtrunc[2];
}
int grp2mat ( FILE * grpfile , FILE * matfile , int * mtrunc ) { int msize [ 3 ] , grphdr [ 5 ] , bpgvol , rrow , bpg ; long i , grpslab , matslab ; cplx * grpvals , * slabvals ; fread ( grphdr , sizeof ( int ) , 5 , grpfile ) ; bpg = grphdr [ 4 ] ; bpgvol = bpg * bpg * bpg ; msize [ 0 ] = grphdr [ 1 ] * bpg ; msize [ 1 ] = grphdr [ 2 ] * bpg ; msize [ 2 ] = grphdr [ 3 ] * bpg ; if ( ! mtrunc ) mtrunc = msize ; matslab = mtrunc [ 0 ] * mtrunc [ 1 ] ; grpslab = grphdr [ 1 ] * grphdr [ 2 ] * bpgvol ; grpvals = malloc ( grpslab * sizeof ( cplx ) ) ; slabvals = malloc ( bpg * matslab * sizeof ( cplx ) ) ; fwrite ( mtrunc , sizeof ( int ) , 3 , matfile ) ; for ( i = 0 ; i < mtrunc [ 2 ] ; i += bpg ) { rrow = MIN ( bpg , MAX ( 0 , mtrunc [ 2 ] - i ) ) ; fread ( grpvals , sizeof ( cplx ) , grpslab , grpfile ) ; memset ( slabvals , 0 , rrow * matslab * sizeof ( cplx ) ) ; #pragma  omp parallel default(shared) { long l , j , k ; int gidx [ 3 ] , bidx [ 3 ] ; #pragma  omp for for ( j = 0 ; j < grpslab ; ++ j ) { l = j / bpgvol ; k = j % bpgvol ; GRID ( gidx , l , grphdr [ 1 ] , grphdr [ 2 ] ) ; gidx [ 0 ] *= bpg ; gidx [ 1 ] *= bpg ; GRID ( bidx , k , bpg , bpg ) ; bidx [ 0 ] += gidx [ 0 ] ; bidx [ 1 ] += gidx [ 1 ] ; if ( bidx [ 0 ] >= mtrunc [ 0 ] || bidx [ 1 ] >= mtrunc [ 1 ] || bidx [ 2 ] >= rrow ) continue ; k = bidx [ 0 ] + mtrunc [ 0 ] * ( bidx [ 1 ] + mtrunc [ 1 ] * bidx [ 2 ] ) ; slabvals [ k ] = grpvals [ j ] ; } } fwrite ( slabvals , sizeof ( cplx ) , rrow * matslab , matfile ) ; } free ( grpvals ) ; free ( slabvals ) ; return mtrunc [ 0 ] * mtrunc [ 1 ] * mtrunc [ 2 ] ; }
Convert an input grouped file into an output matrix file.
Convert an input grouped file into an output matrix file .
/* Read the group header:
	 * 0 Nx Ny Nz Bpg
	 * (Nx,Ny,Nz) is size of grid of groups.
	 * Bpg is number of basis functions per group per dimension. */ /* The number of elements in a group per dimension. */ /* The total number of elements in a group. */ /* Build the overall matrix size. */ /* Don't truncate the matrix if not desired. */ /* The number of matrix elements in one z-slab. */ /* The number of matrix elements in one z-slab of groups. */ /* Allocate a slab of data for rearranging. */ /* Write the matrix size header. */ /* Loop through slabs of groups to do the conversion. */ /* Track the number of output rows to write for this slab. */ /* Read the slab of groups. */ /* Blank the matrix slab. */ /* Now build each slab. */ /* Pull out the group and element index. */ /* Find the index of the starting basis of the group. */ /* Find the index of the basis in the slab. */ /* Don't write values that should be truncated. */ /* Copy the value into position. */ /* Write the slab of the matrix. */