Skip to content
Snippets Groups Projects
Commit eb0a6e1e authored by Paul Dechamps's avatar Paul Dechamps :speech_balloon:
Browse files

(revert) Back to finite-diff for jacobian computation

For now the tape recording does not work for adjoint and jacobian at the same time
parent a9efa2ab
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,7 @@ blVectorXd Fluxes::computeResiduals(size_t inod, std::shared_ptr<BoundaryLayer> ...@@ -26,7 +26,7 @@ blVectorXd Fluxes::computeResiduals(size_t inod, std::shared_ptr<BoundaryLayer>
} }
/** /**
* @brief Compute Jacobian of the integral boundary layer equations using * @brief Compute Jacobian of the integral boundary layer equations using
* automatic differentiation or finite differences. * automatic differentiation or finite differences.
* *
* @param inod Marker id. * @param inod Marker id.
...@@ -36,7 +36,7 @@ blVectorXd Fluxes::computeResiduals(size_t inod, std::shared_ptr<BoundaryLayer> ...@@ -36,7 +36,7 @@ blVectorXd Fluxes::computeResiduals(size_t inod, std::shared_ptr<BoundaryLayer>
* @return blMatrixXd * @return blMatrixXd
*/ */
blMatrixXd Fluxes::computeJacobian(size_t inod, std::shared_ptr<BoundaryLayer> &bl, blMatrixXd Fluxes::computeJacobian(size_t inod, std::shared_ptr<BoundaryLayer> &bl,
blVectorXd const &sysRes, bldouble eps) blVectorXd const &sysRes, bldouble eps)
{ {
size_t nVar = bl->getnVar(); size_t nVar = bl->getnVar();
blMatrixXd JacMatrix = blMatrixXd::Zero(nVar, nVar); blMatrixXd JacMatrix = blMatrixXd::Zero(nVar, nVar);
...@@ -44,44 +44,53 @@ blMatrixXd Fluxes::computeJacobian(size_t inod, std::shared_ptr<BoundaryLayer> & ...@@ -44,44 +44,53 @@ blMatrixXd Fluxes::computeJacobian(size_t inod, std::shared_ptr<BoundaryLayer> &
for (size_t k = 0; k < nVar; ++k) for (size_t k = 0; k < nVar; ++k)
sol[k] = bl->u[inod * nVar + k]; sol[k] = bl->u[inod * nVar + k];
#ifdef USE_CODI // #ifdef USE_CODI
// Compute the Jacobian using automatic differentiation. // // Compute the Jacobian using automatic differentiation.
bltape &tape = bldouble::getTape(); // bltape &tape = bldouble::getTape();
tape.reset();
tape.setActive();
// Register inputs
for (auto &ui : sol)
tape.registerInput(ui);
// Compute residuals
blVectorXd res = blLaws(inod, bl, sol);
// Register outputs
for (auto& ri : res)
tape.registerOutput(ri);
// Desactivate tape
tape.setPassive();
// Form the Jacobian
for (size_t i = 0; i < 5; ++i)
{
res[i].gradient() = 1.0;
tape.evaluate(); // // The tape could be active for adjoint calculation
for(size_t j = 0; j < 5; ++j) { // bool wasActive = true;
JacMatrix(i, j) = sol[j].getGradient(); // if (!tape.isActive())
} // {
tape.clearAdjoints(); // tape.reset();
} // tape.setActive();
#else // wasActive = false;
// Compute the Jacobian using finite differences. // }
bldouble varSave = 0.; // // Register inputs
for (size_t iVar = 0; iVar < nVar; ++iVar) // for (auto &ui : sol)
{ // tape.registerInput(ui);
varSave = sol[iVar]; // // Compute residuals
sol[iVar] += eps; // blVectorXd res = blLaws(inod, bl, sol);
JacMatrix.col(iVar) = (blLaws(inod, bl, sol) - sysRes) / eps; // // Register outputs
sol[iVar] = varSave; // for (auto &ri : res)
} // tape.registerOutput(ri);
#endif // // Desactivate tape
// tape.setPassive();
// // Form the Jacobian
// for (size_t i = 0; i < 5; ++i)
// {
// res[i].gradient() = 1.0;
// tape.evaluate();
// for (size_t j = 0; j < 5; ++j)
// JacMatrix(i, j) = sol[j].getGradient();
// tape.clearAdjoints();
// }
// if (wasActive)
// tape.setActive();
// // tape.printStatistics();
// #else
// Compute the Jacobian using finite differences.
bldouble varSave = 0.;
for (size_t iVar = 0; iVar < nVar; ++iVar)
{
varSave = sol[iVar];
sol[iVar] += eps;
JacMatrix.col(iVar) = (blLaws(inod, bl, sol) - sysRes) / eps;
sol[iVar] = varSave;
}
// #endif
return JacMatrix; return JacMatrix;
} }
...@@ -94,7 +103,7 @@ blMatrixXd Fluxes::computeJacobian(size_t inod, std::shared_ptr<BoundaryLayer> & ...@@ -94,7 +103,7 @@ blMatrixXd Fluxes::computeJacobian(size_t inod, std::shared_ptr<BoundaryLayer> &
* @return blVectorXd * @return blVectorXd
*/ */
blVectorXd Fluxes::blLaws(size_t inod, std::shared_ptr<BoundaryLayer> &bl, blVectorXd Fluxes::blLaws(size_t inod, std::shared_ptr<BoundaryLayer> &bl,
std::vector<bldouble> &u) std::vector<bldouble> &u)
{ {
size_t nVar = bl->getnVar(); size_t nVar = bl->getnVar();
auto &currNode = bl->nodes[inod]; auto &currNode = bl->nodes[inod];
...@@ -363,7 +372,7 @@ bldouble Fluxes::amplificationRoutine(bldouble Hk, bldouble Ret, bldouble theta) ...@@ -363,7 +372,7 @@ bldouble Fluxes::amplificationRoutine(bldouble Hk, bldouble Ret, bldouble theta)
// Normal envelope amplification rate // Normal envelope amplification rate
bldouble m = -0.05 + 2.7 * Hmi - 5.5 * Hmi * Hmi + 3. * Hmi * Hmi * Hmi + bldouble m = -0.05 + 2.7 * Hmi - 5.5 * Hmi * Hmi + 3. * Hmi * Hmi * Hmi +
0.1 * std::exp(-20. * Hmi); 0.1 * std::exp(-20. * Hmi);
bldouble arg = 3.87 * Hmi - 2.52; bldouble arg = 3.87 * Hmi - 2.52;
bldouble dndRet = 0.028 * (Hk - 1.) - 0.0345 * std::exp(-(arg * arg)); bldouble dndRet = 0.028 * (Hk - 1.) - 0.0345 * std::exp(-(arg * arg));
ax = (m * dndRet / theta) * Rfac; ax = (m * dndRet / theta) * Rfac;
......
...@@ -23,11 +23,11 @@ public: ...@@ -23,11 +23,11 @@ public:
~Fluxes() {} ~Fluxes() {}
blVectorXd computeResiduals(size_t inod, std::shared_ptr<BoundaryLayer> &bl); blVectorXd computeResiduals(size_t inod, std::shared_ptr<BoundaryLayer> &bl);
blMatrixXd computeJacobian(size_t inod, std::shared_ptr<BoundaryLayer> &bl, blMatrixXd computeJacobian(size_t inod, std::shared_ptr<BoundaryLayer> &bl,
blVectorXd const &sysRes, bldouble eps); blVectorXd const &sysRes, bldouble eps);
private: private:
blVectorXd blLaws(size_t inod, std::shared_ptr<BoundaryLayer> &bl, blVectorXd blLaws(size_t inod, std::shared_ptr<BoundaryLayer> &bl,
std::vector<bldouble> &u); std::vector<bldouble> &u);
bldouble amplificationRoutine(bldouble Hk, bldouble Ret, bldouble theta) const; bldouble amplificationRoutine(bldouble Hk, bldouble Ret, bldouble theta) const;
}; };
} // namespace blast } // namespace blast
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment