|
| 1 | +#include <stdlib.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <math.h> |
| 5 | +#include "exitCodes.h" |
| 6 | +#include "modelParams.h" |
| 7 | +#include "util.h" |
| 8 | + |
| 9 | +// Private/helper functions: not defined in modelParams.h |
| 10 | + |
| 11 | +// return 1 if all maxParams parameters have been initialized, 0 if not |
| 12 | +int allParamsInitialized(ModelParams *ModelParams) { |
| 13 | + if (ModelParams->numParams == ModelParams->maxParams) { |
| 14 | + // all parameters have been initialized |
| 15 | + return 1; |
| 16 | + } |
| 17 | + |
| 18 | + return 0; |
| 19 | +} |
| 20 | + |
| 21 | +// Set array[0..length-1] to all be equal to value |
| 22 | +void setAll(double *array, int length, double value) { |
| 23 | + int i; |
| 24 | + |
| 25 | + for (i = 0; i < length; i++) { |
| 26 | + array[i] = value; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Checks to make sure that all parameters with isRequired=true have been read |
| 31 | +// If not, kills program |
| 32 | +// Writes out names of all parameters that weren't read (even if not required, |
| 33 | +// as a warning message) |
| 34 | +void checkAllRead(ModelParams *ModelParams) { |
| 35 | + int i; |
| 36 | + int okay; |
| 37 | + OneModelParam *param; |
| 38 | + |
| 39 | + okay = 1; // so far so good |
| 40 | + for (i = 0; i < ModelParams->numParams; i++) { |
| 41 | + param = &(ModelParams->params[i]); |
| 42 | + if (param->value == NULL) { |
| 43 | + if (param->isRequired) { // should have been read but wasn't! |
| 44 | + okay = 0; |
| 45 | + printf("ERROR: Didn't read required parameter %s\n", param->name); |
| 46 | + } else { |
| 47 | + printf("WARNING: Didn't read parameter %s (not flagged as required, so " |
| 48 | + "continuing)\n", |
| 49 | + param->name); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + if (!okay) { |
| 55 | + exit(1); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/*************************************************/ |
| 60 | + |
| 61 | +// Public functions: defined in ModelParams.h |
| 62 | + |
| 63 | +// allocate space for a new ModelParams structure, return a pointer to it |
| 64 | +ModelParams *newModelParams(int maxParams) { |
| 65 | + ModelParams *modelParams; |
| 66 | + |
| 67 | + modelParams = (ModelParams *)malloc(sizeof(ModelParams)); |
| 68 | + modelParams->maxParams = maxParams; |
| 69 | + modelParams->numParams = 0; // for now, anyway |
| 70 | + modelParams->numParamsRead = 0; // for now, anyway |
| 71 | + |
| 72 | + // allocate space for vectors within this structure: |
| 73 | + modelParams->params = |
| 74 | + (OneModelParam *)malloc(maxParams * sizeof(OneModelParam)); |
| 75 | + // Allocate enough space for reading all params |
| 76 | + modelParams->readIndices = (int *)malloc(maxParams * sizeof(int)); |
| 77 | + |
| 78 | + return modelParams; |
| 79 | +} |
| 80 | + |
| 81 | +void initializeOneModelParam(ModelParams *modelParams, char *name, |
| 82 | + double *externalLoc, int isRequired) { |
| 83 | + int paramIndex; // index of next uninitialized parameter |
| 84 | + OneModelParam *param; // a pointer to the next uninitialized parameter |
| 85 | + |
| 86 | + if (allParamsInitialized(modelParams)) { // all parameters have been |
| 87 | + // initialized! |
| 88 | + printf("Error trying to initialize %s: have already initialized all %d " |
| 89 | + "parameters\n", |
| 90 | + name, modelParams->maxParams); |
| 91 | + printf("Check value of maxParams passed into newModelParams function\n"); |
| 92 | + exit(1); |
| 93 | + } |
| 94 | + |
| 95 | + // otherwise, get the index of the next uninitialized parameter |
| 96 | + paramIndex = modelParams->numParams; |
| 97 | + // and set param to point to it for easier access |
| 98 | + param = &(modelParams->params[paramIndex]); |
| 99 | + |
| 100 | + strcpy(param->name, name); |
| 101 | + param->value = externalLoc; |
| 102 | + param->isRequired = isRequired; |
| 103 | + param->isRead = 0; |
| 104 | + |
| 105 | + modelParams->numParams++; |
| 106 | +} |
| 107 | + |
| 108 | +void checkParamFormat(char *line, const char *sep) { |
| 109 | + int numParams = countFields(line, sep); |
| 110 | + if (numParams > 2) { |
| 111 | + printf("WARNING: extra columns in .param file are being ignored (found %d " |
| 112 | + "columns)\n", |
| 113 | + numParams); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +void readModelParams(ModelParams *modelParams, FILE *paramFile) { |
| 118 | + const char *SEPARATORS = " \t\n\r"; // characters that can separate values in |
| 119 | + // parameter files |
| 120 | + const char *COMMENT_CHARS = "!"; // comment characters (ignore everything |
| 121 | + // after this on a line) |
| 122 | + |
| 123 | + char line[256]; |
| 124 | + char pName[MODEL_PARAM_MAXNAME]; // parameter name |
| 125 | + int paramIndex; |
| 126 | + OneModelParam *param; // a pointer to a single parameter, for easier access |
| 127 | + char strValue[32]; // before we know whether value is a number or "*" |
| 128 | + double value; |
| 129 | + char *errc; |
| 130 | + int isComment; |
| 131 | + |
| 132 | + // Check for old-style (spatial param) format on first line containing params |
| 133 | + int formatChecked = 0; |
| 134 | + |
| 135 | + while (fgets(line, sizeof(line), paramFile) != NULL) { // while not EOF or |
| 136 | + // error |
| 137 | + // remove trailing comments: |
| 138 | + isComment = stripComment(line, COMMENT_CHARS); |
| 139 | + |
| 140 | + if (!isComment) { // if this isn't just a comment line or blank line |
| 141 | + // Check for old spatial-param format and warn if appropriate |
| 142 | + if (!formatChecked) { |
| 143 | + checkParamFormat(line, SEPARATORS); |
| 144 | + formatChecked = 1; |
| 145 | + } |
| 146 | + |
| 147 | + // tokenize line: |
| 148 | + strcpy(pName, strtok(line, SEPARATORS)); // copy first token into pName |
| 149 | + |
| 150 | + strcpy(strValue, strtok(NULL, SEPARATORS)); |
| 151 | + |
| 152 | + if (strcmp(strValue, "*") == 0) { |
| 153 | + // This used to mean "spatially varying" when sipnet supported multiple |
| 154 | + // sites, but now this is an error |
| 155 | + printf("Error reading parameter %s; '*' is no longer supported\n", |
| 156 | + pName); |
| 157 | + exit(EXIT_CODE_BAD_PARAMETER_VALUE); |
| 158 | + } |
| 159 | + value = strtod(strValue, &errc); |
| 160 | + paramIndex = locateParam(modelParams, pName); |
| 161 | + |
| 162 | + if (paramIndex == -1) { // not found |
| 163 | + printf("WARNING: Ignoring parameter %s: this parameter wasn't " |
| 164 | + "initialized in the code\n", |
| 165 | + pName); |
| 166 | + } else if (valueSet(modelParams, paramIndex)) { |
| 167 | + printf("Error reading parameter file: read %s, but this parameter has " |
| 168 | + "already been set\n", |
| 169 | + pName); |
| 170 | + exit(EXIT_CODE_INPUT_FILE_ERROR); |
| 171 | + } else { // otherwise, we're good to go |
| 172 | + // set param to point to the appropriate parameter, for easier access |
| 173 | + param = &(modelParams->params[paramIndex]); |
| 174 | + *(param->value) = value; |
| 175 | + param->isRead = 1; |
| 176 | + // mark this as the next parameter read from file: |
| 177 | + modelParams->readIndices[modelParams->numParamsRead] = paramIndex; |
| 178 | + modelParams->numParamsRead++; |
| 179 | + } // else (no errors in reading this line from parameter file) |
| 180 | + } // if !isComment |
| 181 | + } // while not EOF or error |
| 182 | + |
| 183 | + // check for error in reading: |
| 184 | + if (ferror(paramFile)) { |
| 185 | + printf("Error reading file in readModelParams\n"); |
| 186 | + printf("ferror = %d\n", ferror(paramFile)); |
| 187 | + exit(1); |
| 188 | + } |
| 189 | + |
| 190 | + checkAllRead(modelParams); // terminate program if some required parameters |
| 191 | + // weren't read |
| 192 | +} // readModelParams |
| 193 | + |
| 194 | +// Return numParams, the actual number of parameters that have been |
| 195 | +// initialized with initializeOneModelParam |
| 196 | +int getnumParams(ModelParams *modelParams) { return modelParams->numParams; } |
| 197 | + |
| 198 | +// Return number of parameters that have been read in from file |
| 199 | +int getNumParamsRead(ModelParams *modelParams) { |
| 200 | + return modelParams->numParamsRead; |
| 201 | +} |
| 202 | + |
| 203 | +// Find parameter with given name in the parameters vector |
| 204 | +// If found, return index in vector, otherwise return -1 |
| 205 | +int locateParam(ModelParams *modelParams, char *name) { |
| 206 | + int i; |
| 207 | + int found; |
| 208 | + |
| 209 | + i = 0; |
| 210 | + found = 0; |
| 211 | + while (i < modelParams->numParams && !found) { |
| 212 | + if (strcmpIgnoreCase(name, modelParams->params[i].name) == 0) { |
| 213 | + found = 1; |
| 214 | + } |
| 215 | + i++; |
| 216 | + } |
| 217 | + |
| 218 | + if (found) { |
| 219 | + return (i - 1); |
| 220 | + } |
| 221 | + |
| 222 | + return -1; |
| 223 | +} |
| 224 | + |
| 225 | +// Return 1 if parameter i has had its value set, 0 otherwise |
| 226 | +int valueSet(ModelParams *modelParams, int i) { |
| 227 | + return modelParams->params[i].isRead; |
| 228 | +} |
| 229 | + |
| 230 | +void deleteModelParams(ModelParams *modelParams) { |
| 231 | + // Free space used by ModelParams structure itself: |
| 232 | + free(modelParams->params); |
| 233 | + free(modelParams->readIndices); |
| 234 | + free(modelParams); |
| 235 | +} |
0 commit comments