コード例 #1
0
/**
 * \brief Write an xml node "field".
 * \param item The item instance to write.
 * \param f The field to save.
 * \param os The stream in which we write.
 */
void bf::xml::item_instance_field_node::write
( const item_instance& item, const type_field& f, std::ostream& os ) const
{
  os << "<field name='" << f.get_name() << "'>\n";
  save_field( item, f, os );
  os << "</field>\n";
} // item_instance_field_node::write()
コード例 #2
0
ファイル: profile.c プロジェクト: arcean/ohm-plugins-misc
static int profile_save_state(OhmFact *fact)
{
    FILE *fp;
    GSList *l;
    GQuark  q;
    const gchar *key;
    GValue *value;
    int err;
    
    if ((fp = fopen(PROFILE_SAVE_PATH, "w")) == NULL)
        return errno;

    for (l = ohm_fact_get_fields(fact); l != NULL; l = l->next) {
        q = (GQuark)GPOINTER_TO_INT(l->data);
        key = g_quark_to_string(q);
        value = ohm_fact_get(fact, key);

        if ((err = save_field(fp, key, value)) != 0) {
            fclose(fp);
            unlink(PROFILE_SAVE_PATH);
            return err;
        }
    }
    
    fflush(fp);
    /* fdatasync(fileno(fp)); */
    fclose(fp);

    OHM_INFO("Profile state saved.");
    return 0;
}
コード例 #3
0
int main(int argc, char **argv)
{

	//double tend = 1E2, speed = 1.;
	double tend = 1E-1, speed = 1.;
	char *init_type = "mixed2";
	double *roots, *weights, *ll, *dl, xmin, xmax, lxmin, lxmax,
		deltax, jac, xr, xl, cfl, dt, rtime, min_dx;
	int ii, jj, kk, ee, idx, eres;
	long nstep;
	double *dx, *mesh;
	double *smat, *xx, *qq, *qtemp, *k1, *k2, *k3, *k4, *minv_vec, *mmat, *dv,
		*mf, *ib, *df, *fstar;

	MPI_Init(&argc, &argv);
	MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	para_range(0, tne, nprocs, rank, &ista, &iend);
	ne = iend - ista;

	// initialize
	// fortran index structure array[ii,jj,ee] where size(array) = (np, np, ne)
	// c 1d index structure array = [ee*np*np + jj*np + ii]
	roots = (double *)malloc(np * sizeof(double));
	weights = (double *)malloc(np * sizeof(double));
	ll = (double *)malloc(np * sizeof(double));
	dl = (double *)malloc(np * sizeof(double));
	dx = (double *)malloc(ne * sizeof(double));
	mesh = (double *)malloc((ne + 1) * sizeof(double));

	smat = (double *)malloc(np * np * sizeof(double));		// [jj np, ii np]
	xx = (double *)malloc(ne * np * sizeof(double));		// [ee ne, ii np]
	qq = (double *)malloc(ne * np * sizeof(double));		// [ee ne, ii np]
	qtemp = (double *)malloc(ne * np * sizeof(double));		// [ee ne, ii np]
	k1 = (double *)malloc(ne * np * sizeof(double));		// [ee ne, ii np]
	k2 = (double *)malloc(ne * np * sizeof(double));		// [ee ne, ii np]
	k3 = (double *)malloc(ne * np * sizeof(double));		// [ee ne, ii np]
	k4 = (double *)malloc(ne * np * sizeof(double));		// [ee ne, ii np]
	minv_vec = (double *)malloc(ne * np * sizeof(double));  // [ee ne, ii np]
	mmat = (double *)malloc(ne * np * np * sizeof(double)); // [ee ne, jj np, ii np]
	dv = (double *)malloc(ne * np * np * sizeof(double));   // [ee ne, jj np, ii np]
	mf = (double *)malloc(2 * np * sizeof(double));			// [jj 2,  ii np]
	ib = (double *)malloc(2 * np * sizeof(double));			// [jj 2,  ii np]
	fstar = (double *)malloc(2 * ne * sizeof(double));		// [jj 2,  ii ne]
	df = (double *)malloc(ne * 2 * np * sizeof(double));	// [ee ne, jj 2, ii np]

	for (ii = 0; ii < np; ++ii)
	{
		roots[ii] = 0;
		weights[ii] = 0;
		ll[ii] = 0;
		dl[ii] = 0;
	}
	for (ii = 0; ii < ne; ++ii)
	{
		dx[ii] = 0;
		mesh[ii] = 0;
	}
	mesh[ne] = 0;

	for (ii = 0; ii < np * np; ++ii)
	{
		smat[ii] = 0;
	}
	for (ii = 0; ii < ne * np; ++ii)
	{
		xx[ii] = 0;
		qq[ii] = 0;
		k1[ii] = 0;
		k2[ii] = 0;
		k3[ii] = 0;
		k4[ii] = 0;
		minv_vec[ii] = 0;
	}
	for (ii = 0; ii < ne * np * np; ++ii)
	{
		mmat[ii] = 0;
		dv[ii] = 0;
	}
	for (ii = 0; ii < np * 2; ++ii)
	{
		mf[ii] = 0;
		ib[ii] = 0;
	}
	for (ii = 0; ii < ne * 2; ++ii)
	{
		fstar[ii] = 0;
	}
	for (ii = 0; ii < ne * 2 * np; ++ii)
	{
		df[ii] = 0;
	}

	// mesh setup
	xmin = 0.;
	xmax = 10.;
	deltax = (xmax-xmin)/(double)tne;
	/**
	 * lxim, lxmax를 이용하여 각 구간의 mesh[ee]를 구한다
	 * ne의 크기가 tne / process의 개수이기 때문에, 
	 * 각 구간에 맞는 mesh[ee]를 구해야 한다.
	 * 그리고 mesh[ee]를 이용하여 각 변수들을 초기화 한다.
	 */
	lxmin = xmin + (ista)*deltax;
	lxmax = xmin + (iend)*deltax;
	/**
	 * mesh[ne]은 마지막 원소가 아니라는점에 유의한다.
	 */ 
	mesh[ne] = lxmax; 
	for(ee=0;ee<ne;++ee){
		mesh[ee] = lxmin+ee*deltax;
	}

	// gauss lobatto quadrature point, weight setup
	gausslobatto_quadrature(np, roots, weights);

	// coordinates and element size
	min_dx = xmax - xmin; // initial guess
	for (ee = 0; ee < ne; ee++)
	{
		xl = mesh[ee];
		xr = mesh[ee + 1];
		dx[ee] = xr - xl; // size of each element
		if (dx[ee] < min_dx)
		{
			min_dx = dx[ee]; // finding minimum dx
		}
		for (ii = 0; ii < np; ii++)
		{
			idx = ee * np + ii;
			xx[idx] = xl + 0.5 * (1 + roots[ii]) * dx[ee];
		}
	}

	// mass matrix
	for (ii = 0; ii < ne * np * np; ii++)
	{
		mmat[ii] = 0;
	}
	for (ee = 0; ee < ne; ee++)
	{
		jac = fabs(dx[ee]) / 2;
		for (kk = 0; kk < np; kk++)
		{
			lagrange(roots[kk], ll, roots);
			for (jj = 0; jj < np; jj++)
			{
				for (ii = 0; ii < np; ii++)
				{
					idx = ee * np * np + jj * np + ii;
					// mass matrix mmat[ne][np][np] in 1d index representation
					mmat[idx] += jac * weights[kk] * ll[ii] * ll[jj];
				}
			}
		}
	}

	// stiffness matrix
	for (ii = 0; ii < np * np; ii++)
	{
		smat[ii] = 0;
	}
	for (kk = 0; kk < np; kk++)
	{
		lagrange(roots[kk], ll, roots);
		lagrange_deriv(roots[kk], dl, roots);
		for (jj = 0; jj < np; jj++)
		{
			for (ii = 0; ii < np; ii++)
			{
				idx = jj * np + ii;
				// stiffness matrix smat[np][np] in 1d index representation
				smat[idx] += weights[kk] * ll[jj] * dl[ii];
			}
		}
	}

	// face integration
	for (ii = 0; ii < np * 2; ii++)
	{
		mf[ii] = 0;
	}
	lagrange(-1, mf, roots);	 // mf[ii] for(ii=0, ii<np,ii++) represents element left face integration
	lagrange(1, mf + np, roots); // mf[ii] for ii=np, ii<2*np, ii++) reresents element right face integration

	// boundary interpolation
	for (ii = 0; ii < np * 2; ii++)
	{
		ib[ii] = 0;
	}
	lagrange(-1, ib, roots);	 // element left edge interpolation
	lagrange(1, ib + np, roots); // element right edge interpolation

	// divergence operators
	for (ii = 0; ii < ne * np * np; ii++)
	{
		dv[ii] = 0;
	}
	for (ii = 0; ii < ne * np * 2; ii++)
	{
		dv[ii] = 0;
	}
	for (ee = 0; ee < ne; ee++)
	{
		for (jj = 0; jj < np; jj++)
		{
			// it turn out that mmat is diagonal. i.e., ii != jj, mmat[ee][jj][ii] = 0
			// the inverse of mmat is just the inverse of the diagonal components
			// here, we are extracting the inverse diagonal components only
			minv_vec[ee * np + jj] = 1. / mmat[ee * np * np + jj * np + jj];
		}
		for (jj = 0; jj < np; jj++)
		{
			for (ii = 0; ii < np; ii++)
			{
				dv[ee * np * np + jj * np + ii] = minv_vec[ee * np + ii] * smat[jj * np + ii];
			}
		}
		for (jj = 0; jj < 2; jj++)
		{
			for (ii = 0; ii < np; ii++)
			{
				df[ee * np * 2 + jj * np + ii] = minv_vec[ee * np + ii] * mf[jj * np + ii];
			}
		}
	}

	// initialize qq field
	initialize(qq, xx, xmax, xmin, init_type);
	cfl = 1. / (np * np);
	dt = cfl * min_dx / fabs(speed);
	rtime = 0.;
	nstep = 0;

	printf("Start Time Integration\n");

	// Runge-Kutta 4th order Time integration loop

	t_sta = clock();

	while (rtime < tend)
	{
		dt = fmin(dt, tend - rtime);
		rhs(qq, k1, dv, df, ib, speed);
		for (ii = 0; ii < ne * np; ii++)
			qtemp[ii] = qq[ii] + 0.5 * dt * k1[ii];

		rhs(qtemp, k2, dv, df, ib, speed);
		for (ii = 0; ii < ne * np; ii++)
			qtemp[ii] = qq[ii] + 0.5 * dt * k2[ii];
		rhs(qtemp, k3, dv, df, ib, speed);


		for (ii = 0; ii < ne * np; ii++)
			qtemp[ii] = qq[ii] + dt * k3[ii];
		rhs(qtemp, k4, dv, df, ib, speed);

		for (ii = 0; ii < ne * np; ii++)
			qq[ii] += 1. / 6. * dt * (k1[ii] + 2 * k2[ii] + 2 * k3[ii] + k4[ii]);

		rtime += dt;
		nstep += 1;
		if (nstep % 10000 == 0 && rank == 0)
			printf("nstep = %10ld, %5.1f%% complete\n", nstep, rtime / tend * 100);
	}

	// timeloop ends here;

	if (rank != 0)
	{
		int nne = iend - ista;
		MPI_Isend(&nne, 1, MPI_INT, 0, 11, MPI_COMM_WORLD, &ser1);
		MPI_Isend(xx, ne * np, MPI_DOUBLE, 0, 22, MPI_COMM_WORLD, &ser2);
		MPI_Isend(qq, ne * np, MPI_DOUBLE, 0, 33, MPI_COMM_WORLD, &ser3);
		MPI_Wait(&ser1, &st);
		MPI_Wait(&ser2, &st);
		MPI_Wait(&ser3, &st);
	}

	double *bufx;
	double *bufq;
	int *istart;
	int *idisp;

	if (rank == 0)
	{

		printf("Integration complete\n");

		if (tne > 200)
		{
			eres = 2;
		}
		else if (tne > 60)
		{
			eres = 3;
		}
		else if (tne > 30)
		{
			eres = 6;
		}
		else
		{
			eres = 10;
		}

		// final report
		printf("-----------------------------------------------\n");
		printf("code type   : c serial\n");
		printf("Final time  : %13.5e\n", rtime);
		printf("CFL         : %13.5e\n", cfl);
		printf("DOF         : %13d\n", tne * np);
		printf("No. of Elem : %13d\n", tne);
		printf("Order       : %13d\n", np);
		printf("eres        : %13d\n", eres);
		printf("time steps  : %13ld\n", nstep);
		printf("-----------------------------------------------\n");

		bufx = (double *)malloc(sizeof(double) * tne * np);
		bufq = (double *)malloc(sizeof(double) * tne * np);

		for (int i = 0; i < ne * np; i++)
		{
			bufx[i] = xx[i];
			bufq[i] = qq[i];
		}
	}
	if (rank == 0)
	{
		int index[nprocs];
		index[0] = ne * np;
		int idx = index[0];
		for (int i = 1; i < nprocs; i++)
		{
			MPI_Irecv(index + i, 1, MPI_INT, i, 11, MPI_COMM_WORLD, &rer1);
			MPI_Wait(&rer1, &st);
			index[i] *= np;
			MPI_Irecv(bufx + idx, index[i], MPI_DOUBLE, i, 22, MPI_COMM_WORLD, &rer2);
			MPI_Irecv(bufq + idx, index[i], MPI_DOUBLE, i, 33, MPI_COMM_WORLD, &rer3);
			MPI_Wait(&rer2, &st);
			MPI_Wait(&rer3, &st);
			idx += index[i];
		}

		for(int i = 0; i < tne*np; i++){
			printf("%f ", bufx[i]);
		}
		printf("\n");
		for(int i = 0; i < tne*np; i++){
			printf("%f ", bufq[i]);
		}
		printf("\n");

		save_field(bufx, bufq, tne, roots, eres);
		t_end = clock();
		printf("Motion time = %f msec\n", (double)(t_end - t_sta) / 1000.0);
	}
	free(roots);
	free(weights);
	free(ll);
	free(dl);
	free(dx);
	free(mesh);
	free(smat);
	free(xx);
	free(qq);
	free(qtemp);
	free(k1);
	free(k2);
	free(k3);
	free(k4);
	free(minv_vec);
	free(mmat);
	free(dv);
	free(mf);
	free(ib);
	free(fstar);
	free(df);

	MPI_Finalize();
	return 0;
}
コード例 #4
0
int main(int argc, char **argv){

	double tend = 1E2, speed = 1.;
	// double tend = 1E-1, speed = 1.;
	char *init_type="mixed2";
	double *roots, *weights, *ll, *dl, xmin, xmax, 
		   deltax, jac, xr, xl, cfl, dt, rtime, min_dx;
	int ii, jj, kk, ee, idx, eres;
	long nstep;
	double *dx, *mesh; 
	double *smat, *xx, *qq, *qtemp, *k1, *k2, *k3, *k4, *minv_vec, *mmat, *dv, 
		   *mf, *ib, *df, *fstar;

	// initialize 
	// fortran index structure array[ii,jj,ee] where size(array) = (np, np, ne)
	// c 1d index structure array = [ee*np*np + jj*np + ii]
	roots   = (double*)malloc(np*   sizeof(double));
	weights = (double*)malloc(np*   sizeof(double));
	ll      = (double*)malloc(np*   sizeof(double));
	dl      = (double*)malloc(np*   sizeof(double));
	dx      = (double*)malloc(ne*   sizeof(double));
	mesh    = (double*)malloc((ne+1)*sizeof(double));

	smat	= (double*)malloc(np*np*sizeof(double));    // [jj np, ii np]
	xx		= (double*)malloc(ne*np*sizeof(double));    // [ee ne, ii np]
	qq		= (double*)malloc(ne*np*sizeof(double));    // [ee ne, ii np]	
	qtemp	= (double*)malloc(ne*np*sizeof(double));    // [ee ne, ii np]	
	k1		= (double*)malloc(ne*np*sizeof(double));    // [ee ne, ii np]	
	k2		= (double*)malloc(ne*np*sizeof(double));    // [ee ne, ii np]
	k3		= (double*)malloc(ne*np*sizeof(double));    // [ee ne, ii np]
	k4		= (double*)malloc(ne*np*sizeof(double));    // [ee ne, ii np]
	minv_vec= (double*)malloc(ne*np*sizeof(double));    // [ee ne, ii np]
	mmat	= (double*)malloc(ne*np*np*sizeof(double)); // [ee ne, jj np, ii np]
	dv		= (double*)malloc(ne*np*np*sizeof(double)); // [ee ne, jj np, ii np]
	mf		= (double*)malloc(2*np*sizeof(double));     // [jj 2,  ii np]
	ib		= (double*)malloc(2*np*sizeof(double));     // [jj 2,  ii np]
	fstar	= (double*)malloc(2*ne*sizeof(double));     // [jj 2,  ii ne]
	df		= (double*)malloc(ne*2*np*sizeof(double));  // [ee ne, jj 2, ii np]

	for (ii=0; ii<np; ++ii){
		roots[ii] = 0;
		weights[ii] = 0;
		ll[ii] = 0;
		dl[ii] = 0;
	}
	for (ii=0; ii<ne; ++ii){
		dx[ii] = 0;
		mesh[ii] = 0;
	}
	mesh[ne] = 0;

	
	for (ii=0; ii<np*np; ++ii){
		smat[ii] = 0;
	}
	for (ii=0; ii<ne*np; ++ii){
		xx[ii]	= 0;		 	
		qq[ii]	= 0;		 	
		k1[ii]	= 0;		 	
		k2[ii]	= 0;		 	
		k3[ii]	= 0;		 	
		k4[ii]	= 0;		 	
		minv_vec[ii]	= 0; 
	}
	for (ii=0; ii<ne*np*np; ++ii){
		mmat[ii] = 0;    	
		dv[ii]	 = 0;
	}
	for (ii=0; ii<np*2; ++ii){
		mf[ii] = 0;
		ib[ii] = 0;
	}
	for (ii=0; ii<ne*2; ++ii){
		fstar[ii] = 0;
	}
	for (ii=0; ii<ne*2*np; ++ii){
		df[ii] = 0;
	}

	// mesh setup
	xmin = 0.;
	xmax = 10.;
	deltax = (xmax-xmin)/(double)ne;
	mesh[ne] = xmax;
	for(ee=0;ee<ne;++ee) {
		mesh[ee] = xmin+ee*deltax;
	}
	
	// gauss lobatto quadrature point, weight setup
	gausslobatto_quadrature(np, roots, weights);

	// coordinates and element size
	min_dx = xmax - xmin; // initial guess
	for(ee=0;ee<ne;ee++){
		xl = mesh[ee];
		xr = mesh[ee+1];
		dx[ee] = xr-xl; // size of each element
		if(dx[ee] < min_dx){
			min_dx = dx[ee]; // finding minimum dx
		}
		for(ii=0;ii<np;ii++){
			idx = ee*np+ii;
			xx[idx] = xl + 0.5*(1+roots[ii])*dx[ee];
		}
	}

	// mass matrix
	for(ii=0;ii<ne*np*np;ii++){
		mmat[ii] = 0;
	}

	for(ee=0;ee<ne;ee++){
		jac = fabs(dx[ee])/2;
		for(kk=0;kk<np;kk++){
			lagrange(roots[kk], ll, roots);
			for(jj=0;jj<np;jj++){
				for(ii=0;ii<np;ii++){
					idx = ee*np*np+jj*np+ii;
					// mass matrix mmat[ne][np][np] in 1d index representation
					mmat[idx] += jac*weights[kk]*ll[ii]*ll[jj];
				}
			}
		}
	}

	// stiffness matrix
	for(ii=0;ii<np*np;ii++){
		smat[ii] = 0;
	}
	for(kk=0;kk<np;kk++){
		lagrange(roots[kk], ll, roots);
		lagrange_deriv(roots[kk], dl, roots);
		for(jj=0;jj<np;jj++){
			for(ii=0;ii<np;ii++){
				idx = jj*np+ii;
				// stiffness matrix smat[np][np] in 1d index representation
				smat[idx] += weights[kk]*ll[jj]*dl[ii];
			}
		}
	}

	// face integration
	for(ii=0;ii<np*2;ii++){
		mf[ii] = 0;
	}
	lagrange(-1,mf,   roots); // mf[ii] for(ii=0, ii<np,ii++) represents element left face integration
	lagrange( 1,mf+np,roots); // mf[ii] for ii=np, ii<2*np, ii++) reresents element right face integration

	// boundary interpolation
	for(ii=0;ii<np*2;ii++){
		ib[ii] = 0;
	}
	lagrange(-1,ib,   roots); // element left edge interpolation
	lagrange( 1,ib+np,roots); // element right edge interpolation

	
	// divergence operators
	for(ii=0;ii<ne*np*np;ii++){
		dv[ii] = 0;
	}
	for(ii=0;ii<ne*np*2;ii++){
		dv[ii] = 0;
	}

	for(ee=0;ee<ne;ee++){
		for(jj=0;jj<np;jj++){
			// it turn out that mmat is diagonal. i.e., ii != jj, mmat[ee][jj][ii] = 0
			// the inverse of mmat is just the inverse of the diagonal components
			// here, we are extracting the inverse diagonal components only
			minv_vec[ee*np+jj] = 1./mmat[ee*np*np+jj*np+jj];
		}
		for(jj=0;jj<np;jj++){
			for(ii=0;ii<np;ii++){
				dv[ee*np*np+jj*np+ii] = minv_vec[ee*np+ii]*smat[jj*np+ii];
			}
		}
		for(jj=0;jj<2;jj++){
			for(ii=0;ii<np;ii++){
				df[ee*np*2+jj*np+ii]  = minv_vec[ee*np+ii]*mf[jj*np+ii];
			}
		}

	}
	
	// initialize qq field
	initialize(qq, xx, xmax, xmin, init_type);
	cfl = 1./(np*np);
	dt = cfl * min_dx / fabs(speed);
	rtime = 0.;
	nstep = 0;

	printf("Start Time Integration\n");

	// Runge-Kutta 4th order Time integration loop
	
	t_sta = clock();

	while(rtime < tend){
		dt = fmin(dt, tend-rtime);

		rhs(qq,	   k1, dv, df, ib, speed);

		for(ii=0;ii<ne*np;ii++)
			qtemp[ii] = qq[ii]+0.5*dt*k1[ii];
		rhs(qtemp, k2, dv, df, ib, speed);

		for(ii=0;ii<ne*np;ii++)
			qtemp[ii] = qq[ii]+0.5*dt*k2[ii];
		rhs(qtemp, k3, dv, df, ib, speed);
		
		for(ii=0;ii<ne*np;ii++)
			qtemp[ii] = qq[ii]+dt*k3[ii];
		rhs(qtemp, k4, dv, df, ib, speed);

		for(ii=0;ii<ne*np;ii++)
			qq[ii] += 1./6.*dt*(k1[ii]+2*k2[ii]+2*k3[ii]+k4[ii]);

		rtime += dt;
		nstep += 1;
		if(nstep%10000 == 0) 
			printf("nstep = %10ld, %5.1f%% complete\n", nstep, rtime/tend*100);
	}

	// timeloop ends here;

	printf("Integration complete\n");

	if(ne > 200){
		eres = 2;
	}
	else if (ne > 60){
		eres = 3;
	}
	else if (ne > 30){
		eres = 6;
	}
	else {
		eres = 10;
	}

	// final report
	printf("-----------------------------------------------\n");
	printf("code type   : c serial\n");
	printf("Final time  : %13.5e\n", rtime);
	printf("CFL         : %13.5e\n", cfl);
	printf("DOF         : %13d\n", ne*np);
	printf("No. of Elem : %13d\n", ne);
	printf("Order       : %13d\n", np);
	printf("eres        : %13d\n", eres);
	printf("time steps  : %13ld\n", nstep);
	printf("-----------------------------------------------\n");

	save_field(xx, qq, ne, roots, eres);

	t_end = clock();
	printf("Motion time = %f msec\n", (double)(t_end - t_sta)/1000.0);

	free(roots);   
	free(weights); 
	free(ll);      
	free(dl);      
	free(dx);      
	free(mesh);    
	free(smat);	
	free(xx);		
	free(qq);		
	free(qtemp);	
	free(k1);		
	free(k2);		
	free(k3);		
	free(k4);		
	free(minv_vec);
	free(mmat);	
	free(dv);		
	free(mf);		
	free(ib);		
	free(fstar);	
	free(df);		

	return 0;
}
コード例 #5
0
ファイル: pictures.c プロジェクト: mortid0/gefestus
void bird_style_pics(int step)
{
	int kx, ky, krp, nc, nm;
	PARTICLE *p;
	double avr_conc[NC], avr_temp[NC], avr_mass[NC], new_avr_vx[NC], new_avr_vy[NC];
	for (nc = 0; nc < NC; nc++)
	{
		new_avr_vx[nc] = 0;
		new_avr_vy[nc] = 0;
		for (kx = 0; kx < KX; kx++)
		{
			for (ky = 0; ky < KY; ky++)
			{
				temp_field[nc][kx][ky] = 0.0;
				conc_field[nc][kx][ky] = 0.0;
				mass_field[nc][kx][ky] = 0.0;
				impulse_field[nc][kx][ky] = 0.0;
			}
		}
		for (kx = 0; kx < MAX_PLOT_VEL; kx++)
		{
			distr_x[nc][kx] = 0;
			distr_y[nc][kx] = 0;
		}
	}
	for (krp = 0; krp < KRP; krp++)
	{
		for (kx = 0; kx < KX; kx++)
		{
			for (ky = 0; ky < KY; ky++)
			{
				for (nc = 0; nc < NC; nc++)
				{
					for (nm = 0; nm < NM; nm++)
					{
						p = &camera[krp][kx][ky][nc][nm];
						conc_field[nc][kx][ky] += p->weight;
						temp_field[nc][kx][ky] += get_energy(p);
						mass_field[nc][kx][ky] += p->weight * p->mass * SIZEX * SIZEY * SIZEZ;
						impulse_field[nc][kx][ky] += p->weight * atan2(p->v.y, p->v.x);
						new_avr_vx[nc] += p->weight * p->v.x;
						new_avr_vy[nc] += p->weight * p->v.y;
						int num_vel_x = (int)(p->v.x + 0.5 * MAX_PLOT_VEL), num_vel_y = (int)(p->v.y + 0.5 * MAX_PLOT_VEL);
						if ((num_vel_x < MAX_PLOT_VEL)&&(num_vel_x>0)) {distr_x[nc][num_vel_x] += p->weight;}
						if ((num_vel_y < MAX_PLOT_VEL)&&(num_vel_y>0)) {distr_y[nc][num_vel_y] += p->weight;}
// 						if (fabs(p->v.x) < 0.5*MAX_PLOT_VEL) {distr_x[nc][(int)(p->v.x + 0.5 * MAX_PLOT_VEL)] += p->weight;}
// 						if (fabs(p->v.y) < 0.5*MAX_PLOT_VEL) {distr_y[nc][(int)(p->v.y + 0.5 * MAX_PLOT_VEL)] += p->weight;}
					}
				}
			}
		}
	}
	for (nc = 0; nc < NC; nc++)
	{
		avr_conc[nc] = 0;
		avr_temp[nc] = 0;
		avr_mass[nc] = 0;
		for (kx = 0; kx < KX; kx++)
		{
			for (ky = 0; ky < KY; ky++)
			{
				temp_field[nc][kx][ky] /= KRP;
				avr_temp[nc] += temp_field[nc][kx][ky];
				conc_field[nc][kx][ky] /= KRP;
				avr_conc[nc] += conc_field[nc][kx][ky];
				mass_field[nc][kx][ky] /= KRP;
				avr_mass[nc] += mass_field[nc][kx][ky];
				impulse_field[nc][kx][ky] = (conc_field[nc][kx][ky]>0)?(impulse_field[nc][kx][ky]/(conc_field[nc][kx][ky]*KRP)):100.0;
			}
		}
		avr_vx[nc] = (avr_conc[nc] > 0) ? new_avr_vx[nc]/(avr_conc[nc] * KRP) : 0;
		avr_vy[nc] = (avr_conc[nc] > 0) ? new_avr_vy[nc]/(avr_conc[nc] * KRP) : 0;
		printf("comp#%i: temp = %5.5e; conc = %5.5e; mass = %5.5e; vx = %e; vy = %e;\n", nc, avr_temp[nc]/(KX*KY), avr_conc[nc]/(KX*KY), avr_mass[nc] /(KX*KY), avr_vx[nc], avr_vy[nc]);
		
	}
	save_plot("temp2/plotx", distr_x, step);
	save_plot("temp2/ploty", distr_y, step);
	save_field("temp2/temp", temp_field, step);
	save_field("temp2/conc", conc_field, step);
	save_field("temp2/full_mass", mass_field, step);
	save_three_column("temp2/impulse", impulse_field, step);
//	print_forces(step);
}