Skip to content

Commit

Permalink
Refactoring ReadInitialOptions...
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryBarnes committed Aug 13, 2024
1 parent 74609fc commit 4a1f984
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 51 deletions.
38 changes: 32 additions & 6 deletions src/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,44 @@ void sort_output(OUTPUT *output, int sorted[]) {
* Struct Initialization
*/

void InitializeFiles(FILES *files, int iNumBodies) {
int iBody,iOption;
void InitializeFiles(FILES *files, OPTIONS **options, char **saBodyFiles, int iNumBodies) {
int iFile,iOption,iNumFiles;

iNumFiles=iNumBodies-1;

/* With body files identified, must allocate space */
files->Infile = malloc(files->iNumInputs * sizeof(INFILE));
files->Infile[0].bLineOK = malloc(files->Infile[0].iNumLines * sizeof(int));

//InfileCopy(&files->Infile[0], infile);

files->Outfile = malloc(iNumFiles * sizeof(OUTFILE));
// for (iIndex = 0; iIndex < iNumIndices; iIndex++) {
// memset(files->Outfile[iIndex].cOut, '\0', NAMELEN);
// }

files->cLog = NULL;
files->cExe = NULL;
for (iBody = 0; iBody < iNumBodies; iBody++) {
// Infile must be initilized in ReadBodyNames
files->Outfile[iBody].cOut = NULL;

for (iFile = 0; iFile < iNumFiles; iFile++) {
files->Infile[iFile].cIn = NULL;
fvFormattedString(&files->Infile[iFile].cIn, saBodyFiles[iFile]);

files->Outfile[iFile].cOut = NULL;
// Outfile names assigned after reading in output file names

for (iOption=0;iOption<MODULEOPTEND;iOption++) {
files->Outfile[iBody].caGrid[iOption] = NULL;
files->Outfile[iFile].caGrid[iOption] = NULL;

options[iOption]->iLine[iFile] = -1;
options[iOption]->cFile[iFile] = NULL;
options[iOption]->cFile = malloc(iNumFiles*sizeof(char*));
// memset(options[iOpt].cFile[iFile], '\0', OPTLEN);
fvFormattedString(&options[iOption]->cFile[iFile], "null");
}
}


}

void InitializePropsAux(CONTROL *control, MODULE *module) {
Expand Down
2 changes: 1 addition & 1 deletion src/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void InitializeControl(CONTROL *, MODULE *);
void InitializeControlEvolve(BODY *, CONTROL *, MODULE *, UPDATE *);
void InitializeControlVerifyProperty(CONTROL *);

void InitializeFiles(FILES *, int);
void InitializeFiles(FILES *, OPTIONS **, char **, int);

void WriteHelpOption(OPTIONS *, int);
void WriteHelpOutput(OUTPUT *, int);
Expand Down
72 changes: 29 additions & 43 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,10 +582,11 @@ void UpdateFoundOptionMulti(INFILE *input, OPTIONS *options, int *iLine,
The user should be able to figure it out from there.
*/
options->iLine[iFile] = iLine[0];
fvFormattedString(&options->cFile[iFile], input->cIn);
for (iLineNow = 0; iLineNow < iNumLines; iLineNow++) {
input->bLineOK[iLine[iLineNow]] = 1;
}

fvFormattedString(&options->cFile[iFile], input->cIn);
}

void CheckDuplication(FILES *files, OPTIONS *options, char cFile[], int iLine,
Expand Down Expand Up @@ -1131,16 +1132,16 @@ void ReadSystemName(CONTROL *control, FILES *files, OPTIONS *options,
}
}

void ReadBodyFileNames(CONTROL *control, FILES *files, OPTIONS *options,
INFILE *infile) {
int iIndex, iNumIndices = 0, iNumLines = 0;
void ReadBodyFileNames(BODY **body,CONTROL *control, FILES *files, OPTIONS *options,
INFILE *infile, char ***saBodyFiles, int *iStartLine, int *iNumLines) {
int iIndex, iNumIndices = 0;
int *lTmp;
char **saTmp;

lTmp = malloc(MAXLINES * sizeof(int));
*iNumLines = 0;

AddOptionStringArray(infile->cIn, options->cName, &saTmp, &iNumIndices,
&iNumLines, lTmp, control->Io.iVerbose);
AddOptionStringArray(infile->cIn, options->cName, saBodyFiles, &iNumIndices,
iNumLines, lTmp, control->Io.iVerbose);

if (lTmp[0] >= 0) {
if (iNumIndices == 0) {
Expand All @@ -1166,25 +1167,9 @@ void ReadBodyFileNames(CONTROL *control, FILES *files, OPTIONS *options,
exit(EXIT_INPUT);
}

/* With body files identified, must allocate space */
files->Infile = malloc(files->iNumInputs * sizeof(INFILE));
files->Infile[0].bLineOK = malloc(infile->iNumLines * sizeof(int));

InfileCopy(&files->Infile[0], infile);

for (iIndex = 0; iIndex < iNumIndices; iIndex++) {
files->Infile[iIndex + 1].cIn = NULL;
fvFormattedString(&files->Infile[iIndex + 1].cIn, saTmp[iIndex]);
}

control->Evolve.iNumBodies = iNumIndices;
files->Outfile = malloc(iNumIndices * sizeof(OUTFILE));
// for (iIndex = 0; iIndex < iNumIndices; iIndex++) {
// memset(files->Outfile[iIndex].cOut, '\0', NAMELEN);
// }

UpdateFoundOptionMulti(&files->Infile[0], options, lTmp, iNumLines, 0);

*body = malloc(control->Evolve.iNumBodies * sizeof(BODY));
*iStartLine = lTmp[0];
free(lTmp);
}

Expand All @@ -1196,30 +1181,29 @@ void ReadBodyFileNames(CONTROL *control, FILES *files, OPTIONS *options,

void ReadInitialOptions(BODY **body, CONTROL *control, FILES *files,
MODULE *module, OPTIONS *options, OUTPUT *output,
SYSTEM *system, char infile[]) {
int iFile, iBody, iModule;
SYSTEM *system, char cInfile[]) {
int iFile, iBody, iModule,iBodyFileLine,iNumLines;
char **saBodyFiles;
INFILE input;

input.cIn = NULL;
fvFormattedString(&input.cIn, infile);
/* Initialize primary input file */
InitializeInput(&input);

/* First find input files */
ReadBodyFileNames(control, files, &options[OPT_BODYFILES], &input);
system->iNumBodies = control->Evolve.iNumBodies;
/* Read sBodyFiles, which must be in the primary file */
ReadBodyFileNames(body, control, files, &options[OPT_BODYFILES], &input, &saBodyFiles, &iBodyFileLine, &iNumLines);

// allocate the body struct
*body = malloc(control->Evolve.iNumBodies * sizeof(BODY));
InitializeFiles(files, &options, saBodyFiles, control->Evolve.iNumBodies);

UpdateFoundOptionMulti(&files->Infile[0], options, &iBodyFileLine, iNumLines, 0);

/* Initialize functions in the module struct */
InitializeModule(*body, control, module);

/* Is iVerbose set in primary input? */
/* Is iVerbose set ? */
ReadVerbose(files, &options[OPT_VERBOSE], &control->Io.iVerbose, 0);

/* Now we can search through files for all options. First we scan the files
* for Verbosity */
/* We have to initialize other input files first */
for (iFile = 1; iFile < files->iNumInputs; iFile++) {
InitializeInput(&files->Infile[iFile]);
Expand Down Expand Up @@ -3656,6 +3640,8 @@ void ReadOptions(BODY **body, CONTROL *control, FILES *files, MODULE *module,
ReadInitialOptions(body, control, files, module, options, output, system,
infile);

InitializeSystem(*body, control,system);

/* Now that we know how many bodies there are, initialize more features */
*update = malloc(control->Evolve.iNumBodies * sizeof(UPDATE));

Expand All @@ -3665,8 +3651,6 @@ void ReadOptions(BODY **body, CONTROL *control, FILES *files, MODULE *module,
/* Initialize module control */
InitializeControl(control, module);

InitializeFiles(files, control->Evolve.iNumBodies);

/* Now read in multi-module options */
ReadOptionsGeneral(*body, control, files, module, options, output, system,
fnRead);
Expand Down Expand Up @@ -4716,7 +4700,15 @@ void InitializeOptions(OPTIONS *options, fnReadOption *fnRead) {
/* Initialize all parameters describing the option's location */
for (iOpt = 0; iOpt < MODULEOPTEND; iOpt++) {
// memset(options[iOpt].cName, '\0', OPTLEN);

options[iOpt].cName = NULL;
options[iOpt].cDescr = NULL;
options[iOpt].cLongDescr = NULL;
options[iOpt].cDefault = NULL;
options[iOpt].cValues = NULL;
options[iOpt].cNeg = NULL;
options[iOpt].cDimension = NULL;

fvFormattedString(&options[iOpt].cName, "null");
options[iOpt].iLine = malloc(MAXFILES * sizeof(int));
options[iOpt].bMultiFile = 0;
Expand All @@ -4738,12 +4730,6 @@ void InitializeOptions(OPTIONS *options, fnReadOption *fnRead) {
options[iOpt].bNeg = 0;
options[iOpt].iFileType = 2;
options[iOpt].dNeg = 0;

for (iFile = 0; iFile < MAXFILES; iFile++) {
options[iOpt].iLine[iFile] = -1;
// memset(options[iOpt].cFile[iFile], '\0', OPTLEN);
fvFormattedString(&options[iOpt].cFile[iFile], "null");
}
}

/* Now populate entries for general options. */
Expand Down
5 changes: 5 additions & 0 deletions src/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,11 @@ void InitializeOutput(FILES *files, OUTPUT *output, fnWriteOutput fnWrite[]) {

for (iOut = 0; iOut < MODULEOUTEND; iOut++) {
//memset(output[iOut].cName, '\0', OPTLEN);
output[iOut].cName = NULL;
output[iOut].cDescr = NULL;
output[iOut].cLongDescr = NULL;
output[iOut].cNeg = NULL;

fvFormattedString(&output[iOut].cName, "null");
output[iOut].bGrid = 0;
output[iOut].bNeg = 0; /* Is a negative option allowed */
Expand Down
4 changes: 4 additions & 0 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include "vplanet.h"

void InitializeSystem(BODY *body,CONTROL *control,SYSTEM *system) {
system->iNumBodies = control->Evolve.iNumBodies;
}

/*
* Physical Relationships
*/
Expand Down
2 changes: 2 additions & 0 deletions src/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/* @cond DOXYGEN_OVERRIDE */

void InitializeSystem(BODY *,CONTROL *,SYSTEM *);

double fdSemiToPeriod(double, double);
double fdPeriodToSemi(double, double);
double fdSemiToMeanMotion(double, double);
Expand Down
2 changes: 1 addition & 1 deletion src/vplanet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1963,7 +1963,7 @@ struct OPTIONS {
int iMultiIn;
int *iLine; /**< Option's Line number in Input File */
char *iFile;
char *cFile[MAXFILES]; /**< File Name Where Set */
char **cFile; /**< File Name Where Set */
int bNeg; /**< Is There a Negative Option? */
char *cNeg; /**< Description of Negative Unit Conversion */
int iFileType; /**< What type of file can option be in? 0 = primary only, 1 =
Expand Down

0 comments on commit 4a1f984

Please sign in to comment.