Ejemplo n.º 1
0
//------------------------------------------------------------------------------
bool Target::Execute()
{
#ifdef DEBUG_TARGET_EXEC
    MessageInterface::ShowMessage
    (wxT("Target::Execute() entered, theSolver=<%p>'%s'\n"), (GmatBase*)theSolver,
     theSolver->GetName().c_str());
    MessageInterface::ShowMessage
    (wxT("maxIter=%d\n"),
     theSolver->GetIntegerParameter(theSolver->GetParameterID(wxT("MaximumIterations"))));
    MessageInterface::ShowMessage
    (wxT("currentFunction=<%p>'%s'\n"),
     currentFunction, currentFunction ? ((GmatBase*)currentFunction)->GetName().c_str() : wxT("NULL"));
#endif

    // If targeting inside a function, we need to reinitialize since the local solver is
    // cloned in Initialize(). All objects including solvers are initialized in
    // assignment command which happens after Target::Initialize(). (LOJ: 2009.03.17)
    if (currentFunction != NULL && !targeterInFunctionInitialized)
    {
        Initialize();
        targeterInFunctionInitialized = true;
    }

    bool retval = true;

    // Drive through the state machine.
    Solver::SolverState state = theSolver->GetState();

#ifdef DEBUG_TARGET_COMMANDS
    MessageInterface::ShowMessage(wxT("TargetExecute(%c%c%c%d)\n"),
                                  (commandExecuting?wxT('Y'):wxT('N')),
                                  (commandComplete?wxT('Y'):wxT('N')),
                                  (branchExecuting?wxT('Y'):wxT('N')),
                                  state);
    MessageInterface::ShowMessage(wxT("   targeterConverged=%d\n"),
                                  targeterConverged);
#endif

    // Attempt to reset if recalled
    if (commandComplete)
    {
        commandComplete = false;
        commandExecuting = false;
        specialState = Solver::INITIALIZING;
    }

    if (!commandExecuting)
    {
#ifdef DEBUG_TARGET_COMMANDS
        MessageInterface::ShowMessage(
            wxT("Entered Targeter while command is not executing\n"));
#endif

        FreeLoopData();
        StoreLoopData();


        retval = SolverBranchCommand::Execute();

#ifdef DEBUG_TARGETER
        MessageInterface::ShowMessage(wxT("Resetting the Differential Corrector\n"));
#endif

        theSolver->TakeAction(wxT("Reset"));
        state = theSolver->GetState();
    }

    if (branchExecuting)
    {
        retval = ExecuteBranch();
        if (!branchExecuting)
        {
            if ((state == Solver::FINISHED) || (specialState == Solver::FINISHED))
            {
                PenDownSubscribers();
                LightenSubscribers(1);
                commandComplete = true;
            }
            else
            {
                PenUpSubscribers();
            }
        }
    }
    else
    {
        GmatCommand *currentCmd;

        publisher->SetRunState(Gmat::SOLVING);

        switch (startMode)
        {
        case RUN_INITIAL_GUESS:
#ifdef DEBUG_START_MODE
            MessageInterface::ShowMessage(
                wxT("Running as RUN_INITIAL_GUESS, specialState = %d, currentState = %d\n"),
                specialState, theSolver->GetState());
#endif
            switch (specialState)
            {
            case Solver::INITIALIZING:
                // Finalize initialization of the targeter data
                currentCmd = branch[0];
                targeterConverged = false;
                while (currentCmd != this)
                {
                    wxString type = currentCmd->GetTypeName();
                    if ((type == wxT("Target")) || (type == wxT("Vary")) ||
                            (type == wxT("Achieve")))
                        currentCmd->Execute();
                    currentCmd = currentCmd->GetNext();
                }
                StoreLoopData();
                specialState = Solver::NOMINAL;
                break;

            case Solver::NOMINAL:
                // Execute the nominal sequence
                if (!commandComplete)
                {
                    branchExecuting = true;
                    ResetLoopData();
                }
                specialState = Solver::RUNSPECIAL;
                break;

            case Solver::RUNSPECIAL:
                // Run once more to publish the data from the converged state
                if (!commandComplete)
                {
                    ResetLoopData();
                    branchExecuting = true;
                    publisher->SetRunState(Gmat::SOLVEDPASS);
                }
                theSolver->Finalize();
                specialState = Solver::FINISHED;

                // Final clean-up
                targeterConverged = true;
                break;

            case Solver::FINISHED:
                specialState = Solver::INITIALIZING;
                break;

            default:
                break;
            }
            break;

        case RUN_SOLUTION:
#ifdef DEBUG_START_MODE
            MessageInterface::ShowMessage(
                wxT("Running as RUN_SOLUTION, state = %d\n"), state);
#endif
            throw SolverException(
                wxT("Run Solution is not yet implemented for the Target ")
                wxT("command\n"));
            break;

        case RUN_AND_SOLVE:
        default:
#ifdef DEBUG_START_MODE
            MessageInterface::ShowMessage(
                wxT("Running as RUN_AND_SOLVE or default, state = %d\n"),
                state);
#endif
            switch (state)
            {
            case Solver::INITIALIZING:
                // Finalize initialization of the targeter data
                currentCmd = branch[0];
                targeterConverged = false;
                while (currentCmd != this)
                {
                    wxString type = currentCmd->GetTypeName();
                    if ((type == wxT("Target")) || (type == wxT("Vary")) ||
                            (type == wxT("Achieve")))
                    {
                        currentCmd->Execute();
                        if ((type == wxT("Vary")) && (targeterRunOnce))
                            currentCmd->TakeAction(wxT("SolverReset"));
                    }
                    currentCmd = currentCmd->GetNext();
                }
                StoreLoopData();
                GetActiveSubscribers();
                SetSubscriberBreakpoint();
                break;

            case Solver::NOMINAL:
                // Execute the nominal sequence
                if (!commandComplete)
                {
                    branchExecuting = true;
                    ApplySubscriberBreakpoint();
                    PenDownSubscribers();
                    LightenSubscribers(1);
                    ResetLoopData();
                }
                break;

            case Solver::CHECKINGRUN:
                // Check for convergence; this is done in the targeter state
                // machine, so this case is a NoOp for the Target command
                break;

            case Solver::PERTURBING:
                branchExecuting = true;
                ApplySubscriberBreakpoint();
                PenDownSubscribers();
                LightenSubscribers(4);
                ResetLoopData();
                break;

            case Solver::CALCULATING:
                // Calculate the next set of variables to use; this is
                // performed in the targeter -- nothing to be done here
                break;

            case Solver::FINISHED:
                // Final clean-up
                targeterConverged = true;
                targeterRunOnce = true;

                // Run once more to publish the data from the converged state
                if (!commandComplete)
                {
                    ResetLoopData();
                    branchExecuting = true;
                    ApplySubscriberBreakpoint();
                    PenDownSubscribers();
                    LightenSubscribers(1);
                    publisher->SetRunState(Gmat::SOLVEDPASS);
                }
                break;

            case Solver::ITERATING:     // Intentional fall-through
            default:
                throw CommandException(
                    wxT("Invalid state in the Targeter state machine"));
            }
            break;
        }
    }

    if (!branchExecuting)
    {
        theSolver->AdvanceState();

        if (theSolver->GetState() == Solver::FINISHED)
        {
            publisher->FlushBuffers();
            targeterConverged = true;
        }
    }

    // Pass spacecraft data to the targeter for reporting in debug mode
    if (targeterInDebugMode)
    {
        wxString dbgData = wxT("");
        for (ObjectArray::iterator i = localStore.begin(); i < localStore.end();
                ++i)
        {
            dbgData += (*i)->GetGeneratingString() + wxT("\n---\n");
        }
        theSolver->SetDebugString(dbgData);
    }
    BuildCommandSummary(true);

#ifdef DEBUG_TARGET_EXEC
    MessageInterface::ShowMessage
    (wxT("Target::Execute() returning %d, theSolver=<%p>'%s'\n"), retval,
     theSolver, theSolver->GetName().c_str());
#endif

    return retval;
}
Ejemplo n.º 2
0
//------------------------------------------------------------------------------
bool If::Execute()
{
   bool retval = true;
   
   if (branchExecuting)
   {
      #ifdef DEBUG_IF_EXEC
      MessageInterface::ShowMessage
      (wxT("In If::Execute - Branch Executing -------------\n"));
      #endif
      retval = ExecuteBranch(branchToExecute);
      #ifdef DEBUG_IF_EXEC
         MessageInterface::ShowMessage
         (wxT("In If:: retval returned from ExecuteBranch = %s\n"), (retval? wxT("true") : wxT("false")));
         MessageInterface::ShowMessage
         (wxT("        branchExecuting=%d\n"), branchExecuting);
      #endif
      if (!branchExecuting)
      {
         commandComplete  = true;
         commandExecuting = false;
      }
   }
   else 
   {
      #ifdef DEBUG_IF_EXEC
      MessageInterface::ShowMessage
      (wxT("In If::Execute - Branch NOT Executing -------------\n"));
      #endif
      if (!commandExecuting)
         ConditionalBranch::Execute();
      
      //if (EvaluateCondition(0)) // must deal with multiple conditions later
      if (EvaluateAllConditions()) 
      {
         #ifdef DEBUG_IF_EXEC
         MessageInterface::ShowMessage
         (wxT("In If::Execute all conditions are true - executing first branch\n"));
         #endif
         branchToExecute = 0;
         branchExecuting = true;
         commandComplete = false;
         commandExecuting = true;
      }
      else if ((Integer)branch.size() > 1)  // there could be an 'Else'
      {
         #ifdef DEBUG_IF_EXEC
         MessageInterface::ShowMessage
         (wxT("In If::Execute some conditions are FALSE - executing second branch\n"));
         #endif
         branchExecuting = true;
         branchToExecute = 1; // @todo - add ElseIf (more than two branches)
         commandComplete = false;
         commandExecuting = true;
      }
      else
      {
         #ifdef DEBUG_IF_EXEC
         MessageInterface::ShowMessage
         (wxT("In If::Execute - conditions are FALSE - no other branch to execute\n"));
         //("In If::Execute - ERROR with number of branches - more than two not yet implemented\n");
         #endif
         branchToExecute = 0;
         commandComplete  = true;
         commandExecuting = false;
         branchExecuting = false;  // try this - shouldn't matter
      }
   }
   
   BuildCommandSummary(true);
   #ifdef DEBUG_IF_EXEC
   MessageInterface::ShowMessage
      (wxT("If::BuildCommandSummary completed\n"));
   #endif
   return retval;
} // Execute()