Skip to content

Commit

Permalink
[GeoMechanicsApplication] Fixed a few issues found by Coverity (#12713)
Browse files Browse the repository at this point in the history
Changes include:
- Fixed a memory leak in a C++ unit test (which, however, is not being run at the moment).
- Removed some structurally and logically dead code.
- "Unnamed" a few unused function parameters.
- Initialized some pointer members to `nullptr`.
- Renamed a few data members to comply with the Kratos Style Guide.
  • Loading branch information
avdg81 authored Oct 1, 2024
1 parent eac5203 commit 1289be4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,14 @@ void SmallStrainUDSM3DLaw::SetAttributes(const Properties& rMaterialProperties)
std::vector<double> StateVariables;

const auto& MaterialParameters = rMaterialProperties[UMAT_PARAMETERS];
pUserMod(&IDTask, &modelNumber, &isUndr, &iStep, &iteration, &iElement, &integrationNumber,
&Xorigin, &Yorigin, &Zorigin, &time, &deltaTime, &(MaterialParameters.data()[0]),
&(mStressVectorFinalized.data()[0]), &excessPorePressurePrevious,
StateVariablesFinalized.data(), &(mDeltaStrainVector.data()[0]), (double**)mMatrixD,
&bulkWater, &(mStressVector.data()[0]), &excessPorePressureCurrent,
StateVariables.data(), &iPlastic, &nStateVariables, &mAttributes[IS_NON_SYMMETRIC],
&mAttributes[IS_STRESS_DEPENDENT], &mAttributes[IS_TIME_DEPENDENT],
&mAttributes[USE_TANGENT_MATRIX], mProjectDirectory.data(), &nSizeProjectDirectory, &iAbort);
mpUserMod(&IDTask, &modelNumber, &isUndr, &iStep, &iteration, &iElement, &integrationNumber,
&Xorigin, &Yorigin, &Zorigin, &time, &deltaTime, &(MaterialParameters.data()[0]),
&(mStressVectorFinalized.data()[0]), &excessPorePressurePrevious,
StateVariablesFinalized.data(), &(mDeltaStrainVector.data()[0]), (double**)mMatrixD,
&bulkWater, &(mStressVector.data()[0]), &excessPorePressureCurrent,
StateVariables.data(), &iPlastic, &nStateVariables, &mAttributes[IS_NON_SYMMETRIC],
&mAttributes[IS_STRESS_DEPENDENT], &mAttributes[IS_TIME_DEPENDENT],
&mAttributes[USE_TANGENT_MATRIX], mProjectDirectory.data(), &nSizeProjectDirectory, &iAbort);

KRATOS_ERROR_IF_NOT(iAbort == 0)
<< "The specified UDSM returns an error while call UDSM with IDTASK"
Expand Down Expand Up @@ -387,14 +387,14 @@ int SmallStrainUDSM3DLaw::GetNumberOfStateVariablesFromUDSM(const Properties& rM
std::vector<double> StateVariables;

const auto& MaterialParameters = rMaterialProperties[UMAT_PARAMETERS];
pUserMod(&IDTask, &modelNumber, &isUndr, &iStep, &iteration, &iElement, &integrationNumber,
&Xorigin, &Yorigin, &Zorigin, &time, &deltaTime, &(MaterialParameters.data()[0]),
&(mStressVectorFinalized.data()[0]), &excessPorePressurePrevious,
StateVariablesFinalized.data(), &(mDeltaStrainVector.data()[0]), (double**)mMatrixD,
&bulkWater, &(mStressVector.data()[0]), &excessPorePressureCurrent,
StateVariables.data(), &iPlastic, &nStateVariables, &mAttributes[IS_NON_SYMMETRIC],
&mAttributes[IS_STRESS_DEPENDENT], &mAttributes[IS_TIME_DEPENDENT],
&mAttributes[USE_TANGENT_MATRIX], mProjectDirectory.data(), &nSizeProjectDirectory, &iAbort);
mpUserMod(&IDTask, &modelNumber, &isUndr, &iStep, &iteration, &iElement, &integrationNumber,
&Xorigin, &Yorigin, &Zorigin, &time, &deltaTime, &(MaterialParameters.data()[0]),
&(mStressVectorFinalized.data()[0]), &excessPorePressurePrevious,
StateVariablesFinalized.data(), &(mDeltaStrainVector.data()[0]), (double**)mMatrixD,
&bulkWater, &(mStressVector.data()[0]), &excessPorePressureCurrent,
StateVariables.data(), &iPlastic, &nStateVariables, &mAttributes[IS_NON_SYMMETRIC],
&mAttributes[IS_STRESS_DEPENDENT], &mAttributes[IS_TIME_DEPENDENT],
&mAttributes[USE_TANGENT_MATRIX], mProjectDirectory.data(), &nSizeProjectDirectory, &iAbort);

KRATOS_ERROR_IF_NOT(iAbort == 0)
<< "The specified UDSM returns an error while call UDSM with IDTASK"
Expand All @@ -413,7 +413,7 @@ SizeType SmallStrainUDSM3DLaw::GetNumberOfMaterialParametersFromUDSM(const Prope

int nUDSM = rMaterialProperties[UDSM_NUMBER];
int nParameters(0);
pGetParamCount(&nUDSM, &nParameters);
mpGetParamCount(&nUDSM, &nParameters);

return static_cast<SizeType>(nParameters);

Expand Down Expand Up @@ -460,10 +460,10 @@ bool SmallStrainUDSM3DLaw::loadUDSMLinux(const Properties& rMaterialProperties)
}

// resolve function GetParamCount address
pGetParamCount = (f_GetParamCount)dlsym(lib_handle, "getparamcount");
if (!pGetParamCount) {
pGetParamCount = (f_GetParamCount)dlsym(lib_handle, "getparamcount_");
if (!pGetParamCount) {
mpGetParamCount = (f_GetParamCount)dlsym(lib_handle, "getparamcount");
if (!mpGetParamCount) {
mpGetParamCount = (f_GetParamCount)dlsym(lib_handle, "getparamcount_");
if (!mpGetParamCount) {
KRATOS_INFO("Error in loadUDSMLinux")
<< "cannot load function GetParamCount in the specified UDSM: "
<< rMaterialProperties[UDSM_NAME] << std::endl;
Expand All @@ -473,12 +473,12 @@ bool SmallStrainUDSM3DLaw::loadUDSMLinux(const Properties& rMaterialProperties)
}

// resolve function GetStateVarCount address
pGetStateVarCount = (f_GetStateVarCount)dlsym(lib_handle, "getstatevarcount");
mpGetStateVarCount = (f_GetStateVarCount)dlsym(lib_handle, "getstatevarcount");

pUserMod = (f_UserMod)dlsym(lib_handle, "user_mod");
if (!pUserMod) {
pUserMod = (f_UserMod)dlsym(lib_handle, "user_mod_");
if (!pUserMod) {
mpUserMod = (f_UserMod)dlsym(lib_handle, "user_mod");
if (!mpUserMod) {
mpUserMod = (f_UserMod)dlsym(lib_handle, "user_mod_");
if (!mpUserMod) {
KRATOS_INFO("Error in loadUDSMLinux")
<< "cannot load function User_Mod in the specified UDSM: " << rMaterialProperties[UDSM_NAME]
<< std::endl;
Expand Down Expand Up @@ -521,11 +521,11 @@ bool SmallStrainUDSM3DLaw::loadUDSMWindows(const Properties& rMaterialProperties
}

// resolve function GetParamCount address
pGetParamCount = (f_GetParamCount)GetProcAddress(hGetProcIDDLL, "getparamcount");
if (!pGetParamCount) {
mpGetParamCount = (f_GetParamCount)GetProcAddress(hGetProcIDDLL, "getparamcount");
if (!mpGetParamCount) {
// check if the dll is compiled with gfortran
pGetParamCount = (f_GetParamCount)GetProcAddress(hGetProcIDDLL, "getparamcount_");
if (!pGetParamCount) {
mpGetParamCount = (f_GetParamCount)GetProcAddress(hGetProcIDDLL, "getparamcount_");
if (!mpGetParamCount) {
KRATOS_INFO("Error in loadUDSMWindows")
<< "cannot load function GetParamCount in the specified UDSM: "
<< rMaterialProperties[UDSM_NAME] << std::endl;
Expand All @@ -535,13 +535,13 @@ bool SmallStrainUDSM3DLaw::loadUDSMWindows(const Properties& rMaterialProperties
}

// resolve function GetStateVarCount address
pGetStateVarCount = (f_GetStateVarCount)GetProcAddress(hGetProcIDDLL, "getstatevarcount");
mpGetStateVarCount = (f_GetStateVarCount)GetProcAddress(hGetProcIDDLL, "getstatevarcount");

pUserMod = (f_UserMod)GetProcAddress(hGetProcIDDLL, "user_mod");
if (!pUserMod) {
mpUserMod = (f_UserMod)GetProcAddress(hGetProcIDDLL, "user_mod");
if (!mpUserMod) {
// check if the dll is compiled with gfortran
pUserMod = (f_UserMod)GetProcAddress(hGetProcIDDLL, "user_mod_");
if (!pUserMod) {
mpUserMod = (f_UserMod)GetProcAddress(hGetProcIDDLL, "user_mod_");
if (!mpUserMod) {
KRATOS_INFO("Error in loadUDSMWindows")
<< "cannot load function User_Mod in the specified UDSM: " << rMaterialProperties[UDSM_NAME]
<< std::endl;
Expand Down Expand Up @@ -716,14 +716,14 @@ void SmallStrainUDSM3DLaw::CallUDSM(int* pIDTask, ConstitutiveLaw::Parameters& r
auto nSizeProjectDirectory = static_cast<int>(mProjectDirectory.size());

const auto& MaterialParameters = rMaterialProperties[UMAT_PARAMETERS];
pUserMod(pIDTask, &modelNumber, &isUndr, &iStep, &iteration, &iElement, &integrationNumber,
&Xorigin, &Yorigin, &Zorigin, &time, &deltaTime, &(MaterialParameters.data()[0]),
&(mStressVectorFinalized.data()[0]), &excessPorePressurePrevious,
&(mStateVariablesFinalized.data()[0]), &(mDeltaStrainVector.data()[0]),
(double**)mMatrixD, &bulkWater, &(mStressVector.data()[0]), &excessPorePressureCurrent,
&(mStateVariables.data()[0]), &iPlastic, &nStateVariables, &mAttributes[IS_NON_SYMMETRIC],
&mAttributes[IS_STRESS_DEPENDENT], &mAttributes[IS_TIME_DEPENDENT],
&mAttributes[USE_TANGENT_MATRIX], mProjectDirectory.data(), &nSizeProjectDirectory, &iAbort);
mpUserMod(pIDTask, &modelNumber, &isUndr, &iStep, &iteration, &iElement, &integrationNumber,
&Xorigin, &Yorigin, &Zorigin, &time, &deltaTime, &(MaterialParameters.data()[0]),
&(mStressVectorFinalized.data()[0]), &excessPorePressurePrevious,
&(mStateVariablesFinalized.data()[0]), &(mDeltaStrainVector.data()[0]),
(double**)mMatrixD, &bulkWater, &(mStressVector.data()[0]), &excessPorePressureCurrent,
&(mStateVariables.data()[0]), &iPlastic, &nStateVariables, &mAttributes[IS_NON_SYMMETRIC],
&mAttributes[IS_STRESS_DEPENDENT], &mAttributes[IS_TIME_DEPENDENT],
&mAttributes[USE_TANGENT_MATRIX], mProjectDirectory.data(), &nSizeProjectDirectory, &iAbort);

if (iAbort != 0) {
KRATOS_INFO("CallUDSM, iAbort !=0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,9 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) SmallStrainUDSM3DLaw : public Consti
///@}
///@name Member Variables
///@{
pF_GetParamCount pGetParamCount;
pF_GetStateVarCount pGetStateVarCount;
pF_UserMod pUserMod;
pF_GetParamCount mpGetParamCount = nullptr;
pF_GetStateVarCount mpGetStateVarCount = nullptr;
pF_UserMod mpUserMod = nullptr;

bool mIsModelInitialized = false;
bool mIsUDSMLoaded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,12 @@ bool SmallStrainUMAT3DLaw::loadUMATLinux(const Properties& rMaterialProperties)
return false;
}
if (rMaterialProperties[IS_FORTRAN_UDSM]) {
pUserMod = (f_UMATMod)dlsym(lib_handle, "umat_");
mpUserMod = (f_UMATMod)dlsym(lib_handle, "umat_");
} else {
pUserMod = (f_UMATMod)dlsym(lib_handle, "umat");
mpUserMod = (f_UMATMod)dlsym(lib_handle, "umat");
}

if (!pUserMod) {
if (!mpUserMod) {
KRATOS_ERROR << "cannot load function User_Mod in the specified UMAT "
<< rMaterialProperties[UDSM_NAME] << std::endl;
return false;
Expand Down Expand Up @@ -333,8 +333,8 @@ bool SmallStrainUMAT3DLaw::loadUMATWindows(const Properties& rMaterialProperties
KRATOS_ERROR << "cannot load the specified UMAT " << rMaterialProperties[UDSM_NAME] << std::endl;
}

pUserMod = (f_UMATMod)GetProcAddress(hGetProcIDDLL, "umat");
if (!pUserMod) {
mpUserMod = (f_UMATMod)GetProcAddress(hGetProcIDDLL, "umat");
if (!mpUserMod) {
KRATOS_INFO("Error in loadUMATWindows")
<< "cannot load function umat in the specified UMAT: " << rMaterialProperties[UDSM_NAME]
<< std::endl;
Expand Down Expand Up @@ -514,12 +514,12 @@ void SmallStrainUMAT3DLaw::CallUMAT(ConstitutiveLaw::Parameters& rValues)
// variable to check if an error happened in the model:
const auto& MaterialParameters = rValues.GetMaterialProperties()[UMAT_PARAMETERS];
auto nProperties = static_cast<int>(MaterialParameters.size());
pUserMod(&(mStressVector.data()[0]), &(mStateVariables.data()[0]), (double**)mMatrixD, &SSE,
&SPD, &SCD, nullptr, nullptr, nullptr, nullptr, &(mStrainVectorFinalized.data()[0]),
&(mDeltaStrainVector.data()[0]), &time, &deltaTime, nullptr, nullptr, nullptr, nullptr,
&materialName, &ndi, &nshr, &ntens, &nStateVariables, &(MaterialParameters.data()[0]),
&nProperties, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, &iElement,
&integrationNumber, nullptr, nullptr, &iStep, &iteration);
mpUserMod(&(mStressVector.data()[0]), &(mStateVariables.data()[0]), (double**)mMatrixD, &SSE,
&SPD, &SCD, nullptr, nullptr, nullptr, nullptr, &(mStrainVectorFinalized.data()[0]),
&(mDeltaStrainVector.data()[0]), &time, &deltaTime, nullptr, nullptr, nullptr,
nullptr, &materialName, &ndi, &nshr, &ntens, &nStateVariables,
&(MaterialParameters.data()[0]), &nProperties, nullptr, nullptr, nullptr, nullptr,
nullptr, nullptr, &iElement, &integrationNumber, nullptr, nullptr, &iStep, &iteration);

KRATOS_CATCH("")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) SmallStrainUMAT3DLaw : public Consti
///@}
///@name Member Variables
///@{
pF_UMATMod pUserMod;
pF_UMATMod mpUserMod = nullptr;

bool mIsModelInitialized = false;
bool mIsUMATLoaded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,24 @@ namespace Kratos
{

template <unsigned int TDim, unsigned int TNumNodes>
Element::Pointer GeoStructuralBaseElement<TDim, TNumNodes>::Create(IndexType NewId,
NodesArrayType const& ThisNodes,
PropertiesType::Pointer pProperties) const
Element::Pointer GeoStructuralBaseElement<TDim, TNumNodes>::Create(IndexType,
NodesArrayType const&,
PropertiesType::Pointer) const
{
KRATOS_ERROR << "calling the default Create method for a particular "
"element ... illegal operation!!"
<< std::endl;

return Element::Pointer(new GeoStructuralBaseElement(NewId, this->GetGeometry().Create(ThisNodes), pProperties));
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
template <unsigned int TDim, unsigned int TNumNodes>
Element::Pointer GeoStructuralBaseElement<TDim, TNumNodes>::Create(IndexType NewId,
GeometryType::Pointer pGeom,
PropertiesType::Pointer pProperties) const
Element::Pointer GeoStructuralBaseElement<TDim, TNumNodes>::Create(IndexType,
GeometryType::Pointer,
PropertiesType::Pointer) const
{
KRATOS_ERROR << "calling the default Create method for a particular "
"element ... illegal operation!!"
<< std::endl;

return Element::Pointer(new GeoStructuralBaseElement(NewId, pGeom, pProperties));
}

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) GeoStructuralBaseElement : public El

///----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Element::Pointer Create(IndexType NewId,
NodesArrayType const& ThisNodes,
PropertiesType::Pointer pProperties) const override;
Element::Pointer Create(IndexType, NodesArrayType const&, PropertiesType::Pointer) const override;

Element::Pointer Create(IndexType NewId, GeometryType::Pointer pGeom, PropertiesType::Pointer pProperties) const override;
Element::Pointer Create(IndexType, GeometryType::Pointer, PropertiesType::Pointer) const override;

int Check(const ProcessInfo& rCurrentProcessInfo) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,23 @@ def RunSolutionLoop(self):

KratosMultiphysics.Logger.PrintInfo(self._GetSimulationName(), "--------------------------------------", " ")

is_piping = True
if is_piping:
# todo set this as solver input
max_piping_iterations = 500

# todo set this as solver input
max_piping_iterations = 500
grow_pipe = True
while grow_pipe:
self.initialise_grow_step()
piping_converged = False
piping_iter = 0

grow_pipe = True
while grow_pipe:
while not piping_converged and piping_iter < max_piping_iterations:
self.run_flow_calculation()

self.initialise_grow_step()
piping_converged = False
piping_iter = 0
# update all pipe heights and check for piping convergence and check if
piping_converged = self.update_pipe_heights()
piping_iter += 1

while not piping_converged and piping_iter < max_piping_iterations:
self.run_flow_calculation()

# update all pipe heights and check for piping convergence and check if
piping_converged = self.update_pipe_heights()
piping_iter += 1

grow_pipe = self.finalise_grow_step()

else:
self.run_flow_calculation()
grow_pipe = self.finalise_grow_step()

self.OutputSolutionStep()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ namespace Kratos::Testing
source_directory, temporary_working_directory,
std::filesystem::copy_options::overwrite_existing | std::filesystem::copy_options::recursive);

auto settlement = CustomWorkflowFactory::CreateKratosGeoSettlement();
auto p_settlement =
std::unique_ptr<KratosGeoSettlement>{CustomWorkflowFactory::CreateKratosGeoSettlement()};
for (int i = 0; i < 4; ++i) {
const auto project_file = "ProjectParameters_stage" + std::to_string(i + 1) + ".json";
const int status = settlement->RunStage(temporary_working_directory, project_file,
&flow_stubs::emptyLog, &flow_stubs::emptyProgress,
&flow_stubs::emptyLog, &flow_stubs::emptyCancel);
const int status = p_settlement->RunStage(temporary_working_directory, project_file,
&flow_stubs::emptyLog, &flow_stubs::emptyProgress,
&flow_stubs::emptyLog, &flow_stubs::emptyCancel);

const std::string original_file = "test_model_stage" + std::to_string(i + 1) + ".post.orig.res";
const std::string result_file = "test_model_stage" + std::to_string(i + 1) + ".post.res";
Expand Down

0 comments on commit 1289be4

Please sign in to comment.