My Project
programmer's documentation
Loading...
Searching...
No Matches
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 */
const cs_field_t *f = cs_field_by_id(f_id);
/* local number of mesh cells */
const cs_lnum_t n_cells = cs_glob_mesh->n_cells;
/* mesh quantities */
double cs_real_t
Floating-point value.
Definition cs_defs.h:302
int cs_lnum_t
local mesh entity id
Definition cs_defs.h:298
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_t * cs_glob_mesh
cs_mesh_quantities_t * cs_glob_mesh_quantities
Field descriptor.
Definition cs_field.h:124
cs_real_t * cell_vol
Definition cs_mesh_quantities.h:93
cs_lnum_t n_cells
Definition cs_mesh.h:73

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;
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
int cs_field_key_id(const char *name)
Return an id associated with a given key name.
Definition cs_field.c:2490

Function body

Map required fields

/* velocity */
const cs_real_3_t *cvar_vel = (const cs_real_3_t *)(CS_F_(vel)->val);
cs_real_t cs_real_3_t[3]
vector of 3 floating-point values
Definition cs_defs.h:315
@ vel
Definition cs_field_pointer.h:68
#define CS_F_(e)
Macro used to return a field pointer by its enumerated value.
Definition cs_field_pointer.h:51

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 */
@ CS_DOUBLE
Definition cs_defs.h:265
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_real_t tot_vol
Definition cs_mesh_quantities.h:131

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;
}