private static List<MyNode> OrderNetworkNodes(MyNetwork network)
 {
     IMyOrderingAlgorithm topoOps = new MyHierarchicalOrdering();
     return topoOps.EvaluateOrder(network);
 }
        /// <summary>
        /// Creates the execution plan.
        /// </summary>
        /// <param name="project">The whole project from which the standard execution plan will be built.</param>
        /// <param name="initNodes">Ordered list of new nodes from which the initialization plan will be built.</param>
        /// <returns>The created execution plan.</returns>
        public MyExecutionPlan CreateExecutionPlan(MyProject project, IEnumerable<MyWorkingNode> initNodes = null)
        {
            MyExecutionPlan executionPlan = new MyExecutionPlan();

            IMyOrderingAlgorithm ordering = new MyHierarchicalOrdering();
            ordering.EvaluateOrder(project.Network);

            var initBlocks = new List<IMyExecutable>();
            if (initNodes != null)
                initBlocks.AddRange(initNodes.Select(node => CreateNodeExecutionPlan(node, true)));

            executionPlan.InitStepPlan = new MyExecutionBlock(initBlocks.ToArray());
            executionPlan.InitStepPlan.Name = "Initialization";

            executionPlan.StandardStepPlan = new MyExecutionBlock(
                CreateNodeExecutionPlan(project.World, false),
                CreateNodeExecutionPlan(project.Network, false));
            executionPlan.StandardStepPlan.Name = "Simulation";

            return executionPlan;
        }
        public MyExecutionPlan CreateExecutionPlan(MyProject project)
        {
            MyExecutionPlan executionPlan = new MyExecutionPlan();

            IMyOrderingAlgorithm ordering = new MyHierarchicalOrdering();
            ordering.EvaluateOrder(project.Network);

            executionPlan.InitStepPlan = new MyExecutionBlock(
                CreateNodeExecutionPlan(project.World, true),
                CreateNodeExecutionPlan(project.Network, true));
            executionPlan.InitStepPlan.Name = "Initialization";

            executionPlan.StandardStepPlan = new MyExecutionBlock(
                CreateNodeExecutionPlan(project.World, false),
                CreateNodeExecutionPlan(project.Network, false));
            executionPlan.StandardStepPlan.Name = "Simulation";

            return executionPlan;
        }
        // TODO: throw an exception if the model doesn't converge. The return value is unintuitive.
        /// <summary>
        /// Update the whole memory model - all blocks will get their memory block sizes updated correctly.
        /// Since this might not converge, only a set number of iterations is done.
        /// </summary>
        /// <returns>true if the model did not converge (error), false if it did.</returns>
        public bool UpdateMemoryModel()
        {
            MyLog.INFO.WriteLine("Updating memory blocks...");

            IMyOrderingAlgorithm topoOps = new MyHierarchicalOrdering();
            List<MyNode> orderedNodes = topoOps.EvaluateOrder(Project.Network);

            if (!orderedNodes.Any())
            {
                return true;
            }

            int attempts = 0;
            bool anyOutputChanged = false;

            try
            {

                while (attempts < MAX_BLOCKS_UPDATE_ATTEMPTS)
                {
                    attempts++;
                    anyOutputChanged = false;

                    anyOutputChanged |= UpdateAndCheckChange(Project.World);
                    orderedNodes.ForEach(node => anyOutputChanged |= UpdateAndCheckChange(node));

                    if (!anyOutputChanged)
                    {
                        MyLog.INFO.WriteLine("Successful update after " + attempts + " cycle(s).");
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                MyLog.ERROR.WriteLine("Exception occured while updating memory model: " + e.Message);
                return true;
            }

            return anyOutputChanged;
        }