snap_ssg.c 8.44 KB
Newer Older
tilman.metz's avatar
tilman.metz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
/*------------------------------------------------------------------------
 * Copyright (C) 2011 For the list of authors, see file AUTHORS.
 *
 * This file is part of SOFI3D.
 * 
 * SOFI3D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 * 
 * SOFI3D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SOFI3D. See file COPYING and/or 
  * <http://www.gnu.org/licenses/gpl-2.0.html>.
--------------------------------------------------------------------------*/
/*------------------------------------------------------------------------
 *   Write 3D snapshot for current timestep  to disk                                   
 *
 *  ----------------------------------------------------------------------*/

#include "fd.h"


void snap(FILE *fp, int nt, int nsnap, int format, int type, 
float ***vx, float ***vy, float ***vz, float ***sxx, float ***syy, float ***szz,
float ***u, float ***pi,
int idx, int idy, int idz, int nx1, int ny1, int nz1, int nx2, 
int ny2, int nz2){

	/* 
	different data formats of output available:
	format=1  :  SU (IEEE)
	format=2  :  ASCII
	format=3  :  BINARY (IEEE)
	
	different types:
	type=1 : values in vx, vy, and vz
	type=2 : -(sxx+syy+szz) (pressure field)
	type=3 : divergence of vx, vy and vz (energy of compressional waves)
	         and curl of vx, vy and vz (energy of shear waves)
	type=4 : both particle velocities (type=1) and energy (type=3)
	*/


	
	char xfile[STRING_SIZE], yfile[STRING_SIZE], zfile[STRING_SIZE];
	char rotfile[STRING_SIZE], ext[8], wm[2];
	char  divfile[STRING_SIZE], pfile[STRING_SIZE];
	FILE *fpx1, *fpy1, *fpz1, *fpx2, *fpy2, *fpp;
	int i,j,k;
	float a=0.0, amp, dh24x, dh24y, dh24z, vyx, vxy, vxx, vyy, vzx, vyz, vxz, vzy, vzz;


	extern float DX, DY, DZ, DT;
	extern char SNAP_FILE[STRING_SIZE];
	extern int MYID, POS[4], SNAP_PLANE, LOG;



	switch(format){
	case 1: 
		sprintf(ext,".su");
		break;
	case 2: 
		sprintf(ext,".asc");
		break;
	case 3: 
		sprintf(ext,".bin");
		break;
	}


	sprintf(xfile,"%s%s.vx.%i%i%i",SNAP_FILE,ext,POS[1],POS[2],POS[3]);
	sprintf(yfile,"%s%s.vy.%i%i%i",SNAP_FILE,ext,POS[1],POS[2],POS[3]);
	sprintf(zfile,"%s%s.vz.%i%i%i",SNAP_FILE,ext,POS[1],POS[2],POS[3]);
	sprintf(divfile,"%s%s.div.%i%i%i",SNAP_FILE,ext,POS[1],POS[2],POS[3]);
	sprintf(rotfile,"%s%s.curl.%i%i%i",SNAP_FILE,ext,POS[1],POS[2],POS[3]);
	sprintf(pfile,"%s%s.p.%i%i%i",SNAP_FILE,ext,POS[1],POS[2],POS[3]);

        if (LOG){
	fprintf(fp,"\n\n PE %d is writing snapshot-data at T=%fs to \n",MYID,nt*DT);}


	if (nsnap==1) 
		sprintf(wm,"w");
	else 
		sprintf(wm,"a");

	switch(type){
	case 1 :
		fprintf(fp,"\t%s\n", xfile);
		fprintf(fp,"\t%s\n", yfile);
		fprintf(fp,"\t%s\n\n", zfile);
		fpx1=fopen(xfile,wm);
		fpy1=fopen(yfile,wm);
		fpz1=fopen(zfile,wm);
		for (k=nz1;k<=nz2;k+=idz)
			for (i=nx1;i<=nx2;i+=idx)
				for (j=ny1;j<=ny2;j+=idy){
				
			
					writedsk(fpx1,vx[j][i][k],format);
					writedsk(fpy1,vy[j][i][k],format);
					writedsk(fpz1,vz[j][i][k],format);
					
					
				}
		fclose(fpx1);
		fclose(fpy1);
		fclose(fpz1);
		break;
	case 2 :
		fprintf(fp,"\t%s\n\n", pfile);
		fpp=fopen(pfile,wm);
		for (k=nz1;k<=nz2;k+=idz)
			for (i=nx1;i<=nx2;i+=idx)
				for (j=ny1;j<=ny2;j+=idy){
					amp=-sxx[j][i][k]-syy[j][i][k]-szz[j][i][k];
					
				
					writedsk(fpp,amp,format);

				}
		fclose(fpp);
		break;
	case 4 :
		fprintf(fp,"\t%s\n", xfile);
		fprintf(fp,"\t%s\n", yfile);
		fprintf(fp,"\t%s\n\n", zfile);
		fprintf(fp,"\t%s\n\n", pfile);
		fpx1=fopen(xfile,wm);
		fpy1=fopen(yfile,wm);
		fpz1=fopen(zfile,wm);
		fpp=fopen(pfile,wm);
		for (k=nz1;k<=nz2;k+=idz)
			for (i=nx1;i<=nx2;i+=idx)
				for (j=ny1;j<=ny2;j+=idy){
					amp=-sxx[j][i][k]-syy[j][i][k]-szz[j][i][k];

					writedsk(fpx1,vx[j][i][k],format);
					writedsk(fpy1,vy[j][i][k],format);
					writedsk(fpz1,vz[j][i][k],format);
					writedsk(fpp,amp,format);
					
				}
		fclose(fpx1);
		fclose(fpy1);
		fclose(fpz1);
		fclose(fpp);
	case 3 :
		/* output of the curl of the velocity field according to Dougherty and
		                  Stephen (PAGEOPH, 1988) */
		fprintf(fp,"\t%s\n", divfile);
		fprintf(fp,"\t%s\n\n", rotfile);
		fpx2=fopen(divfile,wm);
		fpy2=fopen(rotfile,wm);
		
		dh24x=1.0/DX;
		dh24y=1.0/DY;
		dh24z=1.0/DZ;
		
		for (k=nz1;k<=nz2;k+=idz)
			for (i=nx1;i<=nx2;i+=idx)
				for (j=ny1;j<=ny2;j+=idy){
					/*vxy=(-vx[j+2][i][k]+27.0*(vx[j+1][i][k]-vx[j][i][k])+vx[j-1][i][k])*(dh24);
					vxz=(-vx[j][i][k+2]+27.0*(vx[j][i][k+1]-vx[j][i][k])+vx[j][i][k-1])*(dh24);
					vyx=(-vy[j][i+2][k]+27.0*(vy[j][i+1][k]-vy[j][i][k])+vy[j][i-1][k])*(dh24);
					vyz=(-vy[j][i][k+2]+27.0*(vy[j][i][k+1]-vy[j][i][k])+vy[j][i][k-1])*(dh24);
					vzx=(-vz[j][i+2][k]+27.0*(vz[j][i+1][k]-vz[j][i][k])+vz[j][i-1][k])*(dh24);
					vzy=(-vz[j+2][i][k]+27.0*(vz[j+1][i][k]-vz[j][i][k])+vz[j-1][i][k])*(dh24);*/
					
					vxy=(vx[j+1][i][k]-vx[j][i][k])*(dh24y);
					vxz=(vx[j][i][k+1]-vx[j][i][k])*(dh24z);
					vyx=(vy[j][i+1][k]-vy[j][i][k])*(dh24x);
					vyz=(vy[j][i][k+1]-vy[j][i][k])*(dh24z);
					vzx=(vz[j][i+1][k]-vz[j][i][k])*(dh24x);
					vzy=(vz[j+1][i][k]-vz[j][i][k])*(dh24y);
					
					/*amp= absolute value of curl(v)), without sqrt!!!*/
					amp=((vzy-vyz)*(vzy-vyz)+(vxz-vzx)*(vxz-vzx)+(vyx-vxy)*(vyx-vxy)); 
					
					/*note that "y" denotes the vertical coordinate*/
					switch(SNAP_PLANE){
					case 1 : /* energy without sign */
						/* sqrt(Es) with Es = u*amp*amp (second amp removed due to missing sqrt in amp*/
						a=sqrt((u[j][i][k])*amp);
						break;
					case 2 : /* energy with sign true for x-y-plane */
						/*sign(rot(v)x * sqrt(Es) with Es = u*amp*amp (second amp removed due to missing sqrt in amp*/
						a=fsign((vxy-vyx))*sqrt((u[j][i][k])*amp);
						break;
					case 3 : /* energy with sign true for x-z-plane */
						/*sign(rot(v)r * sqrt(Es) with Es = u*amp*amp (second amp removed due to missing sqrt in amp*/
						a=fsign((vxz-vzx))*sqrt((u[j][i][k])*amp);
						break;
					case 4 :/* energy with sign true for y-z-plane */
						/*sign(rot(v)t * sqrt(Es) with Es = u*amp*amp (second amp removed due to missing sqrt in amp*/
						a=fsign((vzy-vyz))*sqrt((u[j][i][k])*amp);
						break;
					case 5 : /*custom force*/ /*not yet working properly*/
						/*sign(rot(v)t * sqrt(Es) with Es = u*amp*amp (second amp removed due to missing sqrt in amp*/
						a=fsign((vxz-vzx))*sqrt((u[j][i][k])*amp);
						break;
					
					}
					
					/*sign(rot(v)t * sqrt(Es) with Es = u*amp*amp (second amp removed due to missing sqrt in amp*/
					/*a=fsign((vxz-vzx))*sqrt((u[j][i][k])*amp); 

					amp=u[j][i][k]*((vyz-vzy)*fabs(vyz-vzy)+
					    (vzx-vxz)*fabs(vzx-vxz)+(vxy-vyx)*fabs(vxy-vyx));
					a=fsign(amp)*sqrt(fabs(amp));*/
					
					writedsk(fpy2,a,format);
					   

				}



		/* output of the divergence of the velocity field according to Dougherty and
		                  Stephen (PAGEOPH, 1988) */
		for (k=nz1;k<=nz2;k+=idz)
			for (i=nx1;i<=nx2;i+=idx)
				for (j=ny1;j<=ny2;j+=idy){
					/*vxx=(-vx[j][i+1][k]+27.0*(vx[j][i][k]-vx[j][i-1][k])+vx[j][i-2][k])*(dh24);
					vyy=(-vy[j+1][i][k]+27.0*(vy[j][i][k]-vy[j-1][i][k])+vy[j-2][i][k])*(dh24);
					vzz=(-vz[j][i][k+1]+27.0*(vz[j][i][k]-vz[j][i][k-1])+vz[j][i][k-2])*(dh24);*/
					
					vxx=(vx[j][i][k]-vx[j][i-1][k])*(dh24x);
					vyy=(vy[j][i][k]-vy[j-1][i][k])*(dh24y);
					vzz=(vz[j][i][k]-vz[j][i][k-1])*(dh24z);
					
					/*amp= div(v))*/
					amp=(vxx+vyy+vzz);
					
					switch(SNAP_PLANE){
					case 1 : /* energy without sign */
						/* Ep with Ep=pi*amp*amp */
						a=sqrt((pi[j][i][k])*amp*amp);
						break;
					case 2 : /* single force in x */
						/*sign of div(v) * Ep with Ep=pi*amp*amp */
						a=fsign(amp)*sqrt((pi[j][i][k])*amp*amp);
						break;
					case 3 : /* single force in y */
						/*sign of div(v) * Ep with Ep=pi*amp*amp */
						a=fsign(amp)*sqrt((pi[j][i][k])*amp*amp);
						break;
					case 4 : /* single force in z */
						/*sign of div(v) * Ep with Ep=pi*amp*amp */
						a=fsign(amp)*sqrt((pi[j][i][k])*amp*amp);
						break;
					}
					
					/*sign of div(v) * Ep with Ep=pi*amp*amp */
					/*a=fsign(amp)*sqrt((pi[j][i][k])*amp*amp);
										
					a=(vxx+vyy+vzz)*sqrt(pi[j][i][k]);*/
					
					writedsk(fpx2,a,format);
				
					

				}

		fclose(fpx2);
		fclose(fpy2);
		break;
	}
}