previous next Up Title Contents

Reading the Main Parameters

It is a general practise in scientific programming to read process parameters from files. As stated above each module has its own set of parameters which need to be set, and so each module can read its own specific parameter files, names of which may be given in the test program's parameter file. Stage (ii) of the standard program format is the reading of a program's general parameters from file. This operation is carried out by the ReadParsFromFile(...) routine, which is specific to a particular simulation program. All of the simulation programs supplied with LUTEar 2.0.8 have a ReadParsFromFile(...) routine. The routine always has the same basic format, what changes is the number and type of parameters read. The ReadParsFromFile(...) routine for the "AudPerGen.c" program is shown in figure [14].

void
ReadParsFromFile(char *fileName)
{
	FILE	*fp;
	
	if ((fp = fopen(fileName, "r")) == NULL) {
		NotifyError("ReadTestPars: Cannot open data file '%s'.\n", fileName);
		exit(1);
	}
	printf("Reading parameters from file: %s\n", fileName);
	Init_ParFile();
	GetPars_ParFile(fp, "%s", outputFile);
	GetPars_ParFile(fp, "%s %s", stParFile, stModuleName);
	GetPars_ParFile(fp, "%s %s", pEParFile, pEModuleName);
	GetPars_ParFile(fp, "%s %s", bMParFile, bMModuleName);
	GetPars_ParFile(fp, "%s %s", rPParFile, rPModuleName);
	GetPars_ParFile(fp, "%s %s", hCParFile, hCModuleName);
	GetPars_ParFile(fp, "%lf", &rampInterval);
	fclose(fp);
	Free_ParFile();
	
}

Figure [14]: AudPerGen.c - Reading the Main Parameters

The routine uses the ParFile module to interrogate an ASCII character file which contains the parameters. The parameter file format is quite unrestrictive; the user decides how the parameters are set out in the file, and then formats the ReadParsFromFile(...) accordingly. The ParFile module is first initialised using the Init_ParFile() routine. This sets up the system which checks the parameter file reading, i.e. to check that the correct number of parameters is read from a file. The parameters can then be read using the GetPars_ParFile(...) routine. The GetPars_ParFile(...) routine is used in much the same way as the standard fscanf(...) ANSI C library function, as shown in figure [15].3

Figure [15]: The GetPars_ParFile(...) routine

The GetPars_ParFile(...) routine will ignore all lines that begin with a hash, "#", sign and will ignore the rest of the characters on a line after the specified variable(s) has/have been read. The ParFile module should always be closed at the end of parameter reading using the Free_ParFile() routine.

In this example program, as in all the programs supplied with LUTEar 2.0.9 , the main parameters, read from file, are declared as global variables. This allows us to use a routine to read the parameters, rather than having to read them from the main body of the program and introducing extraneous code in the main simulation procedure.


previous next Up Title Contents