GADGET-4
parameters.cc
Go to the documentation of this file.
1/*******************************************************************************
2 * \copyright This file is part of the GADGET4 N-body/SPH code developed
3 * \copyright by Volker Springel. Copyright (C) 2014-2020 by Volker Springel
4 * \copyright (vspringel@mpa-garching.mpg.de) and all contributing authors.
5 *******************************************************************************/
6
12#include "gadgetconfig.h"
13
14#include <math.h>
15#include <mpi.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <sys/stat.h>
20
21#include "../data/allvars.h"
22#include "../data/dtypes.h"
23#include "../data/mymalloc.h"
24#include "../io/io.h"
25#include "../io/parameters.h"
26#include "../main/main.h"
27#include "../mpi_utils/shared_mem_handler.h"
28#include "../system/system.h"
29
30void parameters::add_param(const char *name, void *buf, int type, int flag)
31{
33 Terminate("exceeded MAX_PARAMETERS=%d", MAX_PARAMETERS);
34
35 if(strlen(name) > MAXLEN_PARAM_TAG - 1)
36 Terminate("parameter '%s' too long", name);
37
38 strcpy(ParametersTag[NParameters], name);
43}
44
57int parameters::read_parameter_file(const char *fname)
58{
59 FILE *fd, *fdout;
60 char buf[MAXLEN_PARAM_TAG + MAXLEN_PARAM_VALUE + 200];
61 int param_handled[MAX_PARAMETERS];
62 int errorFlag = 0;
63
64 for(int i = 0; i < MAX_PARAMETERS; i++)
65 {
66 param_handled[i] = 0;
67 ParameterSequence[i] = -1;
68 }
69
70 if(sizeof(long long) != 8)
71 Terminate("\nType `long long' is not 64 bit on this platform. Stopping.\n\n");
72
73 if(sizeof(int) != 4)
74 Terminate("\nType `int' is not 32 bit on this platform. Stopping.\n\n");
75
76 if(sizeof(float) != 4)
77 Terminate("\nType `float' is not 32 bit on this platform. Stopping.\n\n");
78
79 if(sizeof(double) != 8)
80 Terminate("\nType `double' is not 64 bit on this platform. Stopping.\n\n");
81
82 if(ThisTask == 0) /* read parameter file on process 0 */
83 {
84 if((fd = fopen(fname, "r")))
85 {
86 sprintf(buf, "%s%s", fname, "-usedvalues");
87 if(!(fdout = fopen(buf, "w")))
88 {
89 printf("error opening file '%s' \n", buf);
90 errorFlag = 1;
91 }
92 else
93 {
94 printf("Obtaining parameters from file '%s':\n\n", fname);
95 int cnt = 0;
96 while(!feof(fd))
97 {
98 char buf1[MAXLEN_PARAM_TAG + 200], buf2[MAXLEN_PARAM_VALUE + 200], buf3[MAXLEN_PARAM_TAG + MAXLEN_PARAM_VALUE + 400];
99
100 *buf = 0;
101 fgets(buf, MAXLEN_PARAM_TAG + MAXLEN_PARAM_VALUE + 200, fd);
102 if(sscanf(buf, "%s%s%s", buf1, buf2, buf3) < 2)
103 continue;
104
105 if(buf1[0] == '%' || buf1[0] == '#')
106 continue;
107
108 int j = -1;
109 for(int i = 0; i < NParameters; i++)
110 if(strcmp(buf1, ParametersTag[i]) == 0)
111 {
112 if(param_handled[i] == 0)
113 {
114 j = i;
115 param_handled[i] = 1;
116 ParameterSequence[cnt++] = i;
117 break;
118 }
119 else
120 {
121 j = -2;
122 break;
123 }
124 }
125
126 if(j >= 0)
127 {
128 switch(ParametersType[j])
129 {
130 case PARAM_DOUBLE:
131 *((double *)ParametersValue[j]) = atof(buf2);
132 sprintf(buf3, "%%-%ds%%g\n", MAXLEN_PARAM_TAG);
133 fprintf(fdout, buf3, buf1, *((double *)ParametersValue[j]));
134 fprintf(stdout, " ");
135 fprintf(stdout, buf3, buf1, *((double *)ParametersValue[j]));
136 break;
137 case PARAM_STRING:
138 if(strcmp(buf2, "OUTPUT_DIR") == 0)
139 {
140 if(getenv("OUTPUT_DIR"))
141 strcpy(buf2, getenv("OUTPUT_DIR"));
142 else
143 Terminate("no environment variable OUTPUT_DIR found");
144 }
145 strcpy((char *)ParametersValue[j], buf2);
146 sprintf(buf3, "%%-%ds%%s\n", MAXLEN_PARAM_TAG);
147 fprintf(fdout, buf3, buf1, buf2);
148 fprintf(stdout, " ");
149 fprintf(stdout, buf3, buf1, buf2);
150 break;
151 case PARAM_INT:
152 *((int *)ParametersValue[j]) = atoi(buf2);
153 sprintf(buf3, "%%-%ds%%d\n", MAXLEN_PARAM_TAG);
154 fprintf(fdout, buf3, buf1, *((int *)ParametersValue[j]));
155 fprintf(stdout, " ");
156 fprintf(stdout, buf3, buf1, *((int *)ParametersValue[j]));
157 break;
158 }
159 }
160 else if(j == -2)
161 {
162 fprintf(stdout, "Error in file %s: Tag '%s' multiply defined.\n", fname, buf1);
163 errorFlag = 1;
164 }
165 else
166 {
167 fprintf(stdout, "Error in file %s: Tag '%s' not allowed\n", fname, buf1);
168 errorFlag = 1;
169 }
170 }
171 fclose(fd);
172 fclose(fdout);
173 printf("\n");
174 }
175 }
176 else
177 {
178 printf("Parameter file %s not found.\n", fname);
179 errorFlag = 1;
180 }
181
182 for(int i = 0; i < NParameters; i++)
183 {
184 if(param_handled[i] != 1)
185 {
186 printf("Error. I miss a value for tag '%s' in parameter file '%s'.\n", ParametersTag[i], fname);
187 errorFlag = 1;
188 }
189 }
190 }
191
192 return errorFlag;
193}
194
195void parameters::write_used_parameters(const char *dirname, const char *fname)
196{
197 if(ThisTask == 0)
198 {
199 mkdir(dirname, 02755);
200 char buf[MAXLEN_PATH_EXTRA];
201 sprintf(buf, "%s%s", dirname, fname);
202 FILE *fdout = fopen(buf, "w");
203 if(!fdout)
204 Terminate("Can't open file '%s'", buf);
205
206 for(int i = 0; i < NParameters; i++)
207 {
208 int j = ParameterSequence[i];
209
210 if(j >= 0)
211 {
212 char buf3[MAXLEN_PARAM_TAG + MAXLEN_PARAM_VALUE + 400];
213
214 switch(ParametersType[j])
215 {
216 case PARAM_DOUBLE:
217 sprintf(buf3, "%%-%ds%%g\n", MAXLEN_PARAM_TAG);
218 fprintf(fdout, buf3, ParametersTag[j], *((double *)ParametersValue[j]));
219 break;
220 case PARAM_STRING:
221 sprintf(buf3, "%%-%ds%%s\n", MAXLEN_PARAM_TAG);
222 fprintf(fdout, buf3, ParametersTag[j], (char *)ParametersValue[j]);
223 break;
224 case PARAM_INT:
225 sprintf(buf3, "%%-%ds%%d\n", MAXLEN_PARAM_TAG);
226 fprintf(fdout, buf3, ParametersTag[j], *((int *)ParametersValue[j]));
227 break;
228 }
229 }
230 }
231
232 fclose(fdout);
233 }
234}
char ParametersType[MAX_PARAMETERS]
Definition: parameters.h:46
char ParametersTag[MAX_PARAMETERS][MAXLEN_PARAM_TAG]
Definition: parameters.h:44
void add_param(const char *name, void *buf, int type, int flag)
Definition: parameters.cc:30
int ParameterSequence[MAX_PARAMETERS]
Definition: parameters.h:48
void write_used_parameters(const char *dirname, const char *fname)
Definition: parameters.cc:195
int NParameters
Definition: parameters.h:42
int read_parameter_file(const char *fname)
This function parses the parameter file.
Definition: parameters.cc:57
void * ParametersValue[MAX_PARAMETERS]
Definition: parameters.h:45
char ParametersChangeable[MAX_PARAMETERS]
Definition: parameters.h:47
int ThisTask
Definition: setcomm.h:33
#define MAXLEN_PATH_EXTRA
Definition: constants.h:301
#define Terminate(...)
Definition: macros.h:15
#define MAXLEN_PARAM_TAG
Definition: parameters.h:25
#define MAX_PARAMETERS
Definition: parameters.h:27
#define PARAM_INT
Definition: parameters.h:20
#define MAXLEN_PARAM_VALUE
Definition: parameters.h:26
#define PARAM_STRING
Definition: parameters.h:19
#define PARAM_DOUBLE
Definition: parameters.h:18