Skip to content

Commit

Permalink
Users can now set the max nr. of negative jacobians reported using th…
Browse files Browse the repository at this point in the history
…e output_negative_jacobians flag.
  • Loading branch information
SteveMaas1978 committed Aug 21, 2024
1 parent 668ea2d commit 39b9216
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
9 changes: 3 additions & 6 deletions FEBio/FEBioApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ bool FEBioApp::ParseCmdLine(int nargs, char* argv[])
else if ((strcmp(sz, "-g") == 0) || (strcmp(sz, "-g1") == 0))
{
ops.ndebug = 1;
NegativeJacobian::m_boutput = true;
}
else if (strcmp(sz, "-g2") == 0)
{
Expand Down Expand Up @@ -436,15 +435,13 @@ bool FEBioApp::ParseCmdLine(int nargs, char* argv[])
}
else if (strncmp(sz, "-output_negative_jacobians", 26) == 0)
{
bool output = true;
int n = -1;
if (sz[26] == '=')
{
const char* szval = sz + 27;
if (strcmp(szval, "0") == 0) output = false;
else if (strcmp(szval, "1") == 0) output = true;
else { fprintf(stderr, "Invalid value for output_negative_jacobian option\n"); return false; }
n = atoi(szval);
}
NegativeJacobian::m_boutput = output;
NegativeJacobian::m_maxout = n;
}
else if (sz[0] == '-')
{
Expand Down
4 changes: 2 additions & 2 deletions FEBio/FEBioCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ int FEBioCmd_set::run(int nargs, char** argv)
FEBioModel* fem = GetFEM();
if (nargs == 1)
{
printf("output_negative_jacobians = %d\n", (NegativeJacobian::m_boutput ? 1 : 0));
printf("output_negative_jacobians = %d\n", NegativeJacobian::m_maxout);
if (fem)
{
printf("print_model_params = %d\n", (fem->GetPrintParametersFlag() ? 1 : 0));
Expand All @@ -760,7 +760,7 @@ int FEBioCmd_set::run(int nargs, char** argv)

if (strcmp(argv[1], "output_negative_jacobians") == 0)
{
NegativeJacobian::m_boutput = (n != 0);
NegativeJacobian::m_maxout = n;
printf("output_negative_jacobians = %d", n);
}
else if (fem && strcmp(argv[1], "print_model_params") == 0)
Expand Down
2 changes: 1 addition & 1 deletion FEBioLib/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ namespace febio {
{
int n;
tag.value(n);
NegativeJacobian::m_boutput = (n != 0);
NegativeJacobian::m_maxout = n;
return true;
}

Expand Down
17 changes: 15 additions & 2 deletions FECore/FEException.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,9 @@ ZeroDiagonal::ZeroDiagonal(vector<int>& l, FEM& fem)
}
*/
//=============================================================================
bool NegativeJacobian::m_boutput = false;
bool NegativeJacobian::m_bthrown = false;
int NegativeJacobian::m_maxout = 0; // output off by default
int NegativeJacobian::m_count = 0;

//-----------------------------------------------------------------------------
NegativeJacobian::NegativeJacobian(int iel, int ng, double vol, FEElement* pe)
Expand All @@ -162,7 +163,9 @@ NegativeJacobian::NegativeJacobian(int iel, int ng, double vol, FEElement* pe)
//-----------------------------------------------------------------------------
bool NegativeJacobian::DoOutput()
{
return m_boutput;
m_count++; // we do this here because this function is called from critical sections.
bool b = (m_maxout < 0) || (m_count <= m_maxout);
return b;
}

//-----------------------------------------------------------------------------
Expand All @@ -177,6 +180,16 @@ bool NegativeJacobian::IsThrown()
return m_bthrown;
}

int NegativeJacobian::Count()
{
return m_count;
}

void NegativeJacobian::ResetCount()
{
m_count = 0;
}

//-----------------------------------------------------------------------------
NANInResidualDetected::NANInResidualDetected(const FENodalDofInfo& ndi)
{
Expand Down
15 changes: 13 additions & 2 deletions FECore/FEException.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ class FECORE_API NegativeJacobian : public FEException
static void clearFlag();
static bool IsThrown();

static int Count();
static void ResetCount();

public:
static bool m_boutput; //!< set to false to suppress output of negative jacobians
static bool m_bthrown;
static int m_maxout; // max nr of negative jacobians reported (< 0 for unlimited, 0 = off, > 0 sets limit)
static int m_count;
};

class FECORE_API ZeroDiagonal : public FEException
Expand Down Expand Up @@ -151,7 +155,14 @@ public: FactorizationError() : FEException("Fatal error in factorization of stif
};

class FECORE_API NegativeJacobianDetected : public FEException {
public: NegativeJacobianDetected() : FEException("Negative jacobian was detected.") {}
public: NegativeJacobianDetected() {
int n = NegativeJacobian::Count();
if (n > 1)
what("%d negative jacobians detected.", n);
else
what("Negative jacobian detected.");
NegativeJacobian::ResetCount();
}
};

class FECORE_API ConcentrationChangeDetected : public FEException {
Expand Down

0 comments on commit 39b9216

Please sign in to comment.