Changeset 25496
- Timestamp:
- 08/27/20 18:46:48 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/branches/trunk-larour-SLPS2020/src/c/main/issm_post.cpp
r25495 r25496 156 156 int* xsize=NULL; 157 157 158 IssmDouble** meanx=NULL; 159 IssmDouble** meanx2=NULL; 160 int* meantype=NULL; 161 int* meansize=NULL; 162 158 163 /*Retrieve parameters:*/ 159 164 parameters->FindParam(&nsamples,QmuNsampleEnum); … … 175 180 xtype=xNew<int>(nfields*nsteps); 176 181 xsize=xNew<int>(nfields*nsteps); 182 183 meantype=xNew<int>(nfields); 184 meansize=xNew<int>(nfields); 185 meanx=xNew<IssmDouble*>(nfields); 186 meanx2=xNew<IssmDouble*>(nfields); 177 187 178 188 /*Start opening files:*/ … … 247 257 } 248 258 } 259 260 /*Deal with time mean: */ 261 for (int f=0;f<nfields;f++){ 262 char* field=fields[f]; 263 fseek(fid,0,SEEK_SET); 264 meantype[f]=readdata(&doublemat, &doublematsize, &scalar, fid,field,steps[0]); 265 if(i==(lower_row+1)){ 266 if(meantype[f]==1){ 267 meanx[f]=xNewZeroInit<IssmDouble>(1); 268 meanx2[f]=xNewZeroInit<IssmDouble>(1); 269 meansize[f]=1; 270 } 271 else{ 272 meanx[f]=xNewZeroInit<IssmDouble>(doublematsize); 273 meanx2[f]=xNewZeroInit<IssmDouble>(doublematsize); 274 meansize[f]=doublematsize; 275 } 276 } 277 fseek(fid,0,SEEK_SET); 278 if(meantype[f]==1){ 279 IssmDouble sc=0; 280 IssmDouble sc2=0; 281 for(int j=0;j<nsteps;j++){ 282 readdata(&doublemat, &doublematsize, &scalar, fid,field,steps[j]); 283 sc+=scalar/nsteps; 284 } 285 sc2+=pow(sc,2.0); 286 *meanx[f]+=sc; 287 *meanx2[f]+=sc2; 288 } 289 else{ 290 IssmDouble* sc=meanx[f]; 291 IssmDouble* sc2=meanx2[f]; 292 IssmDouble* timemean=xNewZeroInit<IssmDouble>(doublematsize); 293 IssmDouble* timemean2=xNewZeroInit<IssmDouble>(doublematsize); 294 295 for(int j=0;j<nsteps;j++){ 296 readdata(&doublemat, &doublematsize, &scalar, fid,field,steps[j]); 297 for (int k=0;k<doublematsize;k++){ 298 timemean[k]+=doublemat[k]/nsteps; 299 } 300 } 301 for (int k=0;k<doublematsize;k++){ 302 timemean2[k]=pow(timemean[k],2.0); 303 } 304 for (int k=0;k<doublematsize;k++){ 305 sc[k]+=timemean[k]; 306 sc2[k]+=timemean2[k]; 307 } 308 309 } 310 311 } 249 312 fclose(fid); 250 313 … … 258 321 *cpu0 and then compute statistics:*/ 259 322 for (int f=0;f<nfields;f++){ 260 261 IssmDouble sumscalar_alltimes=0;262 IssmDouble sumscalar2_alltimes=0;263 IssmDouble* sumarray_alltimes=NULL;264 IssmDouble* sumarray2_alltimes=NULL;265 266 323 int counter0=f*nsteps+0; 267 324 if (xtype[counter0]==1){ /*deal with scalars {{{*/ … … 293 350 } 294 351 295 /*We are also looking for the mean and stddev over all time steps,296 *so accumulate sums again:*/297 sumscalar_alltimes+=sumscalar;298 sumscalar2_alltimes+=sumscalar2;299 }300 /*Build averae and standard deviation for all time steps:*/301 mean=sumscalar_alltimes/nsamples/nsteps;302 stddev=sqrt(sumscalar2_alltimes/nsamples/nsteps-pow(mean,2.0));303 /*add to results at step 1:*/304 if(my_rank==0){305 char fieldname[1000];306 307 sprintf(fieldname,"%s%s",fields[f],"TimeMean");308 results->AddResult(new GenericExternalResult<IssmDouble>(results->Size()+1,fieldname,mean,1,0));309 sprintf(fieldname,"%s%s",fields[f],"TimeStddev");310 results->AddResult(new GenericExternalResult<IssmDouble>(results->Size()+1,fieldname,stddev,1,0));311 352 } 312 353 } /*}}}*/ … … 314 355 315 356 int size=xsize[counter0]; 316 sumarray_alltimes=xNewZeroInit<IssmDouble>(size);317 sumarray2_alltimes=xNewZeroInit<IssmDouble>(size);318 357 319 358 IssmDouble* mean=xNew<IssmDouble>(size); … … 349 388 results->AddResult(new GenericExternalResult<IssmPDouble*>(results->Size()+1,fieldname,stddev,size,1,j+1,0)); 350 389 } 351 352 /*We want mean and stddev across time too, so accumulate through time: */ 353 for (int k=0;k<size;k++){ 354 sumarray_alltimes[k]+=sumx[k]; 355 sumarray2_alltimes[k]+=sumx2[k]; 356 } 357 } 358 359 /*build time mean and stddev:*/ 390 } 391 } /*}}}*/ 392 } 393 /*Do the same but for the time mean:*/ 394 for (int f=0;f<nfields;f++){ 395 if (meantype[f]==1){ /*deal with scalars {{{*/ 396 IssmDouble mean,stddev; 397 398 /*we are broadcasting doubles:*/ 399 IssmDouble scalar=*meanx[f]; 400 IssmDouble scalar2=*meanx2[f]; 401 IssmDouble sumscalar; 402 IssmDouble sumscalar2; 403 404 ISSM_MPI_Reduce(&scalar,&sumscalar,1,ISSM_MPI_PDOUBLE,ISSM_MPI_SUM,0,IssmComm::GetComm()); 405 ISSM_MPI_Reduce(&scalar2,&sumscalar2,1,ISSM_MPI_PDOUBLE,ISSM_MPI_SUM,0,IssmComm::GetComm()); 406 /*Build average and standard deviation. For standard deviation, use the 407 *following formula: sigma^2=E(x^2)-mu^2:*/ 408 mean=sumscalar/nsamples; 409 stddev=sqrt(sumscalar2/nsamples-pow(mean,2.0)); 410 411 /*add to results:*/ 412 if(my_rank==0){ 413 char fieldname[1000]; 414 415 sprintf(fieldname,"%s%s",fields[f],"TimeMean"); 416 results->AddResult(new GenericExternalResult<IssmDouble>(results->Size()+1,fieldname,mean,1,0)); 417 sprintf(fieldname,"%s%s",fields[f],"TimeStddev"); 418 results->AddResult(new GenericExternalResult<IssmDouble>(results->Size()+1,fieldname,stddev,1,0)); 419 } 420 } /*}}}*/ 421 else{ /*deal with arrays:{{{*/ 422 423 int size=meansize[f]; 424 IssmDouble* mean=xNew<IssmDouble>(size); 425 IssmDouble* stddev=xNew<IssmDouble>(size); 426 427 /*we are broadcasting double arrays:*/ 428 x=meanx[f]; 429 x2=meanx2[f]; 430 431 IssmDouble* sumx=xNew<IssmDouble>(size); 432 IssmDouble* sumx2=xNew<IssmDouble>(size); 433 434 ISSM_MPI_Reduce(x,sumx,size,ISSM_MPI_PDOUBLE,ISSM_MPI_SUM,0,IssmComm::GetComm()); 435 ISSM_MPI_Reduce(x2,sumx2,size,ISSM_MPI_PDOUBLE,ISSM_MPI_SUM,0,IssmComm::GetComm()); 436 437 /*Build average and standard deviation. For standard deviation, use the 438 *following formula: sigma^2=E(x^2)-mu^2:*/ 360 439 for (int k=0;k<size;k++){ 361 mean[k]=sum array_alltimes[k]/nsamples/nsteps;362 stddev[k]=sqrt(sum array2_alltimes[k]/nsamples/nsteps-pow(mean[k],2.0));363 } 364 365 /*add to results at step 1:*/440 mean[k]=sumx[k]/nsamples; 441 stddev[k]=sqrt(sumx2[k]/nsamples-pow(mean[k],2.0)); 442 } 443 444 /*add to results:*/ 366 445 if(my_rank==0){ 367 446 char fieldname[1000]; … … 373 452 } 374 453 } /*}}}*/ 375 376 } 454 } 455 377 456 378 457 } /*}}}*/
Note:
See TracChangeset
for help on using the changeset viewer.