Introduction
C user functions for the setting of a user solver. The cs_user_solver function allows one to replace the code_saturne solver by a solver of his own.
The following example shows the setting of a user solver which solves a heat equation with the finite volume method.
Activating the user solver
If the function cs_user_solver_set returns 1, the code_saturne solver is replaced by the solver defined in cs_user_solver .
int
{
return 1;
}
int cs_user_solver_set(void)
Set user solver.
Definition: cs_user_solver.c:75
User solver implementation
Variable declaration
The following variables are declared locally and will be used later in the code. Three pointers to cs_real_t are declared and will stand for the temperature at the time steps n and n-1, and for the analytical solution of the heat equation;
int i, iter, n_iter;
cs_real_t *
t = NULL, *t_old = NULL, *t_sol = NULL;
const char var_name[] = "temperature";
double cs_real_t
Floating-point value.
Definition: cs_defs.h:319
int cs_lnum_t
local mesh entity id
Definition: cs_defs.h:313
@ t
Definition: cs_field_pointer.h:92
struct _cs_restart_t cs_restart_t
Definition: cs_restart.h:95
struct _cs_time_plot_t cs_time_plot_t
Definition: cs_time_plot.h:48
double precision pi
value with 16 digits
Definition: cstnum.f90:48
real(c_double), pointer, save t0
reference temperature.
Definition: cstphy.f90:214
Initialization
Three arrays of size n
are created. Several constants are initialized, and the array t_old is initialized as follows :
x0 = 1.e30;
xL = -1.e30;
for (i = 0; i <
mesh->n_b_faces; i++) {
cs_real_t x_face = mesh_quantities->b_face_cog[3*i];
if (x_face < x0) x0 = x_face;
if (x_face > xL) xL = x_face;
}
tL = 0.;
r = 0.2;
n_iter = 100000;
for (i = 0; i < n; i++)
t_old[i] = sin(
pi*(0.5+i)/n);
#define BFT_MALLOC(_ptr, _ni, _type)
Allocate memory for _ni elements of type _type.
Definition: bft_mem.h:62
Restart creation
One can define a restart computation, as for the real code_saturne solver.
NULL,
var_name,
1,
1,
2,
t_old);
void cs_restart_destroy(cs_restart_t **restart)
Destroy structure associated with a restart file (and close the file).
Definition: cs_restart.c:2321
int cs_restart_read_section(cs_restart_t *restart, const char *sec_name, int location_id, int n_location_vals, cs_restart_val_type_t val_type, void *val)
Read a section from a restart file.
Definition: cs_restart.c:2794
cs_restart_t * cs_restart_create(const char *name, const char *path, cs_restart_mode_t mode)
Initialize a restart file.
Definition: cs_restart.c:2126
@ CS_RESTART_MODE_READ
Definition: cs_restart.h:76
Time monitoring
Monitoring points can equally be created in order to plot the evolution of the temperature over the time.
"probes_",
true,
-1.,
0,
1,
NULL,
NULL,
NULL);
cs_time_plot_t * cs_time_plot_init_probe(const char *plot_name, const char *file_prefix, cs_time_plot_format_t format, bool use_iteration, double flush_wtime, int n_buffer_steps, int n_probes, const int *probe_list, const cs_real_t probe_coords[], const char *probe_names[])
Definition: cs_time_plot.c:772
@ CS_TIME_PLOT_DAT
Definition: cs_time_plot.h:57
Heat equation solving
At each iteration, the new temperature is computed using the Finite Volume Method.
The boundary conditions at
and
are :
t_old is then updated and t_sol computed. Finally, the temperature at
and at the current iteration is plotted.
for (iter = 0; iter < n_iter; iter++) {
t[0] = t_old[0] + r*(t_old[1] - 3.*t_old[0] + 2.*
t0);
for (i = 1; i < n-1; i++)
t[i] = t_old[i] + r*(t_old[i+1] - 2.*t_old[i] + t_old[i-1]);
t[n-1] = t_old[n-1] + r*(2.*tL - 3.*t_old[n-1] + t_old[n-2]);
for (i = 0; i < n; i++)
for (i = 0; i < n; i++)
t_sol[i] = sin(
pi*(0.5+i)/n)*exp(-r*
pi*
pi*(iter+1)/(n*n));
iter,
-1.,
1,
}
void cs_time_plot_vals_write(cs_time_plot_t *p, int tn, double t, int n_vals, const cs_real_t vals[])
Definition: cs_time_plot.c:922
Checkpoint creation
A checkpoint can be created in order to restart the computation later on.
NULL,
var_name,
1,
1,
2,
void cs_restart_write_section(cs_restart_t *restart, const char *sec_name, int location_id, int n_location_vals, cs_restart_val_type_t val_type, const void *val)
Write a section to a restart file.
Definition: cs_restart.c:2837
@ CS_RESTART_MODE_WRITE
Definition: cs_restart.h:77
Post-processing
Finally, t and t_sol are postprocessed in order to be compared.
true);
var_name,
1,
false,
true,
NULL,
NULL,
NULL);
"solution",
1,
false,
true,
t_sol,
NULL,
NULL,
NULL);
void cs_post_write_var(int mesh_id, int writer_id, const char *var_name, int var_dim, bool interlace, bool use_parent, cs_datatype_t datatype, const void *cel_vals, const void *i_face_vals, const void *b_face_vals, const cs_time_step_t *ts)
Output a variable defined at cells or faces of a post-processing mesh using associated writers.
Definition: cs_post.c:5942
void cs_post_activate_writer(int writer_id, bool activate)
Force the "active" or "inactive" flag for a specific writer or for all writers for the current time s...
Definition: cs_post.c:5620
#define CS_POST_MESH_VOLUME
Definition: cs_post.h:79
#define CS_POST_WRITER_ALL_ASSOCIATED
Definition: cs_post.h:67
#define CS_POST_TYPE_cs_real_t
Definition: cs_post.h:98
Finalization
#define BFT_FREE(_ptr)
Free allocated memory.
Definition: bft_mem.h:101
void cs_time_plot_finalize(cs_time_plot_t **p)
Definition: cs_time_plot.c:881