Sorry, my first example is more abstract, for all cells of current thread or for entire domain if used with cs_parall_sum (second fragment is for particular zone). I hope the following will give you summary volume of "solid" zone and volume-ave temperature in units used by the solver (Celsius or Kelvin).
Code: Select all
cs_lnum_t iZoneCell,iCell;
cs_real_t zoneVlmSum,zoneTempSum,zoneTempAve;
/* Pointers required for calculation */
const cs_real_t *cellTemp=(const cs_real_t*)CS_F_(t)->val; /* Temperature array in current thread cells */
const cs_zone_t *zone=cs_volume_zone_by_name_try("solid"); /* Zone structure (for current thread) */
/* Get summary volume or any other zone cell statistic */
zoneVlmSum=0; /* Init volume */
zoneTempSum=0; /* Init Volume*Temp sum */
for (iZoneCell=0;iZoneCell<zone->n_elts;iZoneCell++) /* Zone cell loop */
{
iCell=zone->elt_ids[iZoneCell]; /* Cell index in current thread by index in selected zone */
zoneVlmSum=zoneVlmSum+cellVlm[iCell]; /* Zone volume sum */
zoneTempSum=zoneTempSum+cellTemp[iCell]*cellVlm[iCell]; /* Zone volume*temp sum */
};
cs_parall_sum(1,CS_REAL_TYPE,&zoneVlmSum); /* Zone volume for all threads */
cs_parall_sum(1,CS_REAL_TYPE,&zoneTempSum); /* Zone volume*temp sum volume for all threads */
zoneTempAve=zoneTempSum/zoneVlmSum; /* Volume-ave temperature in zone */
/* That's it, you can use zoneTempAve variable, don't forget to check units... */
I didn't check so you will probably need to debug but then it should work (I use such approach in my different "plugins" for Saturne).
Here we get temperature field for all thread cells and zone params structure. But, because we cycle through zone cells only, we get only zone statistics or other processing, not for all thread cells. There is
zone->elt_ids[iZoneCell] array in zone structure that gives cell indices in current thread that are related to selected zone (if we just use
iZoneCell counter we will get wrong cells).
I suggest to introduce function that gives non-zero or positive value and use it in divisor to avoid division by zero even if zone is void for some reason. Also, in practise it's very useful to check pointers to zones or fields and
bft_printf() errors if any - it simplifies case debugging.
Oops, there was an error, corrected.