From 4a1f98488398d09099edbfa405dbe1af0c7dd6cc Mon Sep 17 00:00:00 2001 From: RoryBarnes Date: Mon, 12 Aug 2024 18:40:44 -0700 Subject: [PATCH] Refactoring ReadInitialOptions... --- src/control.c | 38 ++++++++++++++++++++++----- src/control.h | 2 +- src/options.c | 72 +++++++++++++++++++++------------------------------ src/output.c | 5 ++++ src/system.c | 4 +++ src/system.h | 2 ++ src/vplanet.h | 2 +- 7 files changed, 74 insertions(+), 51 deletions(-) diff --git a/src/control.c b/src/control.c index b5053db23..c3ff30453 100644 --- a/src/control.c +++ b/src/control.c @@ -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;iOptionOutfile[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) { diff --git a/src/control.h b/src/control.h index 34bfb2375..7795ce881 100644 --- a/src/control.h +++ b/src/control.h @@ -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); diff --git a/src/options.c b/src/options.c index 99cd583ee..d3c502ff8 100644 --- a/src/options.c +++ b/src/options.c @@ -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, @@ -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) { @@ -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); } @@ -1196,8 +1181,9 @@ 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; @@ -1205,21 +1191,19 @@ void ReadInitialOptions(BODY **body, CONTROL *control, FILES *files, /* 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]); @@ -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)); @@ -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); @@ -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; @@ -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. */ diff --git a/src/output.c b/src/output.c index fef08f119..0049f80fa 100644 --- a/src/output.c +++ b/src/output.c @@ -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 */ diff --git a/src/system.c b/src/system.c index 4c7d7a22b..21a598107 100644 --- a/src/system.c +++ b/src/system.c @@ -7,6 +7,10 @@ #include "vplanet.h" +void InitializeSystem(BODY *body,CONTROL *control,SYSTEM *system) { + system->iNumBodies = control->Evolve.iNumBodies; +} + /* * Physical Relationships */ diff --git a/src/system.h b/src/system.h index f481529c5..c38dabb4a 100644 --- a/src/system.h +++ b/src/system.h @@ -7,6 +7,8 @@ /* @cond DOXYGEN_OVERRIDE */ +void InitializeSystem(BODY *,CONTROL *,SYSTEM *); + double fdSemiToPeriod(double, double); double fdPeriodToSemi(double, double); double fdSemiToMeanMotion(double, double); diff --git a/src/vplanet.h b/src/vplanet.h index a784ac15d..309341c5b 100644 --- a/src/vplanet.h +++ b/src/vplanet.h @@ -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 =