lava / matplotlib-cpp

Extremely simple yet powerful header-only C++ plotting library built on the popular matplotlib
MIT License
4.28k stars 1.12k forks source link

Error calling named_semilogy repeatedly #267

Closed anthomas01 closed 3 years ago

anthomas01 commented 3 years ago

I am trying to animate four subplots based on real time data that is getting piped to a log file over docker. Sometimes it works fine, others it will crash on either grid(), legend() if grid() is disabled, or subplot() if both are disabled. The subplot it terminates on seems to be random. If grid() and legend() are not called it crashes after many iterations, if they are called it seems to be after the first few. Any way of handling the errors or preventing them in the first place would be greatly appreciated.

   plt::figure_size(1280,960); // figure size
   int first = 0;
   do  
   {
      if (getUpdatedLog())
      {
        plt::clf(); //clear

        // update plot
        plt::suptitle("Optimization Data");

        // Flow Residuals
        plt::subplot(2,2,1);
          plt::title("Flow Simulation " + (objFuncIteration.empty() ? "" : std::to_string(objFuncIteration.back())));
          plt::xlabel("Flow Iteration - Time");
          if (!(residualIteration.empty())) 
          {
            if (!(residualUx.empty())) {  plt::named_semilogy("Ux", residualIteration, residualUx,"-");  }
            if (!(residualUy.empty())) {  plt::named_semilogy("Uy", residualIteration, residualUy,"-");  }
            if (!(residualUz.empty())) {  plt::named_semilogy("Uz", residualIteration, residualUz,"-");  }
            if (!(residualP.empty())) {  plt::named_semilogy("P", residualIteration, residualP,"-");  }
            if (!(residualNuTilda.empty())) {  plt::named_semilogy("NuTilda", residualIteration, residualNuTilda,"-");  }
            if (!(residualOmega.empty())) {  plt::named_semilogy("Omega", residualIteration, residualOmega, "-");  }
            if (!(residualK.empty())) {  plt::named_semilogy("K", residualIteration, residualK, "-");  }
            if (!(residualEpsilon.empty())) {  plt::named_semilogy("Epsilon", residualIteration, residualEpsilon, "-");  }
            //if (!(residualIteration.empty()) && !(residualP.empty())) { plt::grid(true); }
            //plt::legend();
          }

        // Adjoint Residuals
        plt::subplot(2,2,2);
          plt::title("Adjoint Residual - Objective Simulation " + (sensIteration.empty() ? "" : std::to_string(sensIteration.back()+1)));
          plt::xlabel("Main Iteration #");
          if (!(mainIteration.empty()) && !(residualAdj.empty())) {  plt::named_semilogy("Adjoint Residual 1", mainIteration ,residualAdj ,"-");  }
          if (!(mainIterationTwo.empty()) && !(residualAdjTwo.empty())) {  plt::named_semilogy("Adjoint Residual 2", mainIterationTwo ,residualAdjTwo ,"-");  }
          //if (!(mainIteration.empty()) && !(residualAdj.empty())) { plt::grid(true); }
          //plt::legend();

        // Iteration Obj Func
        plt::subplot(2,2,3);
          plt::title("Objective Function - Iteration " + (objFuncIteration.empty() ? "" : std::to_string(objFuncIteration.back())));
          plt::xlabel("Flow Iteration - Time");
          if (!(residualIteration.empty()) && !(partialObjFunc.empty())) {  plt::named_plot("CD", residualIteration, partialObjFunc, "-");  }
          //plt::named_plot("CL", residualIteration, partialLiftCoeff, "-");
          //if (!(residualIteration.empty()) && !(partialObjFunc.empty())) { plt::grid(true); }
          //plt::legend();

        // Total Obj Func
        plt::subplot(2,2,4);
          plt::title("Optimization Objective Function");
          plt::xlabel("Flow Simulation #");
          if (!(filledObjFuncIteration.empty()) && !(objFunc.empty())) {  plt::named_plot("CD", filledObjFuncIteration, objFunc, "-");  }
          //plt::named_plot("CL", filledObjFuncIteration, liftCoeff, "-");
          //if (!(filledObjFuncIteration.empty()) && !(objFunc.empty())) { plt::grid(true); }
          //plt::legend();

      }

      plt::pause(0.1);

   } while(*optimizationRunning);

   plt::show();

output-

what(): Call to subplot() failed.

anthomas01 commented 3 years ago

I believe I have narrowed this down to named_semilogy(), this call does not explicitly fail, however every call that fails is immediately after the named_semilogy(). It will animate for hundreds of iterations of the while loop corresponding to 1-5 iterations of the optimization then spontaneously fail and no successive calls to the library work.

anthomas01 commented 3 years ago

Added std runtime_error to named_plot and named_semilogy and it is the semilogy causing the problem. Any ideas??

terminate called after throwing an instance of 'std::runtime_error' what(): Call to named_semilogy() failed.