SDDSlib
Loading...
Searching...
No Matches
sdds3dconvert.c
Go to the documentation of this file.
1/**
2 * @file sdds3dconvert.c
3 *
4 * @brief Converts 3D Vorpal output data into SDDS format compatible with sddscontour for plotting.
5 *
6 * This program converts 3D Vorpal output data into a format compatible
7 * with sddscontour for plotting variables such as Ex, Ey, Ez, Rho, Jx, Jy, Jz, etc.
8 * The input file contains 3D data with specific coordinates for x, y, and z.
9 * The output can be plotted for yz and xz planes.
10 *
11 * @section Usage
12 *
13 * ```
14 * sdds3dconvert <inputFile> [<outputRoot>]
15 * ```
16 *
17 * - `<inputFile>`: The input SDDS file containing 3D Vorpal data.
18 * - `[<outputRoot>]`: (Optional) The root name for output files. If not specified, the input file name is used as the root.
19 *
20 */
21
22#include "mdb.h"
23#include "SDDS.h"
24#include "scan.h"
25
26#define USAGE_MESSAGE "Usage: sdds3dconvert <inputFile> [<outputRoot>]\n" \
27 "Converts 3D Vorpal output data into SDDS format compatible with sddscontour.\n\n" \
28 "Arguments:\n" \
29 " <inputFile> Path to the input SDDS file containing 3D Vorpal data.\n" \
30 " <outputRoot> (Optional) Root name for the output files. Defaults to <inputFile> name if not provided.\n\n" \
31 "Link date: " __DATE__ " " __TIME__ ", SVN revision: " SVN_VERSION "\n"
32
33static void setup_output_file(SDDS_DATASET *sdds_out, const char *output, long yz, long output_columns, char **output_column);
34static void free_data_memory(double ****data, long output_columns, long pages, long ydim);
35
36int main(int argc, char **argv) {
37 SDDS_DATASET sdds_orig, sdds_out1, sdds_out2;
38 int32_t i, j, k, row, str_len;
39 SCANNED_ARG *s_arg;
40 char *input_file, *output_root, output1[1024], output2[1024], tmp_col[256];
41 double xmin, xmax, ymin, ymax, zmin, zmax, x_interval, y_interval, z_interval;
42 int32_t x_dim, y_dim, z_dim, column_names, output_columns, page = 0;
43 char **column_name, **output_column;
44 double ****data;
45
46 input_file = output_root = NULL;
48 argc = scanargs(&s_arg, argc, argv);
49
50 if (argc < 2) {
51 fprintf(stderr, "%s", USAGE_MESSAGE);
52 exit(EXIT_FAILURE);
53 }
54
55 column_names = output_columns = 0;
56 column_name = output_column = NULL;
57 input_file = s_arg[1].list[0];
58 output_root = (argc == 3) ? s_arg[2].list[0] : input_file;
59
60 snprintf(output1, sizeof(output1), "%s.yz", output_root);
61 snprintf(output2, sizeof(output2), "%s.xz", output_root);
62
63 data = NULL;
64 if (!SDDS_InitializeInput(&sdds_orig, input_file)) {
65 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
66 exit(EXIT_FAILURE);
67 }
68
69 column_name = (char **)SDDS_GetColumnNames(&sdds_orig, &column_names);
70 for (i = 0; i < column_names; i++) {
71 if (wild_match(column_name[i], "*_1")) {
72 output_column = SDDS_Realloc(output_column, (output_columns + 1) * sizeof(*output_column));
73 if (!output_column) {
74 fprintf(stderr, "Memory allocation failed for output_column.\n");
75 exit(EXIT_FAILURE);
76 }
77 str_len = strlen(column_name[i]);
78 output_column[output_columns] = malloc(str_len - 1); // Allocate enough space
79 if (!output_column[output_columns]) {
80 fprintf(stderr, "Memory allocation failed for output_column[%d].\n", output_columns);
81 exit(EXIT_FAILURE);
82 }
83 strncpy(output_column[output_columns], column_name[i], str_len - 2);
84 output_column[output_columns][str_len - 2] = '\0';
85 output_columns++;
86 }
87 }
88
89 data = malloc(sizeof(*data) * output_columns);
90 if (!data) {
91 fprintf(stderr, "Memory allocation failed for data.\n");
92 exit(EXIT_FAILURE);
93 }
94
95 while (SDDS_ReadPage(&sdds_orig) > 0) {
96 if (page == 0) {
97 if (!SDDS_GetParameterAsDouble(&sdds_orig, "origin1", &xmin) ||
98 !SDDS_GetParameterAsDouble(&sdds_orig, "max_ext1", &xmax) ||
99 !SDDS_GetParameterAsDouble(&sdds_orig, "delta1", &x_interval) ||
100 !SDDS_GetParameterAsLong(&sdds_orig, "numPhysCells1", &x_dim) ||
101 !SDDS_GetParameterAsDouble(&sdds_orig, "origin2", &ymin) ||
102 !SDDS_GetParameterAsDouble(&sdds_orig, "max_ext2", &ymax) ||
103 !SDDS_GetParameterAsDouble(&sdds_orig, "delta2", &y_interval) ||
104 !SDDS_GetParameterAsLong(&sdds_orig, "numPhysCells2", &y_dim) ||
105 !SDDS_GetParameterAsDouble(&sdds_orig, "origin3", &zmin) ||
106 !SDDS_GetParameterAsDouble(&sdds_orig, "max_ext3", &zmax) ||
107 !SDDS_GetParameterAsDouble(&sdds_orig, "delta3", &z_interval) ||
108 !SDDS_GetParameterAsLong(&sdds_orig, "numPhysCells3", &z_dim)) {
109 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
110 exit(EXIT_FAILURE);
111 }
112 setup_output_file(&sdds_out1, output1, 1, output_columns, output_column);
113 setup_output_file(&sdds_out2, output2, 0, output_columns, output_column);
114
115 for (i = 0; i < output_columns; i++) {
116 data[i] = malloc(z_dim * sizeof(**data));
117 if (!data[i]) {
118 fprintf(stderr, "Memory allocation failed for data[%d].\n", i);
119 free_data_memory(data, output_columns, z_dim, y_dim);
120 exit(EXIT_FAILURE);
121 }
122 for (j = 0; j < z_dim; j++) {
123 data[i][j] = malloc(y_dim * sizeof(***data));
124 if (!data[i][j]) {
125 fprintf(stderr, "Memory allocation failed for data[%d][%d].\n", i, j);
126 free_data_memory(data, output_columns, z_dim, y_dim);
127 exit(EXIT_FAILURE);
128 }
129 }
130 }
131 }
132
133 for (i = 0; i < output_columns; i++) {
134 for (j = 1; j <= y_dim; j++) {
135 snprintf(tmp_col, sizeof(tmp_col), "%s_%d", output_column[i], j);
136 data[i][page][j - 1] = SDDS_GetColumnInDoubles(&sdds_orig, tmp_col);
137 if (!data[i][page][j - 1]) {
138 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
139 free_data_memory(data, output_columns, z_dim, y_dim);
140 exit(EXIT_FAILURE);
141 }
142 }
143 }
144 page++;
145 }
146
147 if (!SDDS_Terminate(&sdds_orig)) {
148 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors);
149 free_data_memory(data, output_columns, z_dim, y_dim);
150 exit(EXIT_FAILURE);
151 }
152
153 if (page != z_dim) {
154 free_data_memory(data, output_columns, z_dim, y_dim);
155 fprintf(stderr, "Error: Page count (%d) does not match z-dimension size (%d).\n", page, z_dim);
156 exit(EXIT_FAILURE);
157 }
158
159 /* Write yz output */
160 for (page = 0; page < x_dim; page++) {
161 if (!SDDS_StartPage(&sdds_out1, y_dim * z_dim) ||
162 !SDDS_SetParameters(&sdds_out1, SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE,
163 "origin1", xmin, "origin2", ymin, "origin3", zmin,
164 "max_ext1", xmax, "max_ext2", ymax, "max_ext3", zmax,
165 "delta1", x_interval, "delta2", y_interval, "delta3", z_interval,
166 "numPhysCells1", x_dim, "numPhysCells2", y_dim, "numPhysCells3", z_dim,
167 "Variable1Name", "Y", "Variable2Name", "Z",
168 "ZMinimum", zmin, "ZMaximum", zmax, "ZInterval", z_interval, "ZDimension", z_dim,
169 "YMinimum", ymin, "YMaximum", ymax, "YInterval", y_interval, "YDimension", y_dim,
170 NULL)) {
171 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
172 free_data_memory(data, output_columns, z_dim, y_dim);
173 exit(EXIT_FAILURE);
174 }
175
176 for (i = 0; i < output_columns; i++) {
177 row = 0;
178 for (j = 0; j < y_dim; j++) {
179 for (k = 0; k < z_dim; k++) {
180 if (!SDDS_SetRowValues(&sdds_out1, SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE, row,
181 output_column[i], data[i][k][j][page],
182 "z", k * z_interval + zmin,
183 "y", j * y_interval + ymin, NULL)) {
184 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
185 free_data_memory(data, output_columns, z_dim, y_dim);
186 exit(EXIT_FAILURE);
187 }
188 row++;
189 }
190 }
191 }
192
193 if (!SDDS_WritePage(&sdds_out1)) {
194 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
195 free_data_memory(data, output_columns, z_dim, y_dim);
196 exit(EXIT_FAILURE);
197 }
198 }
199
200 if (!SDDS_Terminate(&sdds_out1)) {
201 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
202 free_data_memory(data, output_columns, z_dim, y_dim);
203 exit(EXIT_FAILURE);
204 }
205
206 /* Write xz output */
207 for (page = 0; page < y_dim; page++) {
208 if (!SDDS_StartPage(&sdds_out2, x_dim * z_dim) ||
209 !SDDS_SetParameters(&sdds_out2, SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE,
210 "origin1", xmin, "origin2", ymin, "origin3", zmin,
211 "max_ext1", xmax, "max_ext2", ymax, "max_ext3", zmax,
212 "delta1", x_interval, "delta2", y_interval, "delta3", z_interval,
213 "numPhysCells1", x_dim, "numPhysCells2", y_dim, "numPhysCells3", z_dim,
214 "Variable1Name", "X", "Variable2Name", "Z",
215 "ZMinimum", zmin, "ZMaximum", zmax, "ZInterval", z_interval, "ZDimension", z_dim,
216 "XMinimum", xmin, "XMaximum", xmax, "XInterval", x_interval, "XDimension", x_dim,
217 NULL)) {
218 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
219 free_data_memory(data, output_columns, z_dim, y_dim);
220 exit(EXIT_FAILURE);
221 }
222
223 for (i = 0; i < output_columns; i++) {
224 row = 0;
225 for (j = 0; j < x_dim; j++) {
226 for (k = 0; k < z_dim; k++) {
227 if (!SDDS_SetRowValues(&sdds_out2, SDDS_SET_BY_NAME | SDDS_PASS_BY_VALUE, row,
228 output_column[i], data[i][k][page][j],
229 "z", k * z_interval + zmin,
230 "x", j * x_interval + xmin, NULL)) {
231 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
232 free_data_memory(data, output_columns, z_dim, y_dim);
233 exit(EXIT_FAILURE);
234 }
235 row++;
236 }
237 }
238 }
239
240 if (!SDDS_WritePage(&sdds_out2)) {
241 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
242 free_data_memory(data, output_columns, z_dim, y_dim);
243 exit(EXIT_FAILURE);
244 }
245 }
246
247 free_data_memory(data, output_columns, z_dim, y_dim);
248
249 if (!SDDS_Terminate(&sdds_out2)) {
250 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
251 exit(EXIT_FAILURE);
252 }
253
254 return EXIT_SUCCESS;
255}
256
257static void free_data_memory(double ****data, long output_columns, long z_dim, long y_dim) {
258 long i, j, k;
259 if (!data)
260 return;
261 for (i = 0; i < output_columns; i++) {
262 if (!data[i])
263 continue;
264 for (j = 0; j < z_dim; j++) {
265 if (!data[i][j])
266 continue;
267 for (k = 0; k < y_dim; k++) {
268 free(data[i][j][k]);
269 }
270 free(data[i][j]);
271 }
272 free(data[i]);
273 }
274 free(data);
275}
276
277static void setup_output_file(SDDS_DATASET *sdds_out, const char *output, long yz, long output_columns, char **output_column) {
278 long i;
279 if (!SDDS_InitializeOutput(sdds_out, SDDS_BINARY, 0, NULL, NULL, output)) {
280 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
281 }
282
283 if (!SDDS_DefineSimpleParameter(sdds_out, "origin1", NULL, SDDS_DOUBLE) ||
284 !SDDS_DefineSimpleParameter(sdds_out, "origin2", NULL, SDDS_DOUBLE) ||
285 !SDDS_DefineSimpleParameter(sdds_out, "origin3", NULL, SDDS_DOUBLE) ||
286 !SDDS_DefineSimpleParameter(sdds_out, "max_ext1", NULL, SDDS_DOUBLE) ||
287 !SDDS_DefineSimpleParameter(sdds_out, "max_ext2", NULL, SDDS_DOUBLE) ||
288 !SDDS_DefineSimpleParameter(sdds_out, "max_ext3", NULL, SDDS_DOUBLE) ||
289 !SDDS_DefineSimpleParameter(sdds_out, "delta1", NULL, SDDS_DOUBLE) ||
290 !SDDS_DefineSimpleParameter(sdds_out, "delta2", NULL, SDDS_DOUBLE) ||
291 !SDDS_DefineSimpleParameter(sdds_out, "delta3", NULL, SDDS_DOUBLE) ||
292 !SDDS_DefineSimpleParameter(sdds_out, "numPhysCells1", NULL, SDDS_LONG) ||
293 !SDDS_DefineSimpleParameter(sdds_out, "numPhysCells2", NULL, SDDS_LONG) ||
294 !SDDS_DefineSimpleParameter(sdds_out, "numPhysCells3", NULL, SDDS_LONG) ||
295 !SDDS_DefineSimpleParameter(sdds_out, "Variable1Name", NULL, SDDS_STRING) ||
296 !SDDS_DefineSimpleParameter(sdds_out, "Variable2Name", NULL, SDDS_STRING) ||
297 !SDDS_DefineSimpleParameter(sdds_out, "ZMinimum", NULL, SDDS_DOUBLE) ||
298 !SDDS_DefineSimpleParameter(sdds_out, "ZMaximum", NULL, SDDS_DOUBLE) ||
299 !SDDS_DefineSimpleParameter(sdds_out, "ZInterval", NULL, SDDS_DOUBLE) ||
300 !SDDS_DefineSimpleParameter(sdds_out, "ZDimension", NULL, SDDS_LONG) ||
301 !SDDS_DefineSimpleColumn(sdds_out, "z", NULL, SDDS_DOUBLE)) {
302 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
303 }
304
305 if (yz) {
306 if (!SDDS_DefineSimpleParameter(sdds_out, "YMinimum", NULL, SDDS_DOUBLE) ||
307 !SDDS_DefineSimpleParameter(sdds_out, "YMaximum", NULL, SDDS_DOUBLE) ||
308 !SDDS_DefineSimpleParameter(sdds_out, "YInterval", NULL, SDDS_DOUBLE) ||
309 !SDDS_DefineSimpleParameter(sdds_out, "YDimension", NULL, SDDS_LONG) ||
310 !SDDS_DefineSimpleColumn(sdds_out, "y", NULL, SDDS_DOUBLE)) {
311 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
312 }
313 } else {
314 if (!SDDS_DefineSimpleParameter(sdds_out, "XMinimum", NULL, SDDS_DOUBLE) ||
315 !SDDS_DefineSimpleParameter(sdds_out, "XMaximum", NULL, SDDS_DOUBLE) ||
316 !SDDS_DefineSimpleParameter(sdds_out, "XInterval", NULL, SDDS_DOUBLE) ||
317 !SDDS_DefineSimpleParameter(sdds_out, "XDimension", NULL, SDDS_LONG) ||
318 !SDDS_DefineSimpleColumn(sdds_out, "x", NULL, SDDS_DOUBLE)) {
319 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
320 }
321 }
322
323 for (i = 0; i < output_columns; i++) {
324 if (!SDDS_DefineSimpleColumn(sdds_out, output_column[i], NULL, SDDS_DOUBLE)) {
325 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
326 }
327 }
328
329 if (!SDDS_WriteLayout(sdds_out)) {
330 SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors | SDDS_EXIT_PrintErrors);
331 }
332}
SDDS (Self Describing Data Set) Data Types Definitions and Function Prototypes.
int32_t SDDS_SetRowValues(SDDS_DATASET *SDDS_dataset, int32_t mode, int64_t row,...)
int32_t SDDS_StartPage(SDDS_DATASET *SDDS_dataset, int64_t expected_n_rows)
int32_t SDDS_SetParameters(SDDS_DATASET *SDDS_dataset, int32_t mode,...)
int32_t * SDDS_GetParameterAsLong(SDDS_DATASET *SDDS_dataset, char *parameter_name, int32_t *memory)
Retrieves the value of a specified parameter as a 32-bit integer from the current data table of a dat...
double * SDDS_GetParameterAsDouble(SDDS_DATASET *SDDS_dataset, char *parameter_name, double *memory)
Retrieves the value of a specified parameter as a double from the current data table of an SDDS datas...
double * SDDS_GetColumnInDoubles(SDDS_DATASET *SDDS_dataset, char *column_name)
Retrieves the data of a specified numerical column as an array of doubles, considering only rows mark...
int32_t SDDS_InitializeInput(SDDS_DATASET *SDDS_dataset, char *filename)
Definition SDDS_input.c:49
int32_t SDDS_Terminate(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_ReadPage(SDDS_DATASET *SDDS_dataset)
int32_t SDDS_InitializeOutput(SDDS_DATASET *SDDS_dataset, int32_t data_mode, int32_t lines_per_row, const char *description, const char *contents, const char *filename)
Initializes the SDDS output dataset.
int32_t SDDS_DefineSimpleColumn(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data column within the SDDS dataset.
int32_t SDDS_DefineSimpleParameter(SDDS_DATASET *SDDS_dataset, const char *name, const char *unit, int32_t type)
Defines a simple data parameter within the SDDS dataset.
int32_t SDDS_WritePage(SDDS_DATASET *SDDS_dataset)
Writes the current data table to the output file.
int32_t SDDS_WriteLayout(SDDS_DATASET *SDDS_dataset)
Writes the SDDS layout header to the output file.
char ** SDDS_GetColumnNames(SDDS_DATASET *SDDS_dataset, int32_t *number)
Retrieves the names of all columns in the SDDS dataset.
void SDDS_PrintErrors(FILE *fp, int32_t mode)
Prints recorded error messages to a specified file stream.
Definition SDDS_utils.c:432
void SDDS_RegisterProgramName(const char *name)
Registers the executable program name for use in error messages.
Definition SDDS_utils.c:288
void * SDDS_Realloc(void *old_ptr, size_t new_size)
Reallocates memory to a new size.
Definition SDDS_utils.c:677
#define SDDS_STRING
Identifier for the string data type.
Definition SDDStypes.h:85
#define SDDS_LONG
Identifier for the signed 32-bit integer data type.
Definition SDDStypes.h:61
#define SDDS_DOUBLE
Identifier for the double data type.
Definition SDDStypes.h:37
int scanargs(SCANNED_ARG **scanned, int argc, char **argv)
Definition scanargs.c:36
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.
Definition wild_match.c:49