From afaf51b835b25d139e5c437db12f8b77f0f66ea0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Sj=C3=B6lund?= Date: Thu, 12 Nov 2026 10:50:04 +0100 Subject: [PATCH] Compare hidden variables, skip missing constants Since OpenModelica #17888, diffSimulationResults fails on a variable that is in the reference but not in the result. Many references have such variables that OpenModelica does not write: - Parameters with HideResult=true, for example m_flow_fixed of the ThermofluidStream sinks. -emit_protected does not write them. - Constants, for example dropOfCommons.instanceNameColor of ThermofluidStream, the fuel constants of ClaRa and eps of ModelicaTest. OpenModelica never writes constants. Simulate with -ignoreHideResult too, so that the hidden variables are in the result and are compared. Do not compare a variable that is not in the result when its value in the reference does not change: for a .mat reference, a variable that is not time-varying; for a CSV reference, a column with one value. The log lists these variables. A missing variable that changes over time is still a failure. Assisted-by: Claude Opus 5.5 --- testmodel.py | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/testmodel.py b/testmodel.py index f52b938..ca84275 100755 --- a/testmodel.py +++ b/testmodel.py @@ -628,7 +628,7 @@ def createOmcSessionNew(): variableFilter=shared.variableFilterOf(referenceVars) # get the number of intervals from the file numberOfIntervalsInReference = omc_new.sendExpression('readSimulationResultSize("%s")' % referenceFile) - emit_protected="-emit_protected" + emit_protected="-emit_protected -ignoreHideResult" except: referenceFile="" if referenceFile=="": @@ -1269,8 +1269,15 @@ def verifyAgainstReference(resFile, prefix, stat): # diffSimulationResults fails on a variable it cannot find in the result. dymolaOnlyVars=[v for v in diffVars if shared.isDymolaOnlyVariable(v)] diffVars=[v for v in diffVars if not shared.isDymolaOnlyVariable(v)] + resVars=omc_new.sendExpression('readSimulationResultVars("%s", readParameters=true, openmodelicaStyle=true)' % resFile) or [] + # OpenModelica writes no constants; Dymola writes them as parameters. + constantVars=constantsInReference([v for v in diffVars if v not in set(resVars)]) + diffVars=[v for v in diffVars if v not in constantVars] + if constantVars: + with open(errFile, 'a+') as fp: + fp.write("Not compared, constant in the reference and not in the result: %s\n" % ",".join(sorted(constantVars))) stat["diff"] = {"time":monotonic()-start, "vars":[], "numCompared":len(referenceVars)} - if len(diffVars)==0 and (referenceOK or dymolaOnlyVars): + if len(diffVars)==0 and (referenceOK or dymolaOnlyVars or constantVars): stat["phase"]=7 with open(errFile, 'a+') as fp: fp.write("Reference file matches\n") @@ -1279,7 +1286,6 @@ def verifyAgainstReference(resFile, prefix, stat): fp.write(omc_new.sendExpression('OpenModelica.Scripting.getErrorString()', parsed = False)) fp.write("\nVariables in the reference:" ) fp.write(",".join(referenceVars)+"\n") - resVars=omc_new.sendExpression('readSimulationResultVars("%s", readParameters=true, openmodelicaStyle=true)' % resFile) fp.write("\nVariables in the result:" ) fp.write(",".join(resVars)+"\n") stat["diff"]["vars"]=diffVars @@ -1288,6 +1294,21 @@ def verifyAgainstReference(resFile, prefix, stat): filterDiffVars(resFile, prefix + ".mat", diffVars) filterDiffVars(referenceFile, prefix + ".ref.mat", diffVars) +def constantsInReference(names): + """The names whose reference value does not change over time.""" + if not names: + return set() + if referenceFile.endswith(".mat"): + timeVarying = set(omc_new.sendExpression('readSimulationResultVars("%s", readParameters=false, openmodelicaStyle=true)' % referenceFile) or []) + return {v for v in names if v not in timeVarying} + constants = set() + # One call per name: a name that is not a component reference fails alone. + for v in names: + values = omc_new.sendExpression('readSimulationResult("%s", {%s}, %d)' % (referenceFile, v, numberOfIntervalsInReference)) + if values and len(set(values[0])) == 1: + constants.add(v) + return constants + def filterDiffVars(inFile, outFile, diffVars): vars = ", ".join('"%s"' % v.replace('\\', '\\\\').replace('"', '\\"') for v in diffVars) try: