|
44 | 44 | #include "newtonIteration.h" |
45 | 45 | #endif |
46 | 46 | #include "nonlinearSolverHomotopy.h" |
| 47 | +#include "simulation/options.h" |
47 | 48 | #include "simulation/simulation_info_json.h" |
48 | 49 | #include "simulation/simulation_runtime.h" |
49 | 50 | #include "simulation/solver/model_help.h" |
@@ -817,54 +818,67 @@ int solve_nonlinear_system(DATA *data, threadData_t *threadData, int sysNumber) |
817 | 818 | (data->callback->useHomotopy == 0) && init_lambda_steps > 1) |
818 | 819 | lambda_steps = init_lambda_steps; |
819 | 820 |
|
820 | | -#if !defined(OMC_NO_FILESYSTEM) |
821 | | - if(lambda_steps > 1 && ACTIVE_STREAM(LOG_INIT)) |
822 | | - { |
823 | | - sprintf(buffer, "%s_homotopy_nls_%d.csv", data->modelData->modelFilePrefix, sysNumber); |
824 | | - infoStreamPrint(LOG_INIT, 0, "The homotopy path of system %d will be exported to %s.", sysNumber, buffer); |
825 | | - pFile = fopen(buffer, "wt"); |
826 | | - fprintf(pFile, "\"sep=,\"\n%s", "lambda"); |
827 | | - for(j=0; j<nonlinsys->size; ++j) |
828 | | - fprintf(pFile, ",%s", modelInfoGetEquation(&data->modelData->modelDataXml, nonlinsys->equationIndex).vars[j]); |
829 | | - fprintf(pFile, "\n"); |
| 821 | + nonlinsys->solved = 0; |
| 822 | + if (lambda_steps == 1 || !omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY]) { |
| 823 | + |
| 824 | + if(data->callback->useHomotopy == 0) |
| 825 | + data->simulationInfo->lambda = 1.0; |
| 826 | + |
| 827 | + if (lambda_steps > 1) |
| 828 | + infoStreamPrint(LOG_INIT, 0, "Try to solve initial system %d without homotopy first.", sysNumber); |
| 829 | + |
| 830 | + /* SOLVE! */ |
| 831 | + nonlinsys->solved = solveNLS(data, threadData, sysNumber); |
830 | 832 | } |
831 | | -#endif |
832 | 833 |
|
833 | | - for(step=0; step<lambda_steps-1; ++step) |
834 | | - { |
835 | | - data->simulationInfo->lambda = ((double)step)/(lambda_steps-1); |
836 | | - infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g", sysNumber, data->simulationInfo->lambda); |
837 | | - solveNLS(data, threadData, sysNumber); |
| 834 | + if (lambda_steps > 1 && !nonlinsys->solved) { |
| 835 | + if (!omc_flag[FLAG_HOMOTOPY_ON_FIRST_TRY]) |
| 836 | + warningStreamPrint(LOG_ASSERT, 0, "Failed to solve initial system %d without homotopy method. The homotopy method is used now.", sysNumber); |
838 | 837 |
|
839 | 838 | #if !defined(OMC_NO_FILESYSTEM) |
840 | 839 | if(ACTIVE_STREAM(LOG_INIT)) |
841 | 840 | { |
842 | | - infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g done\n---------------------------", sysNumber, data->simulationInfo->lambda); |
843 | | - fprintf(pFile, "%.16g", data->simulationInfo->lambda); |
| 841 | + sprintf(buffer, "%s_homotopy_nls_%d.csv", data->modelData->modelFilePrefix, sysNumber); |
| 842 | + infoStreamPrint(LOG_INIT, 0, "The homotopy path of system %d will be exported to %s.", sysNumber, buffer); |
| 843 | + pFile = fopen(buffer, "wt"); |
| 844 | + fprintf(pFile, "\"sep=,\"\n%s", "lambda"); |
844 | 845 | for(j=0; j<nonlinsys->size; ++j) |
845 | | - fprintf(pFile, ",%.16g", nonlinsys->nlsx[j]); |
| 846 | + fprintf(pFile, ",%s", modelInfoGetEquation(&data->modelData->modelDataXml, nonlinsys->equationIndex).vars[j]); |
846 | 847 | fprintf(pFile, "\n"); |
847 | 848 | } |
848 | 849 | #endif |
849 | | - } |
850 | 850 |
|
851 | | - if(data->callback->useHomotopy == 0) |
852 | | - data->simulationInfo->lambda = 1.0; |
| 851 | + for(step=0; step<lambda_steps; ++step) |
| 852 | + { |
| 853 | + data->simulationInfo->lambda = ((double)step)/(lambda_steps-1); |
| 854 | + |
| 855 | + if(data->simulationInfo->lambda > 1.0) { |
| 856 | + data->simulationInfo->lambda = 1.0; |
| 857 | + } |
| 858 | + |
| 859 | + infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g", sysNumber, data->simulationInfo->lambda); |
| 860 | + nonlinsys->solved = solveNLS(data, threadData, sysNumber); |
853 | 861 |
|
854 | | - /* SOLVE! */ |
855 | | - nonlinsys->solved = solveNLS(data, threadData, sysNumber); |
| 862 | +#if !defined(OMC_NO_FILESYSTEM) |
| 863 | + if(ACTIVE_STREAM(LOG_INIT)) |
| 864 | + { |
| 865 | + infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g done\n---------------------------", sysNumber, data->simulationInfo->lambda); |
| 866 | + fprintf(pFile, "%.16g", data->simulationInfo->lambda); |
| 867 | + for(j=0; j<nonlinsys->size; ++j) |
| 868 | + fprintf(pFile, ",%.16g", nonlinsys->nlsx[j]); |
| 869 | + fprintf(pFile, "\n"); |
| 870 | + } |
| 871 | +#endif |
| 872 | + } |
856 | 873 |
|
857 | 874 | #if !defined(OMC_NO_FILESYSTEM) |
858 | | - if(lambda_steps > 1 && ACTIVE_STREAM(LOG_INIT)) |
859 | | - { |
860 | | - infoStreamPrint(LOG_INIT, 0, "[system %d] homotopy parameter lambda = %g done\n---------------------------", sysNumber, data->simulationInfo->lambda); |
861 | | - fprintf(pFile, "%.16g", data->simulationInfo->lambda); |
862 | | - for(j=0; j<nonlinsys->size; ++j) |
863 | | - fprintf(pFile, ",%.16g", nonlinsys->nlsx[j]); |
864 | | - fprintf(pFile, "\n"); |
865 | | - fclose(pFile); |
866 | | - } |
| 875 | + if(lambda_steps > 1 && ACTIVE_STREAM(LOG_INIT)) |
| 876 | + { |
| 877 | + fclose(pFile); |
| 878 | + } |
867 | 879 | #endif |
| 880 | + data->simulationInfo->homotopyUsed = 1; |
| 881 | + } |
868 | 882 |
|
869 | 883 | /* handle asserts */ |
870 | 884 | threadData->currentErrorStage = saveJumpState; |
|
0 commit comments