PCG.c 21.7 KB
Newer Older
Tilman Steinweg's avatar
Tilman Steinweg committed
1
/*-----------------------------------------------------------------------------------------
2
 * Copyright (C) 2016  For the list of authors, see file AUTHORS.
Tilman Steinweg's avatar
Tilman Steinweg committed
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
 *
 * This file is part of DENISE.
 * 
 * DENISE 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.
 * 
 * DENISE 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 DENISE. See file COPYING and/or <http://www.gnu.org/licenses/gpl-2.0.html>.
-----------------------------------------------------------------------------------------*/

/*------------------------------------------------------------------------
 * Module for the Preconditioned Conjugate Gradient Method (PCG)
 * for the material parameters vp, vs, rho and lambda, mu, rho respectively
 * 
 * Daniel Koehn
 * ----------------------------------------------------------------------*/

#include "fd.h"

void PCG(float ** waveconv, float ** taper_coeff, int nsrc, float ** srcpos, int ** recpos, int ntr_glob, int iter, float C_vp, float ** gradp, int nfstart_jac, float ** waveconv_u, float C_vs, float ** gradp_u, float ** waveconv_rho, float C_rho, float ** gradp_rho, float Vs_avg, float FC){
	
	extern int NX, NY, IDX, IDY, SPATFILTER, GRAD_FILTER;
31
	extern int INVMAT, SWS_TAPER_GRAD_VERT, SWS_TAPER_GRAD_HOR, SWS_TAPER_GRAD_SOURCES, SWS_TAPER_FILE;
Tilman Steinweg's avatar
Tilman Steinweg committed
32 33
	extern int POS[3], MYID, ACOUSTIC;
	extern char JACOBIAN[STRING_SIZE];
34 35 36
    extern int RESTART_WORKFLOW;
    extern int VERBOSE;
    extern int start_iter;
Tilman Steinweg's avatar
Tilman Steinweg committed
37 38 39 40
	char jac[225], jac2[225];
	int i, j;
	float betaz, betan, gradplastiter, gradclastiter, betar, beta;
	extern FILE *FP;
41 42
    int use_conjugate_1=1;
    int use_conjugate_2=1;
Tilman Steinweg's avatar
Tilman Steinweg committed
43 44
	FILE *FP3, *FP4, *FP6, *FP5;
	
45 46


Tilman Steinweg's avatar
Tilman Steinweg committed
47 48 49 50
	/* ===================================================================================================================================================== */
	/* ===================================================== GRADIENT ZP ================================================================================== */
	/* ===================================================================================================================================================== */
	
51
	if((INVMAT==0)){
Tilman Steinweg's avatar
Tilman Steinweg committed
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
		
		/* Preconditioning of the gradient */
		/* ------------------------------- */
		
		/* apply taper on the gradient */
		/* --------------------------- */
		
		if (SWS_TAPER_GRAD_VERT){   /*vertical gradient taper is applied*/
		taper_grad(waveconv,taper_coeff,srcpos,nsrc,recpos,ntr_glob,1);}
		
		if (SWS_TAPER_GRAD_HOR){   /*horizontal gradient taper is applied*/
		taper_grad(waveconv,taper_coeff,srcpos,nsrc,recpos,ntr_glob,2);}
		
		if (SWS_TAPER_GRAD_SOURCES){   /*cylindrical taper around sources is applied*/
		taper_grad(waveconv,taper_coeff,srcpos,nsrc,recpos,ntr_glob,3);}
		
		if (SWS_TAPER_FILE){   /* read taper from BIN-File*/
		taper_grad(waveconv,taper_coeff,srcpos,nsrc,recpos,ntr_glob,4);}
		
		/* save gradient */
		sprintf(jac,"%s_g.old.%i.%i",JACOBIAN,POS[1],POS[2]);
		FP3=fopen(jac,"wb");
		
		for (i=1;i<=NX;i=i+IDX){
		for (j=1;j<=NY;j=j+IDY){
			fwrite(&waveconv[j][i],sizeof(float),1,FP3);
		}
		}
			
		fclose(FP3);
		
		MPI_Barrier(MPI_COMM_WORLD);
			
		/* merge gradient file */ 
		sprintf(jac,"%s_g.old",JACOBIAN);
		if (MYID==0) mergemod(jac,3);
88 89 90 91 92
        
		MPI_Barrier(MPI_COMM_WORLD);
        sprintf(jac,"%s_g.old.%i.%i",JACOBIAN,POS[1],POS[2]);
        remove(jac);
        
Tilman Steinweg's avatar
Tilman Steinweg committed
93 94 95 96 97
		/* Normalize gradient to maximum value */
		/*norm(waveconv,iter,1);*/
		
		/* apply spatial wavelength filter */
		if(SPATFILTER==1){
98
			if (MYID==0&&VERBOSE){
Tilman Steinweg's avatar
Tilman Steinweg committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
			fprintf(FP,"\n Spatial filter is applied to gradient (written by PE %d)\n",MYID);}
			spat_filt(waveconv,iter,1);}
		
		/* apply 2D-Gaussian filter*/
		if(GRAD_FILTER==1){smooth(waveconv,1,1,Vs_avg,FC);}
		
		/* output of the preconditioned gradient */
		for (i=1;i<=NX;i=i+IDX){
		for (j=1;j<=NY;j=j+IDY){
			waveconv[j][i] = C_vp * waveconv[j][i];
			gradp[j][i]=waveconv[j][i];
		}
		}
		
		/* save gradient for output as inversion result */
		if(iter==nfstart_jac){
115
                        
Tilman Steinweg's avatar
Tilman Steinweg committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
			sprintf(jac,"%s_p_it%d.old.%i.%i",JACOBIAN,iter,POS[1],POS[2]);
			FP3=fopen(jac,"wb");
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				fwrite(&waveconv[j][i],sizeof(float),1,FP3);
			}
			}
			
			fclose(FP3);
			
			MPI_Barrier(MPI_COMM_WORLD);
			
			/* merge gradient file */ 
			sprintf(jac,"%s_p_it%d.old",JACOBIAN,iter);
			if (MYID==0) mergemod(jac,3);
			MPI_Barrier(MPI_COMM_WORLD);
			sprintf(jac,"%s_p_it%d.old.%i.%i",JACOBIAN,iter,POS[1],POS[2]);
			remove(jac);
		}
		
		/* calculate conjugate gradient direction, if iter > 1 (after Mora 1987) */
		/* --------------------------------------------------------------------- */
		
140
		if((iter>1)&&(use_conjugate_1)){
Tilman Steinweg's avatar
Tilman Steinweg committed
141 142 143 144
			
			sprintf(jac,"%s_p.old.%i.%i",JACOBIAN,POS[1],POS[2]);
			FP6=fopen(jac,"rb");
			
145
			if((iter>2)&&(use_conjugate_2)){
Tilman Steinweg's avatar
Tilman Steinweg committed
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
				sprintf(jac2,"%s_c.old.%i.%i",JACOBIAN,POS[1],POS[2]);
				FP5=fopen(jac2,"rb");
			}
			
			/* apply scalar product to obtain the coefficient beta */
			betaz = 0.0;
			betan = 0.0;
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				
				fread(&gradplastiter,sizeof(float),1,FP6);
				
				/*if(gradglastiter==gradg[j][i]) err("TEST1");*/
				/*if (MYID==10)  printf("TEST beta (MYID=%d) bei (j,i)=(%i,%i): gradg(k-1) = %e, gradg(k) = %e\n",MYID,j,i,gradglastiter,gradg[j][i]);*/
				
				/*
				betaz += (1e5*gradp[j][i]) * ( (1e5*gradg[j][i]) - (1e5*gradglastiter) );
				betan += (1e5*gradplastiter) * (1e5*gradglastiter);
				*/
				
				/* Polak and Ribiere */
				/*betaz += (gradp[j][i]) * ( (gradg[j][i]) - (gradglastiter) );
				betan += (gradplastiter) * (gradglastiter);*/
				
				/* Polak and Ribiere */
				betaz += (gradp[j][i]) * ( (gradp[j][i]) - (gradplastiter) );
				betan += (gradplastiter) * (gradplastiter);
				
				/* Fletcher and Reeves */
				/*betaz += (gradp[j][i]) * (gradg[j][i]);
				betan += (gradplastiter) * (gradglastiter);*/
				
			}
			}
			
			/*printf("TEST: vor exchange (MYID=%d): beta = betaz/betan = %e/%e = %e\n",MYID,betaz,betan,betaz/betan);*/
			
			/*betaz = exchange_L2(betaz,1,1);
			betan = exchange_L2(betan,1,1);*/
			
			betar = 0.0;
			MPI_Allreduce(&betaz,&betar,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
			betaz = betar;
			
			betar = 0.0;
			MPI_Allreduce(&betan,&betar,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
			betan = betar;
			
// 			beta = betaz/betan;
			beta = 0.0f;
			if(betan !=0.0f) beta = betaz/betan;
			
			/* direction reset */
			if(beta<0.0){beta = 0.0;}
			
			/*betaVp = beta;*/
			
203
			/*printf("\n\nTEST: nach exchange (MYID=%d): beta = %e / %e = %e\n",MYID,betaz,betan,beta);*/
Tilman Steinweg's avatar
Tilman Steinweg committed
204 205 206 207 208 209 210 211 212 213 214
			
			fseek(FP6,0,SEEK_SET);
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				
				if(iter==2){
					fread(&gradplastiter,sizeof(float),1,FP6);
					waveconv[j][i] = gradp[j][i] + gradplastiter * beta;
				}
				
215
				if((iter>2)&&(use_conjugate_2)){
Tilman Steinweg's avatar
Tilman Steinweg committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
					fread(&gradclastiter,sizeof(float),1,FP5);
					waveconv[j][i] = gradp[j][i] + gradclastiter * beta;
				}
				
				/*if (iter >= 2){
					if (isnan(waveconv[j][i]) || isinf(waveconv[j][i])){
						sum = 0.0;
						h = 0;
						for (ii=-1;ii<=1;ii++){
						for (jj=-1;jj<=1;jj++){
							if (isnan(waveconv[j+jj][i+ii]) || isinf(waveconv[j+jj][i+ii])) continue;
							sum += waveconv[j+jj][i+ii];
							h++;
						}
						}
						if (h>0) waveconv[j][i] = sum / h;
						else waveconv[j][i] = 0.0;
					}
				}*/
			}
			}
			
			fclose(FP6);
			
240
			if((iter>2)&&(use_conjugate_2)){fclose(FP5);}
Tilman Steinweg's avatar
Tilman Steinweg committed
241 242 243
		}
		
		/* output of the conjugate gradient */
244
		if((iter>1)&&(use_conjugate_1)){
Tilman Steinweg's avatar
Tilman Steinweg committed
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 277 278 279 280 281 282 283 284 285 286 287
			sprintf(jac2,"%s_c.old.%i.%i",JACOBIAN,POS[1],POS[2]);
			FP5=fopen(jac2,"wb");
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				fwrite(&waveconv[j][i],sizeof(float),1,FP5);
			}
			}
			
			fclose(FP5);
			
			MPI_Barrier(MPI_COMM_WORLD);
			
			/* merge gradient file */ 
			sprintf(jac2,"%s_c.old",JACOBIAN);
			if (MYID==0) mergemod(jac2,3);
		}
		
		/* output of preconditioned gradient */
		sprintf(jac,"%s_p.old.%i.%i",JACOBIAN,POS[1],POS[2]);
		FP4=fopen(jac,"wb");
		
		/* output of the preconditioned gradient */
		for (i=1;i<=NX;i=i+IDX){
		for (j=1;j<=NY;j=j+IDY){
			/*fwrite(&waveconv[j][i],sizeof(float),1,FP4);*/
			fwrite(&gradp[j][i],sizeof(float),1,FP4);
		}
		}
		
		fclose(FP4);
		
		MPI_Barrier(MPI_COMM_WORLD);
		
		/* merge gradient file */ 
		sprintf(jac,"%s_p.old",JACOBIAN);
		if (MYID==0) mergemod(jac,3);
	}
	
	/* ===================================================================================================================================================== */
	/* ===================================================== GRADIENT Zs ================================================================================== */
	/* ===================================================================================================================================================== */
	
288
	if((INVMAT==0)&&(!ACOUSTIC)){
Tilman Steinweg's avatar
Tilman Steinweg committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
			
		/* Preconditioning of the gradient */
		/* ------------------------------- */
		
		/* apply taper on the gradient */
		/* --------------------------- */
		if (SWS_TAPER_GRAD_VERT){   /*vertical gradient taper is applied*/
		taper_grad(waveconv_u,taper_coeff,srcpos,nsrc,recpos,ntr_glob,1);}
		
		if (SWS_TAPER_GRAD_HOR){   /*horizontal gradient taper is applied*/
		taper_grad(waveconv_u,taper_coeff,srcpos,nsrc,recpos,ntr_glob,2);}
		
		if (SWS_TAPER_GRAD_SOURCES){   /*cylindrical taper around sources is applied*/
		taper_grad(waveconv_u,taper_coeff,srcpos,nsrc,recpos,ntr_glob,3);}
		
		if (SWS_TAPER_FILE){   /* read taper from BIN-File*/                          
		taper_grad(waveconv_u,taper_coeff,srcpos,nsrc,recpos,ntr_glob,5);}
		
		/* save gradient */
		sprintf(jac,"%s_g_u.old.%i.%i",JACOBIAN,POS[1],POS[2]);
		FP3=fopen(jac,"wb");
		
		for (i=1;i<=NX;i=i+IDX){
		for (j=1;j<=NY;j=j+IDY){
			fwrite(&waveconv_u[j][i],sizeof(float),1,FP3);
		}
		}
			
		fclose(FP3);
		
		MPI_Barrier(MPI_COMM_WORLD);
			
		/* merge gradient file */ 
		sprintf(jac,"%s_g_u.old",JACOBIAN);
		if (MYID==0) mergemod(jac,3);
		
		/* Normalize gradient to maximum value */
		/*norm(waveconv_u,iter,2);*/
		
		/* apply spatial wavelength filter */
		if(SPATFILTER==1){
330
			if (MYID==0&&VERBOSE){
Tilman Steinweg's avatar
Tilman Steinweg committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
			fprintf(FP,"\n Spatial filter is applied to gradient (written by PE %d)\n",MYID);}
		spat_filt(waveconv_u,iter,2);}
		
		/* apply 2D-Gaussian filter*/
		if(GRAD_FILTER==1){smooth(waveconv_u,2,1,Vs_avg,FC);}
		
		/* output of the preconditioned gradient */
		for (i=1;i<=NX;i=i+IDX){
		for (j=1;j<=NY;j=j+IDY){
			waveconv_u[j][i] = C_vs * waveconv_u[j][i];
			gradp_u[j][i]=waveconv_u[j][i];
		}
		}
		
		/* save gradient for output as inversion result */
		if(iter==nfstart_jac){
			sprintf(jac,"%s_p_u_it%d.old.%i.%i",JACOBIAN,iter,POS[1],POS[2]);
			FP3=fopen(jac,"wb");
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				fwrite(&waveconv_u[j][i],sizeof(float),1,FP3);
			}
			}
			
			fclose(FP3);
			
			MPI_Barrier(MPI_COMM_WORLD);
			
			/* merge gradient file */ 
			sprintf(jac,"%s_p_u_it%d.old",JACOBIAN,iter);
			if (MYID==0) mergemod(jac,3);
			MPI_Barrier(MPI_COMM_WORLD);
			sprintf(jac,"%s_p_u_it%d.old.%i.%i",JACOBIAN,iter,POS[1],POS[2]);
			remove(jac);
		}
		
		/* calculate conjugate gradient direction, if iter > 1 (after Mora 1987) */
		/* --------------------------------------------------------------------- */
		
371
		if((iter>1)&&(use_conjugate_1)){
Tilman Steinweg's avatar
Tilman Steinweg committed
372 373 374 375
			
			sprintf(jac,"%s_p_u.old.%i.%i",JACOBIAN,POS[1],POS[2]);
			FP6=fopen(jac,"rb");
			
376
			if((iter>2)&&(use_conjugate_2)){
Tilman Steinweg's avatar
Tilman Steinweg committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
				sprintf(jac2,"%s_c_u.old.%i.%i",JACOBIAN,POS[1],POS[2]);
				FP5=fopen(jac2,"rb");
			}
			
				/* apply scalar product to obtain the coefficient beta */
			betaz = 0.0;
			betan = 0.0;
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				
				fread(&gradplastiter,sizeof(float),1,FP6);
				/*if(gradglastiter==gradg[j][i]) err("TEST1");*/
				/*if (MYID==10)  printf("TEST beta (MYID=%d) bei (j,i)=(%i,%i): gradg(k-1) = %e, gradg(k) = %e\n",MYID,j,i,gradglastiter,gradg[j][i]);*/
				
				/*
				betaz += (1e5*gradp[j][i]) * ( (1e5*gradg[j][i]) - (1e5*gradglastiter) );
				betan += (1e5*gradplastiter) * (1e5*gradglastiter);
				*/
				
				/* Polak and Ribiere */
				/*betaz += (gradp_u[j][i]) * ( (gradg_u[j][i]) - (gradglastiter) );
				betan += (gradplastiter) * (gradglastiter);*/
				
				/* Polak and Ribiere */
				betaz += (gradp_u[j][i]) * ( (gradp_u[j][i]) - (gradplastiter) );
				betan += (gradplastiter) * (gradplastiter);
				
				/* Fletcher and Reeves */
				/*betaz += (gradp[j][i]) * (gradg[j][i]);
				betan += (gradplastiter) * (gradglastiter);*/
				
			}
			}
			
			/*printf("TEST: vor exchange (MYID=%d): beta = betaz/betan = %e/%e = %e\n",MYID,betaz,betan,betaz/betan);*/
			
			/*betaz = exchange_L2(betaz,1,1);
			betan = exchange_L2(betan,1,1);*/
			
			betar = 0.0;
			MPI_Allreduce(&betaz,&betar,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
			betaz = betar;
			
			betar = 0.0;
			MPI_Allreduce(&betan,&betar,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
			betan = betar;
			
// 			beta = betaz/betan;
			beta = 0.0f;
			if(betan !=0.0f) beta = betaz/betan;
			
			/* direction reset */
			if(beta<0.0){beta = 0.0;}
			
			/*betaVs = beta;*/
432
			//printf("\n\nTEST: nach exchange (MYID=%d): beta = %e / %e = %e\n",MYID,betaz,betan,beta);
Tilman Steinweg's avatar
Tilman Steinweg committed
433 434 435 436 437 438 439 440 441 442 443
			
			fseek(FP6,0,SEEK_SET);
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				
				if(iter==2){
					fread(&gradplastiter,sizeof(float),1,FP6);
					waveconv_u[j][i] = gradp_u[j][i] + gradplastiter * beta;
				}
				
444
				if((iter>2)&&(use_conjugate_2)){
Tilman Steinweg's avatar
Tilman Steinweg committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
					fread(&gradclastiter,sizeof(float),1,FP5);
					waveconv_u[j][i] = gradp_u[j][i] + gradclastiter * beta;
				}
				
				
				/*if (iter >= 2){
					if (isnan(waveconv_u[j][i]) || isinf(waveconv_u[j][i])){
						sum = 0.0;
						h = 0;
						for (ii=-1;ii<=1;ii++){
						for (jj=-1;jj<=1;jj++){
							if (isnan(waveconv_u[j+jj][i+ii]) || isinf(waveconv_u[j+jj][i+ii])) continue;
							sum += waveconv_u[j+jj][i+ii];
							h++;
						}
						}
						if (h>0) waveconv_u[j][i] = sum / h;
						else waveconv_u[j][i] = 0.0;
					}
				}*/
			}
			}
			
			fclose(FP6);
			
470
			if((iter>2)&&(use_conjugate_2)){fclose(FP5);}
Tilman Steinweg's avatar
Tilman Steinweg committed
471 472 473
		}
		
		/* output of the conjugate gradient */
474
		if((iter>1)&&(use_conjugate_1)){
Tilman Steinweg's avatar
Tilman Steinweg committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
			sprintf(jac2,"%s_c_u.old.%i.%i",JACOBIAN,POS[1],POS[2]);
			FP5=fopen(jac2,"wb");
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				fwrite(&waveconv_u[j][i],sizeof(float),1,FP5);
			}
			}
			
			fclose(FP5);
			
			MPI_Barrier(MPI_COMM_WORLD);
			
			/* merge gradient file */ 
			sprintf(jac2,"%s_c_u.old",JACOBIAN);
			if (MYID==0) mergemod(jac2,3);  
		}
		
		sprintf(jac,"%s_p_u.old.%i.%i",JACOBIAN,POS[1],POS[2]);
		FP4=fopen(jac,"wb");
		
		/* output of the preconditioned gradient */
		for (i=1;i<=NX;i=i+IDX){
		for (j=1;j<=NY;j=j+IDY){
			/*fwrite(&waveconv_u[j][i],sizeof(float),1,FP4);*/
			fwrite(&gradp_u[j][i],sizeof(float),1,FP4);
		}
		}
		
		fclose(FP4);
		
		MPI_Barrier(MPI_COMM_WORLD);
		
		/* merge gradient file */ 
		sprintf(jac,"%s_p_u.old",JACOBIAN);
		if (MYID==0) mergemod(jac,3);
		
	}
	
	/* ===================================================================================================================================================== */
	/* ===================================================== GRADIENT rho ================================================================================== */
	/* ===================================================================================================================================================== */
	
518
	if((INVMAT==0)){
Tilman Steinweg's avatar
Tilman Steinweg committed
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
			
		/* Preconditioning of the gradient */
		/* ------------------------------- */
		if (SWS_TAPER_GRAD_VERT){   /*vertical gradient taper is applied*/
		taper_grad(waveconv_rho,taper_coeff,srcpos,nsrc,recpos,ntr_glob,1);}
		
		if (SWS_TAPER_GRAD_HOR){   /*horizontal gradient taper is applied*/
		taper_grad(waveconv_rho,taper_coeff,srcpos,nsrc,recpos,ntr_glob,2);}
		
		if (SWS_TAPER_GRAD_SOURCES){   /*cylindrical taper around sources is applied*/
		taper_grad(waveconv_rho,taper_coeff,srcpos,nsrc,recpos,ntr_glob,3);}
		
		if (SWS_TAPER_FILE){   /* read taper from BIN-File*/                          
		taper_grad(waveconv_rho,taper_coeff,srcpos,nsrc,recpos,ntr_glob,6);}
		
		/* save gradient */
		sprintf(jac,"%s_g_rho.old.%i.%i",JACOBIAN,POS[1],POS[2]);
		FP3=fopen(jac,"wb");
		
		for (i=1;i<=NX;i=i+IDX){
		for (j=1;j<=NY;j=j+IDY){
			fwrite(&waveconv_rho[j][i],sizeof(float),1,FP3);
		}
		}
			
		fclose(FP3);
		
		MPI_Barrier(MPI_COMM_WORLD);
			
		/* merge gradient file */ 
		sprintf(jac,"%s_g_rho.old",JACOBIAN);
		if (MYID==0) mergemod(jac,3);
		
		/* Normalize gradient to maximum value */
		/*norm(waveconv_rho,iter,3);*/
		
		/* apply spatial wavelength filter */
		if(SPATFILTER==1){
557
			if (MYID==0&&VERBOSE){
Tilman Steinweg's avatar
Tilman Steinweg committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
			fprintf(FP,"\n Spatial filter is applied to gradient (written by PE %d)\n",MYID);}
		spat_filt(waveconv_rho,iter,3);}
		
		/* apply 2D-Gaussian filter*/
		if(GRAD_FILTER==1){smooth(waveconv_rho,3,1,Vs_avg,FC);}
		
		/* output of the preconditioned gradient */
		for (i=1;i<=NX;i=i+IDX){
		for (j=1;j<=NY;j=j+IDY){
		waveconv_rho[j][i] = C_rho * waveconv_rho[j][i];
			gradp_rho[j][i]=waveconv_rho[j][i];
		}
		}
		
		/* save gradient for output as inversion result */
		if(iter==nfstart_jac){
			sprintf(jac,"%s_p_rho_it%d.old.%i.%i",JACOBIAN,iter,POS[1],POS[2]);
			FP3=fopen(jac,"wb");
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				fwrite(&waveconv_rho[j][i],sizeof(float),1,FP3);
			}
			}
			
			fclose(FP3);
			
			MPI_Barrier(MPI_COMM_WORLD);
			
			/* merge gradient file */ 
			sprintf(jac,"%s_p_rho_it%d.old",JACOBIAN,iter);
			if (MYID==0) mergemod(jac,3);
			MPI_Barrier(MPI_COMM_WORLD);
			sprintf(jac,"%s_p_rho_it%d.old.%i.%i",JACOBIAN,iter,POS[1],POS[2]);
			remove(jac);
		}
		
		/* calculate conjugate gradient direction, if iter > 1 (after Mora 1987) */
		/* --------------------------------------------------------------------- */
		
598
		if((iter>1)&&(use_conjugate_1)){
Tilman Steinweg's avatar
Tilman Steinweg committed
599 600 601 602
			
			sprintf(jac,"%s_p_rho.old.%i.%i",JACOBIAN,POS[1],POS[2]);
			FP6=fopen(jac,"rb");
			
603
			if((iter>2)&&(use_conjugate_2)){
Tilman Steinweg's avatar
Tilman Steinweg committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
				sprintf(jac2,"%s_c_rho.old.%i.%i",JACOBIAN,POS[1],POS[2]);
				FP5=fopen(jac2,"rb");
			}
			
			/* apply scalar product to obtain the coefficient beta */
			betaz = 0.0;
			betan = 0.0;
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				
				fread(&gradplastiter,sizeof(float),1,FP6);
				
				/*if(gradglastiter==gradg[j][i]) err("TEST1");*/
				/*if (MYID==10)  printf("TEST beta (MYID=%d) bei (j,i)=(%i,%i): gradg(k-1) = %e, gradg(k) = %e\n",MYID,j,i,gradglastiter,gradg[j][i]);*/
				
				/*
				betaz += (1e5*gradp[j][i]) * ( (1e5*gradg[j][i]) - (1e5*gradglastiter) );
				betan += (1e5*gradplastiter) * (1e5*gradglastiter);
				*/
				
				/* Polak and Ribiere */
				/*betaz += (gradp_rho[j][i]) * ( (gradg_rho[j][i]) - (gradglastiter) );
				betan += (gradplastiter) * (gradglastiter);*/
				
				/* Polak and Ribiere */
				betaz += (gradp_rho[j][i]) * ( (gradp_rho[j][i]) - (gradplastiter) );
				betan += (gradplastiter) * (gradplastiter);
				
				/* Fletcher and Reeves */
				/*betaz += (gradp[j][i]) * (gradg[j][i]);
				betan += (gradplastiter) * (gradglastiter);*/
				
			}
			}
			
			/*printf("TEST: vor exchange (MYID=%d): beta = betaz/betan = %e/%e = %e\n",MYID,betaz,betan,betaz/betan);*/
			
			/*betaz = exchange_L2(betaz,1,1);
			betan = exchange_L2(betan,1,1);*/
			
			betar = 0.0;
			MPI_Allreduce(&betaz,&betar,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
			betaz = betar;
			
			betar = 0.0;
			MPI_Allreduce(&betan,&betar,1,MPI_FLOAT,MPI_SUM,MPI_COMM_WORLD);
			betan = betar;
			
// 			beta = betaz/betan;
			beta = 0.0f;
			if(betan !=0.0f) beta = betaz/betan;
			
			/* direction reset */
			if(beta<0.0){beta = 0.0;}

			/*betarho = beta;*/
660
			//printf("\n\nTEST: nach exchange (MYID=%d): beta = %e / %e = %e\n",MYID,betaz,betan,beta);
Tilman Steinweg's avatar
Tilman Steinweg committed
661 662 663 664 665 666 667 668 669 670 671
			
			fseek(FP6,0,SEEK_SET);
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				
				if(iter==2){
					fread(&gradplastiter,sizeof(float),1,FP6);
					waveconv_rho[j][i] = gradp_rho[j][i] + gradplastiter * beta;
				}
				
672
				if((iter>2)&&(use_conjugate_2)){
Tilman Steinweg's avatar
Tilman Steinweg committed
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
					fread(&gradclastiter,sizeof(float),1,FP5);
					waveconv_rho[j][i] = gradp_rho[j][i] + gradclastiter * beta;
				}
				
				/*if (iter >= 2){
					if (isnan(waveconv_u[j][i]) || isinf(waveconv_u[j][i])){
						sum = 0.0;
						h = 0;
						for (ii=-1;ii<=1;ii++){
						for (jj=-1;jj<=1;jj++){
							if (isnan(waveconv_rho[j+jj][i+ii]) || isinf(waveconv_rho[j+jj][i+ii])) continue;
							sum += waveconv_rho[j+jj][i+ii];
							h++;
						}
						}
						if (h>0) waveconv_rho[j][i] = sum / h;
						else waveconv_rho[j][i] = 0.0;
					}
				}*/
			}
			}
			
			fclose(FP6);
			
697
			if((iter>2)&&(use_conjugate_2)){fclose(FP5);}
Tilman Steinweg's avatar
Tilman Steinweg committed
698 699 700
		}
		
		/* output of the conjugate gradient */
701
		if((iter>1)&&(use_conjugate_1)){
Tilman Steinweg's avatar
Tilman Steinweg committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
			sprintf(jac2,"%s_c_rho.old.%i.%i",JACOBIAN,POS[1],POS[2]);
			FP5=fopen(jac2,"wb");
			
			for (i=1;i<=NX;i=i+IDX){
			for (j=1;j<=NY;j=j+IDY){
				fwrite(&waveconv_rho[j][i],sizeof(float),1,FP5);
			}
			}
			
			fclose(FP5);
			
			MPI_Barrier(MPI_COMM_WORLD);
			
			/* merge gradient file */ 
			sprintf(jac2,"%s_c_rho.old",JACOBIAN);
			if (MYID==0) mergemod(jac2,3);  
		}
		
		sprintf(jac,"%s_p_rho.old.%i.%i",JACOBIAN,POS[1],POS[2]);
		FP4=fopen(jac,"wb");
		
		/* output of the preconditioned gradient */
		for (i=1;i<=NX;i=i+IDX){
		for (j=1;j<=NY;j=j+IDY){
			/*fwrite(&waveconv_rho[j][i],sizeof(float),1,FP4);*/
			fwrite(&gradp_rho[j][i],sizeof(float),1,FP4);
		}
		}
		
		fclose(FP4);
		
		MPI_Barrier(MPI_COMM_WORLD);
		
		/* merge gradient file */ 
		sprintf(jac,"%s_p_rho.old",JACOBIAN);
		if (MYID==0) mergemod(jac,3);
	}
}