My Project
programmer's documentation
Examples of data settings for source terms with scalar in a channel

Additional right-hand side source terms for scalar equations (user scalars and specific physics scalars) with the cs_user_source_terms user-defined function.

Local variables and initialization

/* field structure */
/* local number of mesh cells */
const cs_lnum_t n_cells = cs_glob_mesh->n_cells;
/* mesh quantities */

Only apply to thermal scalar

/* scalar id */
const int key_sca = cs_field_key_id("scalar_id");
const int scalar_id = cs_field_get_key_int(f, key_sca);
if (scalar_id < 0 || scalar_id != cs_glob_thermal_model->iscalt)
return;

Function body

Map required fields

/* velocity */
const cs_real_3_t *cvar_vel = (const cs_real_3_t *)(CS_F_(vel)->val);

Compute bulk mean velocity

/* bulk mean velocity (x component) */
cs_real_t ubulk = 0;
for (cs_lnum_t i = 0; i < n_cells; i++)
ubulk += cvar_vel[i][0] * cell_f_vol[i];
cs_parall_sum(1, CS_DOUBLE, &ubulk); /* sum across processes if needed */

Compute source terms; we want to impose a total flux of 1 Watt.

/* Flux x Total surface / (rho Cp) */
cs_real_t tot_flux = 1.;
for (cs_lnum_t i = 0; i < n_cells; i++) {
st_imp[i] = 0.;
st_exp[i] = cell_f_vol[i] * cvar_vel[i][0] * tot_flux / ubulk;
}
cs_mesh_t::n_cells
cs_lnum_t n_cells
Definition: cs_mesh.h:73
f_id
void const int * f_id
Definition: cs_gui.h:292
cs_glob_mesh_quantities
cs_mesh_quantities_t * cs_glob_mesh_quantities
mesh::cell_f_vol
double precision, dimension(:), pointer cell_f_vol
Definition: mesh.f90:156
cs_real_3_t
cs_real_t cs_real_3_t[3]
vector of 3 floating-point values
Definition: cs_defs.h:315
cs_real_t
double cs_real_t
Floating-point value.
Definition: cs_defs.h:302
cs_parall_sum
static void cs_parall_sum(int n, cs_datatype_t datatype, void *val)
Sum values of a given datatype on all default communicator processes.
Definition: cs_parall.h:147
CS_DOUBLE
Definition: cs_defs.h:265
cs_glob_mesh
cs_mesh_t * cs_glob_mesh
cs_field_get_key_int
int cs_field_get_key_int(const cs_field_t *f, int key_id)
Return a integer value for a given key associated with a field.
Definition: cs_field.c:2976
iscalt
void cs_int_t cs_int_t * iscalt
Definition: cs_lagr_options.h:74
CS_F_
#define CS_F_(e)
Macro used to return a field pointer by its enumerated value.
Definition: cs_field_pointer.h:51
cs_lnum_t
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:298
cs_field_by_id
cs_field_t * cs_field_by_id(int id)
Return a pointer to a field based on its id.
Definition: cs_field.c:2307
cs_mesh_quantities_t::cell_vol
cs_real_t * cell_vol
Definition: cs_mesh_quantities.h:93
vel
void const cs_int_t *const const cs_int_t *const const cs_int_t *const const cs_int_t *const const cs_int_t *const const cs_int_t *const const cs_int_t *const const cs_int_t *const const cs_real_t *const const cs_real_t *const const cs_real_t const cs_real_t const cs_real_3_t vel[]
Definition: cs_divergence.h:64
cs_field_key_id
int cs_field_key_id(const char *name)
Return an id associated with a given key name.
Definition: cs_field.c:2490
cs_mesh_quantities_t::tot_vol
cs_real_t tot_vol
Definition: cs_mesh_quantities.h:131
cs_field_t
Field descriptor.
Definition: cs_field.h:124