lbl-srg / modelica-buildings

Modelica Buildings library
258 stars 160 forks source link

FFD models fail in OCT, likely caused by destructor #2231

Open mwetter opened 4 years ago

mwetter commented 4 years ago

The models Buildings.ThermalZones.Detailed.Examples.FFD.ForcedConvection and Buildings.ThermalZones.Detailed.Examples.FFD.Tutorial.MixedConvection

fail with OPTIMICA, OCT-stable-r19089_JM-r14295. See below for the output. It looks like freeing memory fails.

I will put it on the exclude list until this is fixed.

$ jm_ipython.sh jmodelica.py Buildings.ThermalZones.Detailed.Examples.FFD.ForcedConvection
*** Compiling Buildings.ThermalZones.Detailed.Examples.FFD.ForcedConvection
Warning at line 49, column 12, in file '/home/mwetter/proj/ldrd/bie/modeling/github/lbl-srg/modelica-buildings/Buildings/Fluid/Sources/BaseClasses/PartialSource.mo',
In components:
    bouOut
    bounIn
  The cardinality() function-like operator is deprecated, and will be removed in a future version of Modelica
Warning at line 89, column 3, in file '/home/mwetter/proj/ldrd/bie/modeling/github/lbl-srg/modelica-buildings/Buildings/ThermalZones/Detailed/BaseClasses/CFDAirHeatMassBalance.mo',
In component roo:
  The parameter roo.air.C_outflow_fixed does not have a binding expression
/opt/oct/lib/RuntimeLibrary/libModelicaExternalC.a(ModelicaInternal.c.o): In function `ModelicaInternal_temporaryFileName':
/opt/OCT/P538-OCT/JModelica/ThirdParty/MSL/Modelica/Resources/C-Sources/ModelicaInternal.c:622: warning: the use of `tmpnam' is dangerous, better use `mkstemp'
In file included from sources/Buildings_ThermalZones_Detailed_Examples_FFD_ForcedConvection_funcs.c:5:0:
/home/mwetter/proj/ldrd/bie/modeling/github/lbl-srg/modelica-buildings/Buildings/Resources/C-Sources/cfdStartCosimulation.c: In function ‘cfdStartCosimulation’:
/home/mwetter/proj/ldrd/bie/modeling/github/lbl-srg/modelica-buildings/Buildings/Resources/C-Sources/cfdStartCosimulation.c:83:23: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
   cosim->para->bouCon = (size_t *) malloc(nSur*sizeof(size_t));
                       ^
sources/Buildings_ThermalZones_Detailed_Examples_FFD_ForcedConvection_funcs.c: In function ‘func_Modelica_Utilities_Streams_readLine_def6’:
sources/Buildings_ThermalZones_Detailed_Examples_FFD_ForcedConvection_funcs.c:203:14: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
     string_v = ModelicaInternal_readLine(fileName_v, tmp_1, &tmp_2);
              ^
sources/Buildings_ThermalZones_Detailed_Examples_FFD_ForcedConvection_funcs.c: In function ‘func_Modelica_Utilities_Strings_substring_def10’:
sources/Buildings_ThermalZones_Detailed_Examples_FFD_ForcedConvection_funcs.c:292:14: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
     result_v = ModelicaStrings_substring(string_v, tmp_1, tmp_2);
              ^
/opt/oct/lib/RuntimeLibrary/libModelicaExternalC.a(ModelicaInternal.c.o): In function `ModelicaInternal_temporaryFileName':
/opt/OCT/P538-OCT/JModelica/ThirdParty/MSL/Modelica/Resources/C-Sources/ModelicaInternal.c:622: warning: the use of `tmpnam' is dangerous, better use `mkstemp'
/opt/oct/lib/RuntimeLibrary/libModelicaMatIO.a(ModelicaMatIO.c.o): In function `Mat_VarDelete':
/opt/OCT/P538-OCT/JModelica/ThirdParty/MSL/Modelica/Resources/C-Sources/ModelicaMatIO.c:4876: warning: the use of `mktemp' is dangerous, better use `mkstemp' or `mkdtemp'

Start Fast Fluid Dynamics Simulation with Thread
WARNING:root:No dependency information for the derivatives was found in the model description. Assuming complete dependency.
Final Run Statistics: --- e+01

 Number of steps                                 : 529
 Number of function evaluations                  : 606
 Number of Jacobian evaluations                  : 20
 Number of function eval. due to Jacobian eval.  : 0
 Number of error test failures                   : 0
 Number of nonlinear iterations                  : 529
 Number of nonlinear convergence failures        : 0
 Number of state function evaluations            : 549
 Number of time events                           : 20

Solver options:

 Solver                   : CVode
 Linear multistep method  : BDF
 Nonlinear solver         : Newton
 Linear solver type       : DENSE
 Maximal order            : 5
 Tolerances (absolute)    : 1e-08
 Tolerances (relative)    : 1e-08

Simulation interval    : 0.0 - 120.0 seconds.
Elapsed simulation time: 20.116137761011487 seconds.
malloc_consolidate(): invalid chunk size
/opt/oct/bin/jm_ipython.sh: line 28:  7696 Aborted                 (core dumped) IPOPT_HOME=${JMODELICA_HOME}/ThirdParty/IPOPT SUNDIALS_HOME=${JMODELICA_HOME}/ThirdParty/Sundials PYTHONPATH=:${JMODELICA_HOME}/Python/::$PYTHONPATH LD_LIBRARY_PATH=:${JMODELICA_HOME}/ThirdParty/IPOPT/lib/:${JMODELICA_HOME}/ThirdParty/Sundials/lib:${JMODELICA_HOME}/ThirdParty/CasADi/lib:$LD_LIBRARY_PATH SEPARATE_PROCESS_JVM=${JAVA_HOME} ipython "$@"

and

$ jm_ipython.sh jmodelica.py Buildings.ThermalZones.Detailed.Examples.FFD.Tutorial.MixedConvection
*** Compiling Buildings.ThermalZones.Detailed.Examples.FFD.Tutorial.MixedConvection
Warning at line 49, column 12, in file '/home/mwetter/proj/ldrd/bie/modeling/github/lbl-srg/modelica-buildings/Buildings/Fluid/Sources/BaseClasses/PartialSource.mo',
In components:
    bouIn
    bouOut
  The cardinality() function-like operator is deprecated, and will be removed in a future version of Modelica
Warning at line 89, column 3, in file '/home/mwetter/proj/ldrd/bie/modeling/github/lbl-srg/modelica-buildings/Buildings/ThermalZones/Detailed/BaseClasses/CFDAirHeatMassBalance.mo',
In component roo:
  The parameter roo.air.C_outflow_fixed does not have a binding expression
/opt/oct/lib/RuntimeLibrary/libModelicaExternalC.a(ModelicaInternal.c.o): In function `ModelicaInternal_temporaryFileName':
/opt/OCT/P538-OCT/JModelica/ThirdParty/MSL/Modelica/Resources/C-Sources/ModelicaInternal.c:622: warning: the use of `tmpnam' is dangerous, better use `mkstemp'
In file included from sources/Buildings_ThermalZones_Detailed_Examples_FFD_Tutorial_MixedConvection_funcs.c:5:0:
/home/mwetter/proj/ldrd/bie/modeling/github/lbl-srg/modelica-buildings/Buildings/Resources/C-Sources/cfdStartCosimulation.c: In function ‘cfdStartCosimulation’:
/home/mwetter/proj/ldrd/bie/modeling/github/lbl-srg/modelica-buildings/Buildings/Resources/C-Sources/cfdStartCosimulation.c:83:23: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
   cosim->para->bouCon = (size_t *) malloc(nSur*sizeof(size_t));
                       ^
sources/Buildings_ThermalZones_Detailed_Examples_FFD_Tutorial_MixedConvection_funcs.c: In function ‘func_Modelica_Utilities_Streams_readLine_def2’:
sources/Buildings_ThermalZones_Detailed_Examples_FFD_Tutorial_MixedConvection_funcs.c:142:14: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
     string_v = ModelicaInternal_readLine(fileName_v, tmp_1, &tmp_2);
              ^
sources/Buildings_ThermalZones_Detailed_Examples_FFD_Tutorial_MixedConvection_funcs.c: In function ‘func_Modelica_Utilities_Strings_substring_def7’:
sources/Buildings_ThermalZones_Detailed_Examples_FFD_Tutorial_MixedConvection_funcs.c:247:14: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
     result_v = ModelicaStrings_substring(string_v, tmp_1, tmp_2);
              ^
/opt/oct/lib/RuntimeLibrary/libModelicaExternalC.a(ModelicaInternal.c.o): In function `ModelicaInternal_temporaryFileName':
/opt/OCT/P538-OCT/JModelica/ThirdParty/MSL/Modelica/Resources/C-Sources/ModelicaInternal.c:622: warning: the use of `tmpnam' is dangerous, better use `mkstemp'
/opt/oct/lib/RuntimeLibrary/libModelicaMatIO.a(ModelicaMatIO.c.o): In function `Mat_VarDelete':
/opt/OCT/P538-OCT/JModelica/ThirdParty/MSL/Modelica/Resources/C-Sources/ModelicaMatIO.c:4876: warning: the use of `mktemp' is dangerous, better use `mkstemp' or `mkdtemp'

Start Fast Fluid Dynamics Simulation with Thread
WARNING:root:No dependency information for the derivatives was found in the model description. Assuming complete dependency.
Final Run Statistics: --- e+02

 Number of steps                                 : 549
 Number of function evaluations                  : 666
 Number of Jacobian evaluations                  : 30
 Number of function eval. due to Jacobian eval.  : 0
 Number of error test failures                   : 0
 Number of nonlinear iterations                  : 549
 Number of nonlinear convergence failures        : 0
 Number of state function evaluations            : 579
 Number of time events                           : 30

Solver options:

 Solver                   : CVode
 Linear multistep method  : BDF
 Nonlinear solver         : Newton
 Linear solver type       : DENSE
 Maximal order            : 5
 Tolerances (absolute)    : 1e-08
 Tolerances (relative)    : 1e-08

Simulation interval    : 0.0 - 180.0 seconds.
Elapsed simulation time: 90.15310996999324 seconds.
malloc_consolidate(): invalid chunk size
/opt/oct/bin/jm_ipython.sh: line 28:  8112 Aborted                 (core dumped) IPOPT_HOME=${JMODELICA_HOME}/ThirdParty/IPOPT SUNDIALS_HOME=${JMODELICA_HOME}/ThirdParty/Sundials PYTHONPATH=:${JMODELICA_HOME}/Python/::$PYTHONPATH LD_LIBRARY_PATH=:${JMODELICA_HOME}/ThirdParty/IPOPT/lib/:${JMODELICA_HOME}/ThirdParty/Sundials/lib:${JMODELICA_HOME}/ThirdParty/CasADi/lib:$LD_LIBRARY_PATH SEPARATE_PROCESS_JVM=${JAVA_HOME} ipython "$@"
modelonrobinandersson commented 1 year ago

@JayHuLBL if it is of any help, this is some output I have encountered while testing Buildings (that might be related):

(1110/1500) Buildings.ThermalZones.Detailed.Examples.FFD.Tutorial.MixedConvection ... FAIL (verify at 2.10%)
*** Error in `/usr/path_to_python_virtual_environment/bin/python': double free or corruption (out): 0x00007f94cc83cd60 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81329)[0x7f95112d7329]
/lib64/libc.so.6(fclose+0x177)[0x7f95112c4247]
/tmp/some_tmp_folder/binaries/linux64/libffd.so(ffd_log+0x186)[0x7f94594945ee]
/tmp/some_tmp_folder/binaries/linux64/libffd.so(assign_thermal_bc+0x753)[0x7f945947a706]
/tmp/some_tmp_folder/binaries/linux64/libffd.so(read_cosim_data+0x17b)[0x7f9459478d21]
/tmp/some_tmp_folder/binaries/linux64/libffd.so(FFD_solver+0x2b4)[0x7f945948fa2a]
/tmp/some_tmp_folder/binaries/linux64/libffd.so(ffd+0x1d3)[0x7f9459483a14]
/tmp/some_tmp_folder/binaries/linux64/libffd.so(ffd_cosimulation+0x32)[0x7f9459483821]
/tmp/some_tmp_folder/binaries/linux64/libffd.so(ffd_thread+0xe5)[0x7f945948412b]
/lib64/libpthread.so.0(+0x7ea5)[0x7f9511d34ea5]
/lib64/libc.so.6(clone+0x6d)[0x7f9511354b0d]
======= Memory map: ========
00400000-00724000 r-xp 00000000 fd:00 114539813                          /usr/bin/python3
00924000-00929000 r--p 00324000 fd:00 114539813                          /usr/bin/python3
00929000-00962000 rw-p 00329000 fd:00 114539813                          /usr/bin/python3
00962000-00984000 rw-p 00000000 00:00 0 
0124d000-04a83000 rw-p 00000000 00:00 0                                  [heap]

<hundreds of lines>
...
(1117/1500) Buildings.ThermalZones.Detailed.Examples.FFD.WindowWithoutShade ... FAIL (simulation: unexpectedly terminated)
JayHuLBL commented 1 year ago

@modelonrobinandersson Thank you very much for sharing the message. It is very helpful. From the message, it seems that the error is caused by the fclose() in the function ffd_log(), in the file Buildings/Resources/src/FastFluidDynamics/utility.c.

However, to test, I made the ffd_log() to do nothing but just return 0, the error is still there. This indicates that there may be invalid pointer elsewhere in the program, as suggested here.

A crash in fclose like this indicates that the FILE data structure involved has been corrupted, probably by an invalid pointer use elsewhere in the program.

Would you please let me know how you do the test so can see the information shared here? It may trace the error to somewhere else.

modelonrobinandersson commented 1 year ago

@JayHuLBL I compiled the FFD model using OCT (Optimica Compiler Toolkit) on CentOS 7.4.1708, using Python 3.9.5 and GCC gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44). No specific compiler options to OCT.