CAMP 1.0.0
Chemistry Across Multiple Phases
Jacobian.h
Go to the documentation of this file.
1/* Copyright (C) 2021 Barcelona Supercomputing Center and University of
2 * Illinois at Urbana-Champaign
3 * SPDX-License-Identifier: MIT
4 *
5 * Header for the Jacobian structure and related functions
6 *
7 */
8/** \file
9 * \brief Header for the Jacobian structure and related functions
10 */
11#ifndef JACOBIAN_H_
12#define JACOBIAN_H_
13
14#include <math.h>
15#include <stdlib.h>
16
17// Flags for specifying production or loss elements
18#define JACOBIAN_PRODUCTION 0
19#define JACOBIAN_LOSS 1
20
21/* Registered elements for a column in the Jacobian */
22typedef struct {
23 unsigned int array_size; // Size of the array of flagged elements
24 unsigned int
25 number_of_elements; // Number of registered elements in the column
26 unsigned int
27 *row_ids; // Array of row ids for each registered element in the column
29
30/* Jacobian for solver species */
31typedef struct {
32 unsigned int num_spec; // Number of species
33 unsigned int num_elem; // Number of potentially non-zero Jacobian elements
34 unsigned int *col_ptrs; // Index of start/end of each column in data array
35 unsigned int *row_ids; // Row id of each Jacobian element in data array
36 long double
37 *production_partials; // Data array for productions rate partial derivs
38 long double *loss_partials; // Data array for loss rate partial derivs
39 JacobianColumnElements *elements; // Jacobian elements flagged for inclusion
40} Jacobian;
41
42/** \brief Initialize the Jacobian
43 *
44 * Sets up a sparse matrix (num_spec x num_spec) containing zero elements.
45 * Elements can be added using the \c jacobian_register_element function.
46 *
47 * \param jac Jacobian object
48 * \param num_spec Number of species
49 * \return Flag indicating whether the Jacobian was successfully initialized
50 * (0 = false; 1 = true)
51 */
52int jacobian_initialize_empty(Jacobian *jac, unsigned int num_spec);
53
54/** \brief Initialize the Jacobian
55 *
56 * \param jac Pointer to the Jacobian object
57 * \param num_spec Number of species
58 * \param jac_struct Dense matrix of flags indicating whether an element is
59 * (1) potentially non-zero or (0) not.
60 * \return Flag indicating whether the derivative was successfully initialized
61 * (0 = false; 1 = true)
62 */
63int jacobian_initialize(Jacobian *jac, unsigned int num_spec,
64 unsigned int **jac_struct);
65
66/** \brief Adds an element to the sparse matrix
67 *
68 * \param jac Jacobian object
69 * \param dep_id Dependent species index
70 * \param ind_id Independent species index
71 */
72void jacobian_register_element(Jacobian *jac, unsigned int dep_id,
73 unsigned int ind_id);
74
75/** \brief Builds the sparse matrix with the registered elements
76 *
77 * \return 1 on success, 0 otherwise
78 */
79unsigned int jacobian_build_matrix(Jacobian *jac);
80
81/** \brief Returns the number of elements in the Jacobian
82 *
83 * \param jac Jacobian object
84 * \return Number of Jacobian elements
85 */
87
88/** \brief Returns the value of a column pointer
89 *
90 * \param jac Jacobian object
91 * \param col_id Column index (0...number of columns)
92 * \return Column pointer value
93 */
94unsigned int jacobian_column_pointer_value(Jacobian jac, unsigned int col_id);
95
96/** \brief Returns the row for a given Jacobian element
97 *
98 * \param jac Jacobian object
99 * \param elem_id Jacobian element index (0...number of elements-1)
100 * \return Row index for given element
101 */
102unsigned int jacobian_row_index(Jacobian jac, unsigned int elem_id);
103
104/** \brief Get an element id in the Jacobian data arrays
105 *
106 * If the element is not included in the sparse matrix, -1 is returned.
107 *
108 * \param jac Jacobian object
109 * \param dep_id Dependent species index
110 * \param ind_id Independent species index
111 * \return Index of Jacobian element in the data array
112 */
113unsigned int jacobian_get_element_id(Jacobian jac, unsigned int dep_id,
114 unsigned int ind_id);
115
116/** \brief Reset the Jacobian
117 *
118 * \param jac Jacobian matrix
119 */
120void jacobian_reset(Jacobian jac);
121
122/** \brief Output the Jacobian
123 *
124 * \param jac Jacobian object
125 * \param dest_array Pointer to the array to save Jacobian data to
126 */
127void jacobian_output(Jacobian jac, double *dest_array);
128
129/** \brief Add a contribution to the Jacobian
130 *
131 * \param jac Jacobian object
132 * \param elem_id Index of the element to update in the data array
133 * \param prod_or_loss Flag indicating whether to update the (0) production or
134 * (1) loss elements
135 * \param jac_contribution Value to add to the Jacobian element
136 * (contributions to loss elements should be positive if
137 * the contribution increases the loss)
138 */
139void jacobian_add_value(Jacobian jac, unsigned int elem_id,
140 unsigned int prod_or_loss,
141 long double jac_contribution);
142
143/** \brief Prints the Jacobian structure
144 *
145 * \param jac Jacobian object
146 */
147void jacobian_print(Jacobian jac);
148
149/** \brief Free memory associated with a JacobianColumnElements
150 *
151 * \param column Jacobian column elements
152 */
154
155/** \brief Free memory associated with a Jacobian
156 *
157 * \param jac Jacobian object
158 */
159void jacobian_free(Jacobian *jac);
160
161#endif
unsigned int jacobian_column_pointer_value(Jacobian jac, unsigned int col_id)
Returns the value of a column pointer.
Definition Jacobian.c:192
void jacobian_free(Jacobian *jac)
Free memory associated with a Jacobian.
Definition Jacobian.c:281
unsigned int jacobian_build_matrix(Jacobian *jac)
Builds the sparse matrix with the registered elements.
Definition Jacobian.c:129
int jacobian_initialize_empty(Jacobian *jac, unsigned int num_spec)
Initialize the Jacobian.
Definition Jacobian.c:20
int jacobian_initialize(Jacobian *jac, unsigned int num_spec, unsigned int **jac_struct)
Initialize the Jacobian.
Definition Jacobian.c:46
unsigned int jacobian_number_of_elements(Jacobian jac)
Returns the number of elements in the Jacobian.
Definition Jacobian.c:190
unsigned int jacobian_get_element_id(Jacobian jac, unsigned int dep_id, unsigned int ind_id)
Get an element id in the Jacobian data arrays.
Definition Jacobian.c:200
void jacobian_column_elements_free(JacobianColumnElements *column)
Free memory associated with a JacobianColumnElements.
Definition Jacobian.c:274
void jacobian_add_value(Jacobian jac, unsigned int elem_id, unsigned int prod_or_loss, long double jac_contribution)
Add a contribution to the Jacobian.
Definition Jacobian.c:234
void jacobian_reset(Jacobian jac)
Reset the Jacobian.
Definition Jacobian.c:216
void jacobian_register_element(Jacobian *jac, unsigned int dep_id, unsigned int ind_id)
Adds an element to the sparse matrix.
Definition Jacobian.c:105
void jacobian_print(Jacobian jac)
Prints the Jacobian structure.
Definition Jacobian.c:243
void jacobian_output(Jacobian jac, double *dest_array)
Output the Jacobian.
Definition Jacobian.c:223
unsigned int jacobian_row_index(Jacobian jac, unsigned int elem_id)
Returns the row for a given Jacobian element.
Definition Jacobian.c:196
unsigned int number_of_elements
Definition Jacobian.h:25
unsigned int * row_ids
Definition Jacobian.h:27
unsigned int array_size
Definition Jacobian.h:23
JacobianColumnElements * elements
Definition Jacobian.h:39
unsigned int num_elem
Definition Jacobian.h:33
long double * production_partials
Definition Jacobian.h:37
unsigned int * row_ids
Definition Jacobian.h:35
unsigned int num_spec
Definition Jacobian.h:32
unsigned int * col_ptrs
Definition Jacobian.h:34
long double * loss_partials
Definition Jacobian.h:38