@@ -411,54 +411,38 @@ namespace GridKit
411411 return 0 ;
412412 }
413413
414+ template <typename scalar_type, typename index_type>
415+ template <Ieeet1SignalInputs input, Ieeet1ExternalVariables variable>
416+ void Ieeet1<scalar_type, index_type>::readSignal(const ScalarT& fallback)
417+ {
418+ constexpr auto index = static_cast <size_t >(variable);
419+ const auto & port = ports_.in .template port <input>();
420+
421+ ws_.getData ()[index] = port.readOrDefault (fallback);
422+ ws_indices_[index] = port ? port.signalVariableIndex ()
423+ : INVALID_INDEX <IdxT>;
424+ }
425+
414426 /* *
415427 * @brief Residual evaluation
416428 *
417429 */
418430 template <typename scalar_type, typename index_type>
419431 int Ieeet1<scalar_type, index_type>::evaluateResidual()
420432 {
421- const auto OMEGA = static_cast <size_t >(Ieeet1ExternalVariables::OMEGA );
422- const auto VREF = static_cast <size_t >(Ieeet1ExternalVariables::VREF );
423- const auto VS = static_cast <size_t >(Ieeet1ExternalVariables::VS );
424- const auto VUEL = static_cast <size_t >(Ieeet1ExternalVariables::VUEL );
425- const auto VOEL = static_cast <size_t >(Ieeet1ExternalVariables::VOEL );
433+ using Input = Ieeet1SignalInputs;
434+ using Variable = Ieeet1ExternalVariables;
426435
427436 auto * ws = ws_.getData ();
428437
429438 // Attached signals are read live; unattached ones keep the latched value.
430- ws[OMEGA ] = omega_set_;
431- ws[VREF ] = vref_set_;
432- ws[VS ] = vs_set_;
433- ws[VUEL ] = vuel_set_;
434- ws[VOEL ] = voel_set_;
435439 std::fill (ws_indices_.begin (), ws_indices_.end (), INVALID_INDEX <IdxT>);
436440
437- if (auto port = ports_.in .template port <Ieeet1SignalInputs::speed>())
438- {
439- ws[OMEGA ] = port.readSignal ();
440- ws_indices_[OMEGA ] = port.signalVariableIndex ();
441- }
442- if (auto port = ports_.in .template port <Ieeet1SignalInputs::vref>())
443- {
444- ws[VREF ] = port.readSignal ();
445- ws_indices_[VREF ] = port.signalVariableIndex ();
446- }
447- if (auto port = ports_.in .template port <Ieeet1SignalInputs::vs>())
448- {
449- ws[VS ] = port.readSignal ();
450- ws_indices_[VS ] = port.signalVariableIndex ();
451- }
452- if (auto port = ports_.in .template port <Ieeet1SignalInputs::vuel>())
453- {
454- ws[VUEL ] = port.readSignal ();
455- ws_indices_[VUEL ] = port.signalVariableIndex ();
456- }
457- if (auto port = ports_.in .template port <Ieeet1SignalInputs::voel>())
458- {
459- ws[VOEL ] = port.readSignal ();
460- ws_indices_[VOEL ] = port.signalVariableIndex ();
461- }
441+ readSignal<Input::speed, Variable::OMEGA >(omega_set_);
442+ readSignal<Input::vref, Variable::VREF >(vref_set_);
443+ readSignal<Input::vs, Variable::VS >(vs_set_);
444+ readSignal<Input::vuel, Variable::VUEL >(vuel_set_);
445+ readSignal<Input::voel, Variable::VOEL >(voel_set_);
462446
463447 // Bus voltages
464448 auto * wb = wb_.getData ();
0 commit comments