39 int numStates = solutionHistory->getNumStates();
41 TEUCHOS_TEST_FOR_EXCEPTION(
42 numStates < 1, std::logic_error,
43 "Error - setInitialConditions() needs at least one SolutionState\n"
44 " to set the initial condition. Number of States = "
47 RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
48 RCP<Thyra::VectorBase<Scalar> > x = initialState->getX();
49 RCP<Thyra::VectorBase<Scalar> > xDot = initialState->getXDot();
50 if (xDot() == Teuchos::null) xDot = this->getStepperXDot();
52 auto inArgs = this->wrapperModel_->getNominalValues();
54 RCP<Thyra::VectorBase<Scalar> > initialXDot = initialState->getXDot();
57 TEUCHOS_TEST_FOR_EXCEPTION(
58 !((x != Teuchos::null && initialXDot != Teuchos::null) ||
59 (inArgs.get_x() != Teuchos::null &&
60 inArgs.get_x_dot() != Teuchos::null)),
62 "Error - We need to set the initial conditions for x and xDot from\n"
63 " either initialState or appModel_->getNominalValues::InArgs\n"
64 " (but not from a mixture of the two).\n");
69 if (x == Teuchos::null) {
70 TEUCHOS_TEST_FOR_EXCEPTION(
71 (x == Teuchos::null) && (inArgs.get_x() == Teuchos::null),
73 "Error - setInitialConditions needs the ICs from the "
75 " or getNominalValues()!\n");
77 x = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x());
78 initialState->setX(x);
83 using Teuchos::rcp_const_cast;
84 if (x == Teuchos::null || xDot == Teuchos::null) {
85 TEUCHOS_TEST_FOR_EXCEPTION(
86 (inArgs.get_x() == Teuchos::null) ||
87 (inArgs.get_x_dot() == Teuchos::null),
89 "Error - setInitialConditions() needs the ICs from the initialState\n"
90 " or getNominalValues()!\n");
91 x = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x());
92 initialState->setX(x);
93 RCP<Thyra::VectorBase<Scalar> > x_dot =
94 rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x_dot());
95 initialState->setXDot(x_dot);
100 std::string icConsistency = this->getICConsistency();
101 if (icConsistency ==
"None") {
103 if (initialState->getXDot() == Teuchos::null)
104 Thyra::assign(xDot.ptr(), Scalar(0.0));
107 if (initialState->getXDotDot() == Teuchos::null)
108 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
111 else if (icConsistency ==
"Zero") {
113 Thyra::assign(xDot.ptr(), Scalar(0.0));
115 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), Scalar(0.0));
117 else if (icConsistency ==
"App") {
119 auto x_dot = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> >(
121 TEUCHOS_TEST_FOR_EXCEPTION(
122 x_dot == Teuchos::null, std::logic_error,
123 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
124 " but 'App' returned a null pointer for xDot!\n");
125 Thyra::assign(xDot.ptr(), *x_dot);
128 auto xDotDot = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> >(
129 inArgs.get_x_dot_dot());
130 TEUCHOS_TEST_FOR_EXCEPTION(
131 xDotDot == Teuchos::null, std::logic_error,
132 "Error - setInitialConditions() requested 'App' for IC consistency,\n"
133 " but 'App' returned a null pointer for xDotDot!\n");
134 Thyra::assign(this->getStepperXDotDot(initialState).ptr(), *xDotDot);
137 else if (icConsistency ==
"Consistent") {
140 const Scalar time = initialState->getTime();
141 const Scalar dt = initialState->getTimeStep();
142 RCP<TimeDerivative<Scalar> > timeDer = Teuchos::null;
143 const Scalar alpha = Scalar(1.0);
144 const Scalar beta = Scalar(0.0);
148 const Thyra::SolveStatus<Scalar> sStatus =
149 this->solveImplicitODE(x, xDot, time, p);
151 TEUCHOS_TEST_FOR_EXCEPTION(
152 sStatus.solveStatus != Thyra::SOLVE_STATUS_CONVERGED,
154 "Error - Solver failed while determining the initial conditions.\n"
156 << Thyra::toString(sStatus.solveStatus) <<
".\n");
159 TEUCHOS_TEST_FOR_EXCEPTION(
160 true, std::logic_error,
161 "Error - setInitialConditions(): 'Consistent' for second-order ODE\n"
162 " has not been implemented.\n");
166 TEUCHOS_TEST_FOR_EXCEPTION(
167 true, std::logic_error,
168 "Error - setInitialConditions() invalid IC consistency, "
169 << icConsistency <<
".\n");
174 initialState->setIsSynced(
true);
177 if (this->getICConsistencyCheck()) {
178 auto f = initialState->getX()->clone_v();
180 const Scalar time = initialState->getTime();
181 const Scalar dt = initialState->getTimeStep();
182 RCP<TimeDerivative<Scalar> > timeDer = Teuchos::null;
183 const Scalar alpha = Scalar(0.0);
184 const Scalar beta = Scalar(0.0);
188 this->evaluateImplicitODE(f, x, xDot, time, p);
190 Scalar normX = Thyra::norm(*x);
191 Scalar reldiff = Scalar(0.0);
192 if (normX == Scalar(0.0))
193 reldiff = Thyra::norm(*f);
195 reldiff = Thyra::norm(*f) / normX;
197 Scalar eps = Scalar(100.0) * std::abs(Teuchos::ScalarTraits<Scalar>::eps());
198 RCP<Teuchos::FancyOStream> out = this->getOStream();
199 Teuchos::OSTab ostab(out, 1,
"StepperImplicit::setInitialConditions()");
201 *out <<
"\n---------------------------------------------------\n"
202 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
203 <<
" Initial condition PASSED consistency check!\n"
204 <<
" (||f(x,xDot,t)||/||x|| = " << reldiff <<
") < "
205 <<
"(eps = " << eps <<
")" << std::endl
206 <<
"---------------------------------------------------\n"
210 *out <<
"\n---------------------------------------------------\n"
211 <<
"Info -- Stepper = " << this->getStepperType() <<
"\n"
212 <<
" Initial condition FAILED consistency check but continuing!\n"
213 <<
" (||f(x,xDot,t)||/||x|| = " << reldiff <<
") > "
214 <<
"(eps = " << eps <<
")" << std::endl
215 <<
" ||f(x,xDot,t)|| = " << Thyra::norm(*f) << std::endl
216 <<
" ||x|| = " << Thyra::norm(*x) << std::endl
217 <<
"---------------------------------------------------\n"
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...