diff --git a/src/OpenFOAM/db/typeInfo/typeInfo.H b/src/OpenFOAM/db/typeInfo/typeInfo.H index 83f754f04d..3a93efbf6c 100644 --- a/src/OpenFOAM/db/typeInfo/typeInfo.H +++ b/src/OpenFOAM/db/typeInfo/typeInfo.H @@ -145,6 +145,30 @@ inline To& refCast(From& r) } +//- Reference type cast template function. +// As per dynamicCast, but handles type names for the error messages +// via the virtual type() method. +// Can use index to convey the context. +template +inline To& refCast(From& r, const label index) +{ + try + { + return dynamic_cast(r); + } + catch (const std::bad_cast&) + { + FatalErrorInFunction + << "Attempt to cast type " << r.type() + << " to type " << To::typeName + << " at index " << index + << abort(FatalError); + + return dynamic_cast(r); + } +} + + //- Reference type cast template function. // As per dynamicCast, but handles type names for the error messages // via the virtual type() method. diff --git a/src/TurbulenceModels/turbulenceModels/derivedFvPatchFields/wallFunctions/nutWallFunctions/nutWallFunction/nutWallFunctionFvPatchScalarField.C b/src/TurbulenceModels/turbulenceModels/derivedFvPatchFields/wallFunctions/nutWallFunctions/nutWallFunction/nutWallFunctionFvPatchScalarField.C index edbe662950..eb8d25d48f 100644 --- a/src/TurbulenceModels/turbulenceModels/derivedFvPatchFields/wallFunctions/nutWallFunctions/nutWallFunction/nutWallFunctionFvPatchScalarField.C +++ b/src/TurbulenceModels/turbulenceModels/derivedFvPatchFields/wallFunctions/nutWallFunctions/nutWallFunction/nutWallFunctionFvPatchScalarField.C @@ -188,7 +188,8 @@ Foam::nutWallFunctionFvPatchScalarField::nutw return refCast ( - turbModel.nut()().boundaryField()[patchi] + turbModel.nut()().boundaryField()[patchi], + patchi ); }