/* parse_param_line reads a line in the given parameter sets buffer and stores it in the given array of doubles parameters: params: the array of doubles to store the parameters in buffer_line: the buffer with the line to read index_buffer: the index of the buffer to start from returns: true if a line was found, false if the end of the file was reached without finding a valid line notes: The buffer should contain one parameter set per line, each set containing comma-separated floating point parameters. Blank lines and lines starting with # will be ignored. Each line must contain the correct number of parameters or the program will exit. index_buffer is a reference, allowing this function to store where it finished parsing. todo: */ bool parse_param_line (double* params, char* buffer_line, int& index_buffer) { static const char* usage_message = "There was an error reading the given parameter sets file."; int index_params = 0; // Current index in params int index_digits = index_buffer; // Index of the start of the digits to read int i = index_buffer; // Current index in buffer_line int line_start = i; // The start of the line, used to tell whether or not a line is empty for (; not_EOL(buffer_line[i]); i++) { if (buffer_line[i] == '#') { // Skip any lines starting with # for(; not_EOL(buffer_line[i]); i++); i++; } else if (buffer_line[i] == ',') { // Indicates the end of the digits to read if (sscanf(buffer_line + index_digits, "%lf", &(params[index_params++])) < 1) { // Convert the string of digits to a double when storing it in params usage(usage_message); } index_digits = i + 1; } } index_buffer = i + 1; if (i - line_start > 0) { // This line has content if (sscanf(buffer_line + index_digits, "%lf", &(params[index_params++])) < 1) { usage(usage_message); } if (index_params != NUM_PARS) { cout << term->red << "The given parameter sets file contains sets with an incorrect number of rates! This simulation requires " << NUM_PARS << " per set but at least one line contains " << index_params << " per set." << term->reset << endl; exit(EXIT_INPUT_ERROR); } return true; } else if (buffer_line[index_buffer] != '\0') { // There are more lines to try to parse return parse_param_line(params, buffer_line, index_buffer); } else { // The end of the buffer was found return false; } }
void parse_params( char *env, char *cfgfile, char *argv[], int argc ) { int i; if ( env ) { char *val = getenv( env ); if ( val ) parse_param_line( val ); } if ( cfgfile ) { char *cfg = xfind( cfgfile, getrundir()); #if DEBUG printf( "*** ConfigFile '%s'\n", cfg ); #endif parse_param_file( cfg ); } for (i = 1; i < argc; i++) parse_param( argv[i] ); }
void parse_param_file( char *file ) { static char line1[ LINE_MAX ]; static char line[ LINE_MAX ] ; FILE *fp; assert( file != NULL ); fp = fopen( file, "rt" ); if (fp) { while( fgets( line, LINE_MAX-1, fp ) ) { int len = strlen( line ); if ( len > 0 && line[len-1] == '\n' ) line[len-1] = '\0'; scan_env(line1, line) ; parse_param_line( line1 ); } fclose( fp ); } else { perror( file ); } }
/* load a default set of parameters from a file */ void Replay::load_param_file(const char *pfilename) { FILE *f = fopen(pfilename, "r"); if (f == NULL) { printf("Failed to open parameter file: %s\n", pfilename); exit(1); } char line[100]; while (fgets(line, sizeof(line)-1, f)) { char *pname; float value; if (!parse_param_line(line, &pname, value)) { continue; } struct user_parameter *u = new user_parameter; strncpy(u->name, pname, sizeof(u->name)); u->value = value; u->next = user_parameters; user_parameters = u; } fclose(f); }
/* load a default set of parameters from a file */ bool AP_Param::load_defaults_file(const char *filename) { FILE *f = fopen(filename, "r"); if (f == NULL) { return false; } char line[100]; /* work out how many parameter default structures to allocate */ uint16_t num_defaults = 0; while (fgets(line, sizeof(line)-1, f)) { char *pname; float value; if (!parse_param_line(line, &pname, value)) { continue; } if (!find_def_value_ptr(pname)) { fclose(f); return false; } num_defaults++; } fclose(f); if (param_overrides != NULL) { free(param_overrides); } num_param_overrides = 0; param_overrides = new param_override[num_defaults]; if (param_overrides == NULL) { return false; } /* re-open to avoid possible seek issues with NuttX */ f = fopen(filename, "r"); if (f == NULL) { return false; } uint16_t idx = 0; while (fgets(line, sizeof(line)-1, f)) { char *pname; float value; if (!parse_param_line(line, &pname, value)) { continue; } const float *def_value_ptr = find_def_value_ptr(pname); if (!def_value_ptr) { fclose(f); return false; } param_overrides[idx].def_value_ptr = def_value_ptr; param_overrides[idx].value = value; idx++; enum ap_var_type var_type; AP_Param *vp = AP_Param::find(pname, &var_type); if (!vp) { fclose(f); return false; } vp->set_float(value, var_type); } fclose(f); num_param_overrides = num_defaults; return true; }
void read_parameter_library( char FN_parameter_library[MAX_CHARS], int outlev ) { static ParameterEntry thisParameter; FILE *parameter_library_file; char parameter_library_line[MAX_CHARS]; int nfields; int param_keyword = -1; int int_hbond_type = 0; pr(logFile, "Using read_parameter_library\n"); // Open and read the parameter library // if ((parameter_library_file = ag_fopen(FN_parameter_library, "r")) == NULL) { fprintf(stderr,"Sorry, I can't find or open %s\n", FN_parameter_library); exit(-1); } while (fgets(parameter_library_line, sizeof(parameter_library_line), parameter_library_file) != NULL) { param_keyword = parse_param_line( parameter_library_line ); if (debug > 0) { pr(logFile, "DEBUG: parameter_library_line = %sDEBUG: param_keyword = %d\n", parameter_library_line, param_keyword); } switch (param_keyword) { case PAR_: case PAR_NULL: case PAR_COMMENT: break; case PAR_VDW: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_vdW); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the van der Waals term = \t%.4lf\n\n", AD4.coeff_vdW); break; case PAR_HBOND: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_hbond); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the H-bonding term = \t%.4lf\n\n", AD4.coeff_hbond); break; case PAR_ESTAT: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_estat); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the electrostatic term = \t%.4lf\n\n", AD4.coeff_estat); break; case PAR_DESOLV: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_desolv); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the desolvation term = \t%.4lf\n\n", AD4.coeff_desolv); break; case PAR_TORS: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_tors); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the torsional term = \t%.4lf\n\n", AD4.coeff_tors); break; case PAR_ATOM_PAR: // Read in one line of atom parameters; // NB: scanf doesn't try to write missing fields nfields = sscanf(parameter_library_line, "%*s %s %lf %lf %lf %lf %lf %lf %d %d %d %d", thisParameter.autogrid_type, &thisParameter.Rij, &thisParameter.epsij, &thisParameter.vol, &thisParameter.solpar, &thisParameter.Rij_hb, &thisParameter.epsij_hb, &int_hbond_type, &thisParameter.rec_index, &thisParameter.map_index, &thisParameter.bond_index); if (nfields < 2) { continue; // skip any parameter_library_line without enough info } if (int_hbond_type == 0) { thisParameter.hbond = NON; } else if (int_hbond_type == 1) { thisParameter.hbond = DS; } else if (int_hbond_type == 2) { thisParameter.hbond = D1; } else if (int_hbond_type == 3) { thisParameter.hbond = AS; } else if (int_hbond_type == 4) { thisParameter.hbond = A1; } else if (int_hbond_type == 5) { thisParameter.hbond = A2; } else { thisParameter.hbond = NON; } thisParameter.epsij *= AD4.coeff_vdW; thisParameter.epsij_hb *= AD4.coeff_hbond; apm_enter(thisParameter.autogrid_type, thisParameter); pr(logFile, "Parameters for the atom type named \"%s\" were read in from the parameter library as follows:\n", thisParameter.autogrid_type); if (outlev > 2) { pr(logFile, "\tR-eqm = %5.2f Angstrom\n\tweighted epsilon = %5.3f\n\tAtomic fragmental volume = %5.3f\n\tAtomic solvation parameter = %5.3f\n\tH-bonding R-eqm = %5.3f\n\tweighted H-bonding epsilon = %5.3f\n\tH-bonding type = %d, bond index = %d\n\n", thisParameter.Rij, thisParameter.epsij, thisParameter.vol, thisParameter.solpar, thisParameter.Rij_hb, thisParameter.epsij_hb, thisParameter.hbond, thisParameter.bond_index ); } else { pr(logFile, "\tR-eqm = %.2f Angstrom, weighted epsilon = %.3f, At.frag.vol. = %.3f, At.solv.par. = %.3f, \n\tHb R-eqm = %.3f, weighted Hb epsilon = %.3f, Hb type = %d, bond index = %d\n\n", thisParameter.Rij, thisParameter.epsij, thisParameter.vol, thisParameter.solpar, thisParameter.Rij_hb, thisParameter.epsij_hb, thisParameter.hbond, thisParameter.bond_index ); } break; default: break; } // switch } // while there is another line of parameters to read in }
void setup_parameter_library( int outlev ) { static ParameterEntry thisParameter; char parameter_library_line[MAX_CHARS]; int nfields; int param_keyword = -1; int int_hbond_type = 0; register int counter = 0; pr(logFile, "Setting up parameter library with factory defaults.\n\n\n"); // Default parameters // // These are set up in "default_parameters.h" // and stored in the param_string[MAX_LINES] array while ( param_string[counter] != NULL) { param_keyword = parse_param_line( param_string[counter] ); (void)strcpy(parameter_library_line, param_string[counter]); counter++; if (debug > 0) { pr(logFile, "DEBUG: parameter_library_line = %sDEBUG: param_keyword = %d\n", parameter_library_line, param_keyword); } switch (param_keyword) { case PAR_: case PAR_NULL: case PAR_COMMENT: break; case PAR_VDW: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_vdW); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the van der Waals term = \t%.4lf\n\n", AD4.coeff_vdW); break; case PAR_HBOND: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_hbond); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the H-bonding term = \t%.4lf\n\n", AD4.coeff_hbond); break; case PAR_ESTAT: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_estat); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the electrostatic term = \t%.4lf\n\n", AD4.coeff_estat); break; case PAR_DESOLV: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_desolv); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the desolvation term = \t%.4lf\n\n", AD4.coeff_desolv); break; case PAR_TORS: nfields = sscanf(parameter_library_line, "%*s %lf", &AD4.coeff_tors); if (nfields < 1) { pr( logFile, "%s: WARNING: Please supply a coefficient as a floating point number.\n\n", programname); continue; // skip any parameter_library_line without enough info } pr( logFile, "Free energy coefficient for the torsional term = \t%.4lf\n\n", AD4.coeff_tors); break; case PAR_ATOM_PAR: // Read in one line of atom parameters; // NB: scanf doesn't try to write missing fields nfields = sscanf(parameter_library_line, "%*s %s %lf %lf %lf %lf %lf %lf %d %d %d %d", thisParameter.autogrid_type, &thisParameter.Rij, &thisParameter.epsij, &thisParameter.vol, &thisParameter.solpar, &thisParameter.Rij_hb, &thisParameter.epsij_hb, &int_hbond_type, &thisParameter.rec_index, &thisParameter.map_index, &thisParameter.bond_index); if (nfields < 2) { continue; // skip any parameter_library_line without enough info } if (int_hbond_type == 0) { thisParameter.hbond = NON; } else if (int_hbond_type == 1) { thisParameter.hbond = DS; } else if (int_hbond_type == 2) { thisParameter.hbond = D1; } else if (int_hbond_type == 3) { thisParameter.hbond = AS; } else if (int_hbond_type == 4) { thisParameter.hbond = A1; } else if (int_hbond_type == 5) { thisParameter.hbond = A2; } else { thisParameter.hbond = NON; } thisParameter.epsij *= AD4.coeff_vdW; thisParameter.epsij_hb *= AD4.coeff_hbond; apm_enter(thisParameter.autogrid_type, thisParameter); pr(logFile, "Parameters for the atom type named \"%s\" were read in from the parameter library as follows:\n", thisParameter.autogrid_type); if (outlev > 2) { pr(logFile, "\tR-eqm = %5.2f Angstrom\n\tweighted epsilon = %5.3f\n\tAtomic fragmental volume = %5.3f\n\tAtomic solvation parameter = %5.3f\n\tH-bonding R-eqm = %5.3f\n\tweighted H-bonding epsilon = %5.3f\n\tH-bonding type = %d, bond index = %d\n\n", thisParameter.Rij, thisParameter.epsij, thisParameter.vol, thisParameter.solpar, thisParameter.Rij_hb, thisParameter.epsij_hb, thisParameter.hbond, thisParameter.bond_index ); } else { pr(logFile, "\tR-eqm = %.2f Angstrom, weighted epsilon = %.3f, At.frag.vol. = %.3f, At.solv.par. = %.3f, \n\tHb R-eqm = %.3f, weighted Hb epsilon = %.3f, Hb type = %d, bond index = %d\n\n", thisParameter.Rij, thisParameter.epsij, thisParameter.vol, thisParameter.solpar, thisParameter.Rij_hb, thisParameter.epsij_hb, thisParameter.hbond, thisParameter.bond_index ); } break; default: break; } // switch } // while there is another line of parameters to read in }