85{
86 if(m_max_dt < dt)
87 {
88
89
90 dt = m_max_dt;
92 }
93
94 if (dt != m_dt_last)
95 {
97 }
98
100
102
103
104
105 for(
G4int k = 0; k <= m_current_k_opt+1; ++k)
106 {
107
108
109 m_midpoint.
SetSteps(m_interval_sequence[k]);
110 if(k == 0)
111 {
112 m_midpoint.
DoStep(in, dxdt, out, dt);
113
114 }
115 else
116 {
117 m_midpoint.
DoStep(in, dxdt, m_table[k-1], dt);
118 extrapolate(k, out);
119
120 for (
G4int i = 0; i < fnvar; ++i)
121 {
122 m_err[i] = out[i] - m_table[0][i];
123 }
126 h_opt[k] = calc_h_opt(dt, error, k);
127 work[k] =
static_cast<G4double>(m_cost[k]) / h_opt[k];
128
129 if( (k == m_current_k_opt-1) || m_first)
130 {
131 if(error < 1.0)
132 {
133
134 reject = false;
135 if( (work[k] < KFAC2 * work[k-1]) || (m_current_k_opt <= 2) )
136 {
137
138 m_current_k_opt = std::min(m_k_max - 1 , std::max(2 , k + 1));
139 new_h = h_opt[k];
140 new_h *=
static_cast<G4double>(m_cost[k + 1])
142 }
143 else
144 {
145 m_current_k_opt = std::min(m_k_max - 1, std::max(2, k));
146 new_h = h_opt[k];
147 }
148 break;
149 }
150 if(should_reject(error , k) && !m_first)
151 {
152 reject = true;
153 new_h = h_opt[k];
154 break;
155 }
156 }
157 if(k == m_current_k_opt)
158 {
159 if(error < 1.0)
160 {
161
162 reject = false;
163 if(work[k-1] < KFAC2 * work[k])
164 {
165 m_current_k_opt = std::max( 2 , m_current_k_opt-1 );
166 new_h = h_opt[m_current_k_opt];
167 }
168 else if( (work[k] < KFAC2 * work[k-1]) && !m_last_step_rejected )
169 {
170 m_current_k_opt = std::min(m_k_max - 1, m_current_k_opt + 1);
171 new_h = h_opt[k];
172 new_h *=
static_cast<G4double>(m_cost[m_current_k_opt])
174 }
175 else
176 {
177 new_h = h_opt[m_current_k_opt];
178 }
179 break;
180 }
181 if(should_reject(error, k))
182 {
183 reject = true;
184 new_h = h_opt[m_current_k_opt];
185 break;
186 }
187 }
188 if(k == m_current_k_opt + 1)
189 {
190 if(error < 1.0)
191 {
192 reject = false;
193 if(work[k-2] < KFAC2 * work[k-1])
194 {
195 m_current_k_opt = std::max(2, m_current_k_opt - 1);
196 }
197 if((work[k] < KFAC2 * work[m_current_k_opt]) && !m_last_step_rejected)
198 {
199 m_current_k_opt = std::min(m_k_max - 1 , k);
200 }
201 new_h = h_opt[m_current_k_opt];
202 }
203 else
204 {
205 reject = true;
206 new_h = h_opt[m_current_k_opt];
207 }
208 break;
209 }
210 }
211 }
212
213 if(!reject)
214 {
215 t += dt;
216 }
217
218 if(!m_last_step_rejected || new_h < dt)
219 {
220
221 new_h = std::min(m_max_dt, new_h);
222 m_dt_last = new_h;
223 dt = new_h;
224 }
225
226 m_last_step_rejected = reject;
227 m_first = false;
228
230}
void SetSteps(G4int steps)
void DoStep(const G4double yIn[], const G4double dydxIn[], G4double yOut[], G4double hstep) const
G4double relativeError(const G4double y[], const G4double yerr[], G4double hstep, G4double errorTolerance)