Debian Codegraph @

mex/sources/heterogeneity/utils/pack_output.f08

raw
1 ! Copyright © 2025-2026 Dynare Team
2 !
3 ! This file is part of Dynare.
4 !
5 ! Dynare is free software: you can redistribute it and/or modify it under the terms of the
6 ! GNU General Public License as published by the Free Software Foundation, either version 3 of
7 ! the License, or (at your option) any later version.
8 !
9 ! Dynare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 ! even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ! GNU General Public License for more details.
12 !
13 ! You should have received a copy of the GNU General Public License along with Dynare. If not,
14 ! see <https://www.gnu.org/licenses/>.
15 !
16 ! Original author: Normann Rion <[email protected]>
17 !
18 ! Pack calibration output into MATLAB struct for return from MEX function
19
20 module pack_output
21 use iso_c_binding
22 use matlab_mex
23 use calibration_types
24 implicit none (type, external)
25
26 contains
27
28 ! Pack calibration_output into MATLAB struct
29 !
30 ! Arguments:
31 ! output [in] : Fortran calibration output structure
32 ! input [in] : Fortran calibration input (for metadata)
33 ! output_mx [out] : MATLAB struct pointer
34 !
35 subroutine pack_calibration_output(output, input, output_mx)
36 type(calibration_output), intent(in) :: output
37 type(calibration_input), intent(in) :: input
38 type(c_ptr), intent(out) :: output_mx
39
40 ! MATLAB field names
41 character(len=17), dimension(8) :: field_names = [ &
42 character(len=17) :: 'converged', 'iterations', 'residual_norm', 'params', &
43 'param_names', 'time_iteration', 'distribution', 'aggregates']
44
45 ! Sub-struct field names
46 character(len=13), dimension(4) :: ti_fields = [ &
47 character(len=13) :: 'converged', 'iterations', 'residual_norm', 'policies']
48 character(len=13), dimension(4) :: dist_fields = [ &
49 character(len=13) :: 'converged', 'iterations', 'residual_norm', 'hist']
50 character(len=9), dimension(2) :: agg_fields = [ &
51 character(len=9) :: 'Ix', 'residuals']
52
53 ! MATLAB pointers
54 type(c_ptr) :: ti_struct, dist_struct, agg_struct
55 type(c_ptr) :: field_ptr, names_cell
56 type(c_ptr) :: str_ptr
57 real(real64), pointer, contiguous :: temp_real(:), temp_real_2d(:,:)
58 integer(int32) :: i, n_het_endo, N_sp, N_om, n_agg_endo, n_free_parameters
59 character(len=256), target :: str_buffer
60
61 ! Extract dimensions
62 n_free_parameters = input%dims%n_free_parameters
63 n_het_endo = input%dims%n_het_endo
64 N_sp = input%dims%N_sp
65 N_om = input%dims%N_om
66 n_agg_endo = input%dims%n_agg_endo
67
68 ! Create main output struct using the wrapper (it handles the conversion)
69 output_mx = mxCreateStructMatrix(1_mwSize, 1_mwSize, field_names)
70 if (.not. c_associated(output_mx)) call mexErrMsgTxt("Failed to create output struct")
71
72 ! ================================================================
73 ! PART 1: Top-level convergence fields
74 ! ================================================================
75
76 ! converged (logical) - convert logical(4) to logical(mxLogical)
77 field_ptr = mxCreateLogicalScalar(merge(.true._mxLogical, .false._mxLogical, output%converged))
78 call mxSetField(output_mx, 1_mwIndex, 'converged', field_ptr)
79
80 ! iterations (int32)
81 field_ptr = mxCreateDoubleScalar(real(output%iterations, C_DOUBLE))
82 call mxSetField(output_mx, 1_mwIndex, 'iterations', field_ptr)
83
84 ! residual_norm (double)
85 field_ptr = mxCreateDoubleScalar(output%residual_norm)
86 call mxSetField(output_mx, 1_mwIndex, 'residual_norm', field_ptr)
87
88 ! ================================================================
89 ! PART 2: Calibrated parameters
90 ! ================================================================
91
92 ! params [n_free_parameters × 1]
93 if (n_free_parameters > 0 .and. allocated(output%params)) then
94 field_ptr = mxCreateDoubleMatrix(int(n_free_parameters, mwSize), 1_mwSize, mxREAL)
95 temp_real(1:n_free_parameters) => mxGetDoubles(field_ptr)
96 temp_real = output%params
97 else
98 field_ptr = mxCreateDoubleMatrix(0_mwSize, 0_mwSize, mxREAL)
99 end if
100 call mxSetField(output_mx, 1_mwIndex, 'params', field_ptr)
101
102 ! param_names {n_free_parameters × 1} cell array of strings
103 if (n_free_parameters > 0) then
104 names_cell = mxCreateCellMatrix(int(n_free_parameters, mwSize), 1_mwSize)
105 do i = 1, n_free_parameters
106 ! Convert Fortran string to C string (null-terminated)
107 str_buffer = trim(input%free_parameters_names(i)) // c_null_char
108 str_ptr = mxCreateString(str_buffer)
109 call mxSetCell(names_cell, int(i, mwIndex), str_ptr)
110 end do
111 call mxSetField(output_mx, 1_mwIndex, 'param_names', names_cell)
112 else
113 names_cell = mxCreateCellMatrix(0_mwSize, 0_mwSize)
114 call mxSetField(output_mx, 1_mwIndex, 'param_names', names_cell)
115 end if
116
117 ! ================================================================
118 ! PART 3: Time iteration sub-struct
119 ! ================================================================
120
121 ti_struct = mxCreateStructMatrix(1_mwSize, 1_mwSize, ti_fields)
122 if (.not. c_associated(ti_struct)) call mexErrMsgTxt("Failed to create time_iteration struct")
123
124 if (associated(output%ti_output)) then
125 ! converged
126 field_ptr = mxCreateLogicalScalar(merge(.true._mxLogical, .false._mxLogical, output%ti_output%converged))
127 call mxSetField(ti_struct, 1_mwIndex, 'converged', field_ptr)
128
129 ! iterations
130 field_ptr = mxCreateDoubleScalar(real(output%ti_output%iterations, C_DOUBLE))
131 call mxSetField(ti_struct, 1_mwIndex, 'iterations', field_ptr)
132
133 ! residual_norm
134 field_ptr = mxCreateDoubleScalar(output%ti_output%residual_norm)
135 call mxSetField(ti_struct, 1_mwIndex, 'residual_norm', field_ptr)
136
137 ! policies [n_het_endo × N_sp]
138 if (allocated(output%ti_output%policies_sp)) then
139 field_ptr = mxCreateDoubleMatrix(int(n_het_endo, mwSize), &
140 int(N_sp, mwSize), mxREAL)
141 temp_real_2d(1:n_het_endo, 1:N_sp) => mxGetDoubles(field_ptr)
142 temp_real_2d = output%ti_output%policies_sp
143 else
144 field_ptr = mxCreateDoubleMatrix(0_mwSize, 0_mwSize, mxREAL)
145 end if
146 call mxSetField(ti_struct, 1_mwIndex, 'policies', field_ptr)
147 else
148 ! Fill with defaults if not associated
149 field_ptr = mxCreateLogicalScalar(.false._mxLogical)
150 call mxSetField(ti_struct, 1_mwIndex, 'converged', field_ptr)
151 field_ptr = mxCreateDoubleScalar(0.0_C_DOUBLE)
152 call mxSetField(ti_struct, 1_mwIndex, 'iterations', field_ptr)
153 field_ptr = mxCreateDoubleScalar(0.0_C_DOUBLE)
154 call mxSetField(ti_struct, 1_mwIndex, 'residual_norm', field_ptr)
155 field_ptr = mxCreateDoubleMatrix(0_mwSize, 0_mwSize, mxREAL)
156 call mxSetField(ti_struct, 1_mwIndex, 'policies', field_ptr)
157 end if
158
159 call mxSetField(output_mx, 1_mwIndex, 'time_iteration', ti_struct)
160
161 ! ================================================================
162 ! PART 4: Distribution sub-struct
163 ! ================================================================
164
165 dist_struct = mxCreateStructMatrix(1_mwSize, 1_mwSize, dist_fields)
166 if (.not. c_associated(dist_struct)) call mexErrMsgTxt("Failed to create distribution struct")
167
168 if (associated(output%dist_output)) then
169 ! converged
170 field_ptr = mxCreateLogicalScalar(merge(.true._mxLogical, .false._mxLogical, output%dist_output%converged))
171 call mxSetField(dist_struct, 1_mwIndex, 'converged', field_ptr)
172
173 ! iterations
174 field_ptr = mxCreateDoubleScalar(real(output%dist_output%iterations, C_DOUBLE))
175 call mxSetField(dist_struct, 1_mwIndex, 'iterations', field_ptr)
176
177 ! residual_norm
178 field_ptr = mxCreateDoubleScalar(output%dist_output%residual_norm)
179 call mxSetField(dist_struct, 1_mwIndex, 'residual_norm', field_ptr)
180
181 ! hist [N_om × 1]
182 if (associated(output%dist_output%distribution)) then
183 field_ptr = mxCreateDoubleMatrix(int(N_om, mwSize), 1_mwSize, mxREAL)
184 temp_real(1:N_om) => mxGetDoubles(field_ptr)
185 temp_real = output%dist_output%distribution
186 else
187 field_ptr = mxCreateDoubleMatrix(0_mwSize, 0_mwSize, mxREAL)
188 end if
189 call mxSetField(dist_struct, 1_mwIndex, 'hist', field_ptr)
190 else
191 ! Fill with defaults if not associated
192 field_ptr = mxCreateLogicalScalar(.false._mxLogical)
193 call mxSetField(dist_struct, 1_mwIndex, 'converged', field_ptr)
194 field_ptr = mxCreateDoubleScalar(0.0_C_DOUBLE)
195 call mxSetField(dist_struct, 1_mwIndex, 'iterations', field_ptr)
196 field_ptr = mxCreateDoubleScalar(0.0_C_DOUBLE)
197 call mxSetField(dist_struct, 1_mwIndex, 'residual_norm', field_ptr)
198 field_ptr = mxCreateDoubleMatrix(0_mwSize, 0_mwSize, mxREAL)
199 call mxSetField(dist_struct, 1_mwIndex, 'hist', field_ptr)
200 end if
201
202 call mxSetField(output_mx, 1_mwIndex, 'distribution', dist_struct)
203
204 ! ================================================================
205 ! PART 5: Aggregates sub-struct
206 ! ================================================================
207
208 agg_struct = mxCreateStructMatrix(1_mwSize, 1_mwSize, agg_fields)
209 if (.not. c_associated(agg_struct)) call mexErrMsgTxt("Failed to create aggregates struct")
210
211 if (associated(output%agg_output)) then
212 ! Ix [n_het_endo × 1]
213 if (allocated(output%agg_output%Ix)) then
214 field_ptr = mxCreateDoubleMatrix(int(n_het_endo, mwSize), 1_mwSize, mxREAL)
215 temp_real(1:n_het_endo) => mxGetDoubles(field_ptr)
216 temp_real = output%agg_output%Ix
217 else
218 field_ptr = mxCreateDoubleMatrix(0_mwSize, 0_mwSize, mxREAL)
219 end if
220 call mxSetField(agg_struct, 1_mwIndex, 'Ix', field_ptr)
221
222 ! residuals [n_agg_endo × 1]
223 if (allocated(output%agg_output%residuals)) then
224 field_ptr = mxCreateDoubleMatrix(int(n_agg_endo, mwSize), 1_mwSize, mxREAL)
225 temp_real(1:n_agg_endo) => mxGetDoubles(field_ptr)
226 temp_real = output%agg_output%residuals
227 else
228 field_ptr = mxCreateDoubleMatrix(0_mwSize, 0_mwSize, mxREAL)
229 end if
230 call mxSetField(agg_struct, 1_mwIndex, 'residuals', field_ptr)
231 else
232 ! Fill with defaults if not associated
233 field_ptr = mxCreateDoubleMatrix(0_mwSize, 0_mwSize, mxREAL)
234 call mxSetField(agg_struct, 1_mwIndex, 'Ix', field_ptr)
235 field_ptr = mxCreateDoubleMatrix(0_mwSize, 0_mwSize, mxREAL)
236 call mxSetField(agg_struct, 1_mwIndex, 'residuals', field_ptr)
237 end if
238
239 call mxSetField(output_mx, 1_mwIndex, 'aggregates', agg_struct)
240
241 end subroutine pack_calibration_output
242
243 end module pack_output