Skip to content

Commit 0f36b96

Browse files
committed
Implement modified Newton
1 parent 519303e commit 0f36b96

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/libcadet/SimulatorImpl.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -225,19 +225,32 @@ namespace cadet
225225
cadet::AdJacobianParams{sim->_vecADres, sim->_vecADy, sim->numSensitivityAdDirections()});
226226
}
227227

228-
/*
229-
int jacobianUpdateWrapper(N_Vector y, N_Vector yDot, N_Vector res, N_Vector tempv1, N_Vector tempv2, N_Vector tempv3, void* userData)
228+
/**
229+
* @brief IDAS jacobian wrapper function to call the model's jacobian() method
230+
*/
231+
232+
int jacobianUpdateWrapper(SUNLinearSolver LS, SUNMatrix A)
230233
{
231-
cadet::Simulator* const sim = static_cast<cadet::Simulator*>(userData);
232-
const double t = IDA_mem->ida_tn;
234+
cadet::Simulator* const sim = static_cast<cadet::Simulator*>(LS->content);
235+
233236
const unsigned int secIdx = sim->getCurrentSection(t);
237+
double t;
238+
double alpha;
239+
N_Vector y;
240+
N_Vector yDot;
241+
N_Vector unused1;
242+
N_Vector unused2;
243+
N_Vector unused3;
244+
void* unused4;
245+
IDAGetErrWeights(sim->_idaMemBlock, sim->_linearSolverWeight);
246+
IDAGetNonlinearSystemData(sim->_idaMemBlock, &t, &unused1, &unused2, &y, &yDot, &unused3, &alpha, &unused4);
234247

235248
LOG(Trace) << "==> Jacobian at t = " << t;
236249

237250
return sim->_model->jacobian(cadet::SimulationTime{ t, secIdx }, cadet::ConstSimulationState{ NVEC_DATA(y), NVEC_DATA(yDot) }, NVEC_DATA(tempv1),
238251
cadet::AdJacobianParams{ sim->_vecADres, sim->_vecADy, sim->numSensitivityAdDirections() });
239252
}
240-
*/
253+
241254

242255
/**
243256
* @brief Change the error weights in the state vector
@@ -367,21 +380,6 @@ namespace cadet
367380
return 0;
368381
}
369382

370-
int linearSolverSetup(SUNLinearSolver, SUNMatrix)
371-
{
372-
return 0;
373-
}
374-
375-
int linearSolverSetATimes(SUNLinearSolver, void*, SUNATimesFn)
376-
{
377-
return 0;
378-
}
379-
380-
int linearSolverNumIters(SUNLinearSolver)
381-
{
382-
return 1;
383-
}
384-
385383
N_Vector linearSolverResidual(SUNLinearSolver)
386384
{
387385
return nullptr;
@@ -427,7 +425,7 @@ namespace cadet
427425
Simulator::Simulator() : _model(nullptr), _solRecorder(nullptr), _idaMemBlock(nullptr), _vecStateY(nullptr),
428426
_vecStateYdot(nullptr), _vecFwdYs(nullptr), _vecFwdYsDot(nullptr),
429427
_relTolS(1.0e-9), _absTol(1, 1.0e-12), _relTol(1.0e-9), _initStepSize(1, 1.0e-6), _maxSteps(10000), _maxStepSize(0.0),
430-
_nThreads(0), _sensErrorTestEnabled(true), _maxNewtonIter(4), _maxErrorTestFail(10), _maxConvTestFail(10),
428+
_nThreads(0), _modifiedNewton(false), _sensErrorTestEnabled(true), _maxNewtonIter(4), _maxErrorTestFail(10), _maxConvTestFail(10),
431429
_maxNewtonIterSens(4), _curSec(0), _skipConsistencyStateY(false), _skipConsistencySensitivity(false),
432430
_consistentInitMode(ConsistentInitialization::Full), _consistentInitModeSens(ConsistentInitialization::Full),
433431
_vecADres(nullptr), _vecADy(nullptr), _lastIntTime(0.0), _notification(nullptr), _linearSolver(nullptr), _sunctx(nullptr)
@@ -533,6 +531,8 @@ namespace cadet
533531
_linearSolver->content = this;
534532
_linearSolver->ops->gettype = linearSolverGetType;
535533
_linearSolver->ops->solve = linearSolverSolve;
534+
if (_modifiedNewton)
535+
_linearSolver->ops->setup = jacobianUpdateWrapper;
536536

537537
// Attach user data structure
538538
IDASetUserData(_idaMemBlock, this);

src/libcadet/SimulatorImpl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int residualDaeWrapper(double t, N_Vector y, N_Vector yDot, N_Vector res, void*
3636
int linearSolverSolve(SUNLinearSolver ls, SUNMatrix, N_Vector x, N_Vector b, double tol);
3737
int linearSolverSetScalingVectors(SUNLinearSolver ls, N_Vector weight, N_Vector);
3838

39-
int jacobianUpdateWrapper(void* IDA_mem, N_Vector y, N_Vector yDot, N_Vector res, N_Vector tempv1, N_Vector tempv2, N_Vector tempv3);
39+
int jacobianUpdateWrapper(SUNLinearSolver LS, SUNMatrix A);
4040

4141
int residualSensWrapper(int ns, double t, N_Vector y, N_Vector yDot, N_Vector res,
4242
N_Vector* yS, N_Vector* ySDot, N_Vector* resS,
@@ -219,7 +219,7 @@ class Simulator : public ISimulator
219219

220220
friend int ::cadet::linearSolverSolve(SUNLinearSolver ls, SUNMatrix, N_Vector x, N_Vector b, double tol);
221221
friend int ::cadet::linearSolverSetScalingVectors(SUNLinearSolver ls, N_Vector weight, N_Vector);
222-
friend int ::cadet::jacobianUpdateWrapper(void* IDA_mem, N_Vector y, N_Vector yDot, N_Vector res, N_Vector tempv1, N_Vector tempv2, N_Vector tempv3);
222+
friend int ::cadet::jacobianUpdateWrapper(SUNLinearSolver ls, SUNMatrix A);
223223

224224
// friend int ::cadet::weightWrapper(N_Vector y, N_Vector ewt, void *user_data);
225225

0 commit comments

Comments
 (0)