DREAM Solver

Using DREAM ››
Parent Previous Next

DREAM Solver is the main calculation module implementing mathematical methods described in the Technical Manual. It actually is a C++ library wrapped by C/COM interfaces, which enable using this library in a binary form.

Main functions of DREAM Server


Additional information about DREAM Solver functions and interfaces can be found in IDreamSolver and programming documentation.

Using DREAM Server (an example)

The use of DREAM Solver is demonstrated by the C++ code below, which is part of a simple console application (DREAM Client) generated by DREAM Suite when creating a plugin module for a new project. Another example of the DREAM Console Application. Please note that DREAM COM server must be properly registered, which can be done during the installation process (see Figure 2.3.2. "Installation Features"). The use of DREAM COM server also requires a valid license and authorization.


//-------------------------------------------------------------------------------------------------

 int _tmain(int argc, _TCHAR* argv[])

//-------------------------------------------------------------------------------------------------

{

 // Return value

 HRESULT hRes = S_OK;


 // Initialize COM subsystem

 hRes = CoInitialize(NULL);

 if(SUCCEEDED(hRes))

 {

   // Display info

   wprintf(L"##################################################\n");

   wprintf(L" DREAM Suite 1.0 - Plugin Test Application\n");

   wprintf(L"##################################################\n");

   wprintf(L"This application is intended for testing of DREAM plugin modules.\n");

   int iRunTest = GetUserChoice(L"Do you want to run the test? (Yes/No = 1/0) : ", 0, 1);

   if(iRunTest == 0)

     return 0;


   // We will assume that the current directory is "Project\Plugin\Bin"

   _bstr_t bstrPluginBin = boost::filesystem::current_path().c_str();


   // Path to the project directory

   _bstr_t bstrExampleDir = bstrPluginBin;

   bstrExampleDir += L"\\..\\..";


   // Path to Evaluator DLL

   _bstr_t bstrPluginDllName = L"$DREAM(ProjectName)";

   _bstr_t bstrEvaluatorPath = bstrPluginBin;

   bstrEvaluatorPath += L"\\";

   bstrEvaluatorPath += bstrPluginDllName;

   bstrEvaluatorPath += L".dll";


   // Path to directory with Example data

   _bstr_t bstrModelData = bstrExampleDir;

   bstrModelData += L"\\Model\\Data";


   // Path to directory with Example executables

   _bstr_t bstrModelBin = bstrExampleDir;

   bstrModelBin += L"\\Model\\Bin";


   // Path to working directory

   _bstr_t bstrWorkingDir = bstrExampleDir;

   bstrWorkingDir += L"\\Temp\\Simulations\\Case1";


   // Delete old working directory (to clear old files)

   DrxDeleteDirectory(std::wstring(bstrWorkingDir));

   // Create new working directory

   DrxCreateDirectory(std::wstring(bstrWorkingDir));


   // Calc observer for communication, logging...

   CCalcObserver calcObserver(bstrWorkingDir);


   /////////////////////////////////////////////////////////////////////////////////////////////////

   // Run DREAM Solver

   /////////////////////////////////////////////////////////////////////////////////////////////////

   bool bDisplayInfoAboutCalculationResults = false;

   bool bLogInfoAvailable = false;

   try

   {

     wprintf(L"Connecting DREAM Server...\n");


     // Try to create DreamSolver in COM way

     IDreamSolverPtr pSolver(__uuidof(IDreamSolver));

     // Check if the creation of solver succeeded

     if(pSolver != nullptr)

     {

       // Setup calculation observer

       IDreamObserverPtr pIDreamCalcObserver = nullptr;

       hRes = calcObserver.QueryInterface(IID_IDreamObserver, (void**)&pIDreamCalcObserver);

       if(SUCCEEDED(hRes))

       {

         // Establish communication with solver

         pSolver->AttachObserver(pIDreamCalcObserver);

         bLogInfoAvailable = true;

       }


       // Set directories

       pSolver->SetPath(ePathProjectDir, bstrExampleDir);

       pSolver->SetPath(ePathModelData, bstrModelData);

       pSolver->SetPath(ePathModelBin, bstrModelBin);

       pSolver->SetPath(ePathPluginBin, bstrPluginBin);

       pSolver->SetPath(ePathWorkingDir, bstrWorkingDir);


       // Load plugin module

       hRes = pSolver->LoadPlugin(bstrEvaluatorPath);

       if(SUCCEEDED(hRes))

       {

         // Plugin info

         CDreamPluginInfo pluginInfo;

         pluginInfo.m_PluginType = ePluginType::ePlugin_DLL;

         pluginInfo.m_strPluginName = bstrPluginDllName.copy();

         pluginInfo.m_strModelDataDir = bstrModelData.copy();


         // DREAM calculation parameters

         CDreamCalcParams calcParamInfo;

         // Convergence diagnostics?

         calcParamInfo.m_SaveWithinChainDiagnostics = GetUserChoice(L"Do you want convergence diagnostics?  (Yes/No = 1/0) : ", 0, 1);

         // Export Markov chains?

         calcParamInfo.m_SaveMarkovChains = GetUserChoice(L"Do you want to export Markov chains?  (Yes/No = 1/0) : ", 0, 1);

         // Number of cores

         calcParamInfo.m_NumberOfCPUs = GetUserChoice(L"Number of cores <1,20>, N > 1 = parallel calculation : ", 1, 20);

         calcParamInfo.m_RunParallel = calcParamInfo.m_NumberOfCPUs > 1 ? 1 : 0;


         // Initialize the evaluator and all input data by values obtained from the evaluator

         hRes = pSolver->InitEvaluatorDef(&pluginInfo, &calcParamInfo);

         if(SUCCEEDED(hRes))

         {

           // Get pointer to evaluator

           IDreamEvaluatorPtr pEvaluator = nullptr;

           hRes = pSolver->GetEvaluator(&pEvaluator);

           if(SUCCEEDED(hRes))

           {

             // Set signal handler (Ctrl+C)

             pTheOnlySolverInThisProcess = pSolver;

             SetConsoleCtrlHandler((PHANDLER_ROUTINE)SignalHandler, TRUE);


             // Prepare the console to display calculation progress (%)  

             wprintf(L"Starting calculation...\n");

             wprintf(L"(press Ctrl+C to interrupt the calculation)\n");


             // Perform calculations

             hRes = pSolver->StartCalculation();

             if(SUCCEEDED(hRes))

             {

               wprintf(L"Calculation succeeded.\n");

             }

             else

             {

               // Check errors

               _bstr_t message;

               CDreamErrorInfo lastErrorInfo;

               if(pSolver->GetLastError(&lastErrorInfo) != S_OK && lastErrorInfo.GetErrType() == eTypeError)

                 message = L"Calculation failed with error:\n" + _bstr_t(lastErrorInfo.GetErrDescription()) + L"\n";

               else

                 message = L"Calculation failed.\n";


               wprintf(message);

             }

             bDisplayInfoAboutCalculationResults = true;

           }


           // Failed to get pointer to evaluator

           else

           {

             wprintf(L"Failed to get pointer to evaluator.\n");

           }

         }


         // Failed to initialize evaluator

         else

         {

           wprintf(L"Failed to initialize evaluator.\n");

         }

       }


       // Failed to load plugin module

       else

       {

         wprintf(L"Failed to load plugin module.\n");

       }

     }


     // Failed to create Dream solver

     else

     {

       wprintf(L"Failed to create Dream solver.\n");

     }

   }

   catch(_com_error& e)

   {

     // Dream solver creation failed

     std::wstring message = L"COM error: ";

     message += e.ErrorMessage();

     message += L"\n";

     PrintMessageUTF8(message);

   }


   // Clean-up

   if(pTheOnlySolverInThisProcess != nullptr)

   {

     // Release solver interface - it must be done here, because it fails after CoUnitialize()

     pTheOnlySolverInThisProcess = nullptr;

   }


   // Output information

   if(bDisplayInfoAboutCalculationResults)

   {

     // Output files

     wprintf(L"Output and log files are available in directory:\n" + bstrWorkingDir + L"\n");

   }


   // Error info

   if(bLogInfoAvailable)

   {

     // Number of errors and warnings returned by the solver

     int nErrors = calcObserver.GetNoOfErrors();

     int nWarnings = calcObserver.GetNoOfWarnings();

     wprintf(L"Number of errors: %d, Number of warnings: %d\n", nErrors, nWarnings);


     // Print logged messages (errors, warnings, messages and calculation time)?

     int iPrintLogInfo = GetUserChoice(L"Do you want to display log-file content? (Yes/No = 1/0) : ", 0, 1);

     if(iPrintLogInfo == 1)

     {

       wprintf(L"Log file contents:\n");

       calcObserver.PrintMessages();

     }

   }


   // Clean-up COM services

   CoUninitialize();

 }


 // Wait for ENTER

 std::wcin.ignore(1024, '\n');

 std::cout << "\nPress Enter to continue...";

 std::wcin.get();


 // Return result

 return hRes;

}