Skip to content

Commit c046a50

Browse files
authored
Use output path for nonlinear solver homotopy csv logging (#8107)
* Use output path for nonlinear solver homotopy csv logging * Always close csv file after writing
1 parent c32c675 commit c046a50

File tree

2 files changed

+84
-37
lines changed

2 files changed

+84
-37
lines changed

OMCompiler/SimulationRuntime/c/simulation/solver/initialization/initialization.c

Lines changed: 77 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,77 @@ void dumpInitialSolution(DATA *simData)
171171
messageClose(LOG_SOTI);
172172
}
173173

174+
175+
/**
176+
* @brief Write fileName into buffer.
177+
*
178+
* If FLAG_OUTPUT_PATH is used add output path to file name.
179+
*
180+
* @param buffer FileName on output.
181+
* @param fileName Name for CSV file.
182+
* @param mData Pointer to model data.
183+
*/
184+
void homotopy_log_file_path(char* buffer, const char* fileName, MODEL_DATA *mData)
185+
{
186+
if (omc_flag[FLAG_OUTPUT_PATH]) { /* Add output path to file name */
187+
sprintf(buffer, "%s/%s_%s", omc_flagValue[FLAG_OUTPUT_PATH], mData->modelFilePrefix, fileName);
188+
}
189+
else
190+
{
191+
sprintf(buffer, "%s_%s", mData->modelFilePrefix, fileName);
192+
}
193+
infoStreamPrint(LOG_INIT_HOMOTOPY, 0, "The homotopy path will be exported to %s.", buffer);
194+
return;
195+
}
196+
197+
/**
198+
* @brief Log lambda and all real variables in homotopy CSV file
199+
*
200+
* @param data Pointer to DATA.
201+
* @param threadData Pointer to threadData.
202+
* @param fileName Name of CSV file to write to.
203+
* @param sep CSV Seperator (usually ",").
204+
* @param lambda Value of lambda.
205+
* @param firstLine Boolean specifying if header of CSV should be written.
206+
*/
207+
void log_homotopy_lambda_vars(DATA *data, threadData_t *threadData, const char* fileName, const char* sep, double lambda, int firstLine)
208+
{
209+
int i;
210+
FILE* pFile;
211+
212+
/* Open file */
213+
if (firstLine) {
214+
pFile = omc_fopen(fileName, "wt");
215+
}
216+
else {
217+
pFile = omc_fopen(fileName, "at");
218+
}
219+
if (pFile == NULL)
220+
{
221+
throwStreamPrint(threadData, "Could not write to `%s`.", fileName);
222+
}
223+
224+
/* Write to file */
225+
if (firstLine) {
226+
fprintf(pFile, "\"lambda\"");
227+
for(i=0; i<data->modelData->nVariablesReal; ++i)
228+
{
229+
fprintf(pFile, "%s\"%s\"", sep, data->modelData->realVarsData[i].info.name);
230+
}
231+
fprintf(pFile, "\n");
232+
} else {
233+
fprintf(pFile, "%.16g", lambda);
234+
for(i=0; i<data->modelData->nVariablesReal; ++i)
235+
{
236+
fprintf(pFile, "%s%.16g", sep, data->localData[0]->realVars[i]);
237+
}
238+
fprintf(pFile, "\n");
239+
}
240+
241+
fclose(pFile);
242+
return;
243+
}
244+
174245
/*! \fn static int symbolic_initialization(DATA *data, threadData_t *threadData)
175246
*
176247
* \param [ref] [data]
@@ -184,6 +255,8 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
184255
TRACE_PUSH
185256
int retVal;
186257
FILE *pFile = NULL;
258+
char fileName[4096];
259+
const char* sep = ",";
187260
long i;
188261
MODEL_DATA *mData = data->modelData;
189262
int homotopySupport = 0;
@@ -273,34 +346,16 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
273346
if (data->callback->useHomotopy == 1 && solveWithGlobalHomotopy)
274347
{
275348
long step;
276-
char buffer[4096];
277-
double lambda;
349+
double lambda = -1;
278350
int success = 0;
279351

280352
infoStreamPrint(LOG_INIT_HOMOTOPY, 0, "Global homotopy with equidistant step size started.");
281353

282354
#if !defined(OMC_NO_FILESYSTEM)
283-
const char sep[] = ",";
284355
if(ACTIVE_STREAM(LOG_INIT_HOMOTOPY))
285356
{
286-
if (omc_flag[FLAG_OUTPUT_PATH]) { /* Add output path to file name */
287-
sprintf(buffer, "%s/%s_equidistant_global_homotopy.csv", omc_flagValue[FLAG_OUTPUT_PATH], mData->modelFilePrefix);
288-
}
289-
else
290-
{
291-
sprintf(buffer, "%s_equidistant_global_homotopy.csv", mData->modelFilePrefix);
292-
}
293-
infoStreamPrint(LOG_INIT_HOMOTOPY, 0, "The homotopy path will be exported to %s.", buffer);
294-
pFile = omc_fopen(buffer, "wt");
295-
if (pFile == NULL)
296-
{
297-
throwStreamPrint(threadData, "Could not write to `%s`.", buffer);
298-
}
299-
fprintf(pFile, "\"lambda\"");
300-
for(i=0; i<mData->nVariablesReal; ++i) {
301-
fprintf(pFile, "%s\"%s\"", sep, mData->realVarsData[i].info.name);
302-
}
303-
fprintf(pFile, "\n");
357+
homotopy_log_file_path(fileName, "equidistant_global_homotopy.csv", mData);
358+
log_homotopy_lambda_vars(data, threadData, fileName, sep, lambda, 1 /*TRUE*/);
304359
}
305360
#endif
306361

@@ -343,12 +398,7 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
343398
#if !defined(OMC_NO_FILESYSTEM)
344399
if(ACTIVE_STREAM(LOG_INIT_HOMOTOPY))
345400
{
346-
fprintf(pFile, "%.16g", lambda);
347-
for(i=0; i<mData->nVariablesReal; ++i)
348-
{
349-
fprintf(pFile, "%s%.16g", sep, data->localData[0]->realVars[i]);
350-
}
351-
fprintf(pFile, "\n");
401+
log_homotopy_lambda_vars(data, threadData, fileName, sep, lambda, 0 /*FALSE*/);
352402
}
353403
#endif
354404
}
@@ -361,21 +411,12 @@ static int symbolic_initialization(DATA *data, threadData_t *threadData)
361411
if (!success)
362412
{
363413
messageClose(LOG_INIT_HOMOTOPY);
364-
#if !defined(OMC_NO_FILESYSTEM)
365-
if(ACTIVE_STREAM(LOG_INIT_HOMOTOPY))
366-
fclose(pFile);
367-
#endif
368414
errorStreamPrint(LOG_ASSERT, 0, "Failed to solve the initialization problem with global homotopy with equidistant step size.");
369415
throwStreamPrint(threadData, "Unable to solve initialization problem.");
370416
}
371417

372418
data->simulationInfo->homotopySteps += init_lambda_steps;
373419
messageClose(LOG_INIT_HOMOTOPY);
374-
375-
#if !defined(OMC_NO_FILESYSTEM)
376-
if(ACTIVE_STREAM(LOG_INIT_HOMOTOPY))
377-
fclose(pFile);
378-
#endif
379420
}
380421

381422
/* If there is homotopy in the model and the adaptive global homotopy approach is activated

OMCompiler/SimulationRuntime/c/simulation/solver/nonlinearSolverHomotopy.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,13 @@ static int homotopyAlgorithm(DATA_HOMOTOPY* solverData, double *x)
17051705
const char sep[] = ",";
17061706
if(solverData->initHomotopy && ACTIVE_STREAM(LOG_INIT_HOMOTOPY))
17071707
{
1708-
sprintf(buffer, "%s_nonlinsys%d_adaptive_%s_homotopy_%s.csv", solverData->data->modelData->modelFilePrefix, solverData->sysNumber, solverData->data->callback->useHomotopy == 2 ? "global" : "local", solverData->startDirection > 0 ? "pos" : "neg");
1708+
if (omc_flag[FLAG_OUTPUT_PATH]) { /* Add output path to file name */
1709+
sprintf(buffer, "%s/%s_nonlinsys%d_adaptive_%s_homotopy_%s.csv", omc_flagValue[FLAG_OUTPUT_PATH], solverData->data->modelData->modelFilePrefix, solverData->sysNumber, solverData->data->callback->useHomotopy == 2 ? "global" : "local", solverData->startDirection > 0 ? "pos" : "neg");
1710+
}
1711+
else
1712+
{
1713+
sprintf(buffer, "%s_nonlinsys%d_adaptive_%s_homotopy_%s.csv", solverData->data->modelData->modelFilePrefix, solverData->sysNumber, solverData->data->callback->useHomotopy == 2 ? "global" : "local", solverData->startDirection > 0 ? "pos" : "neg");
1714+
}
17091715
infoStreamPrint(LOG_INIT_HOMOTOPY, 0, "The homotopy path will be exported to %s.", buffer);
17101716
pFile = omc_fopen(buffer, "wt");
17111717
fprintf(pFile, "\"sep=%s\"\n%s", sep, "\"lambda\"");

0 commit comments

Comments
 (0)