Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

multiproblem_example1.cpp

The following code snippet is an example of a customized driver function that solves two problems respectively defined in the files spring.xml and room_fc.xml. These two simple problems are provided with the SPARK distribution and can be found in the examples subdirectory of the SPARK installation directory.

The code snippet shows the variable declarations and the sequence of operations required to solve the two SPARK problems within the same simulation session.

To build the simulator, you first have to build the problems room_fc.pr and spring.pr in their respective directories. Then you can use the multiproblem makefile Multi-problem command file to generate the customized simulator.

/////////////////////////////////////////////////////////////////////////////////////////
// File nultiproblem_example1.cpp  
//
// Definition of a SPARK driver function for multiple problems instantiated from the
// descriptions contained in the files :
// - spring.cpp
// - room_fc
// that you can find in the spark/examples/ subdirectory.
// 
// This driver function is not used by the VisualSPARK distribution. Its purpose is to
// show how a multi-problem driver could be implemented. 
//
// Make sure that all *.prf, *.run and *.xml files used in this driver are provided in the 
// working directory.
//
// All problems are loaded at runtime. Prepare the dynamic libraries with the atomic classes 
// required by each probem.
//
/////////////////////////////////////////////////////////////////////////////////////////
//
// Author  Dimitri Curtil (LBNL/SRG)
// Date    March 30, 2003
//
/////////////////////////////////////////////////////////////////////////////////////////
// PORTIONS COPYRIGHT (C) 2003 AYRES SOWELL ASSOCIATES, INC.
// PORTIONS COPYRIGHT (C) 2003 THE REGENTS OF THE UNIVERSITY OF CALIFORNIA .
//   PENDING APPROVAL BY THE US DEPARTMENT OF ENERGY. ALL RIGHTS RESERVED.
//
/////////////////////////////////////////////////////////////////////////////////////////


// The debugger can't handle symbols more than 255 characters long.
// STL often creates symbols longer than that.
// When symbols are longer than 255 characters, the warning is disabled.
#pragma warning(disable:4786)

  
#ifdef MacVersion
#       include <console>
#endif

#include <iostream>
using std::cout;
using std::ofstream;
using std::endl;

#include <sstream>
using std::ostringstream;
using std::ends;

#include <string> 
using std::string; 


/////////////////////////////////////////////////////////////////////////////////////////
//
// Required header files
//
#include "sparkapi.h"    // for various API functions needed to manage the SPARK problem
#include "problem.h"     // for TProblem definition
#include "ctrls.h"       // for TRuntimeControls definition
#include "prefs.h"       // for TPreferenceSettings definition
#include "exitcode.h"    // for SPARK::TExitCode and SPARK::ExitWithError()
#include "exceptions.h"  // for SPARK exception classes
/////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////
// Function name        : RunProblem
// Description      : Loads and solves problem for the specified instance name,
//                    and the *.run, *.prf and *.xml files.
// Return type          : void 
// Argument         : const string& instanceName
// Argument         : const string& runFileName
// Argument         : const string& prfFileName
// Argument         : const string& xmlFileName
void RunProblem(
    const string& instanceName, 
    const string& runFileName, 
    const string& prfFileName, 
    const string& xmlFileName )
{
    // We assume that the needed names are valid!
    // Construct TProblem object with:
    // - unique problem name
    // - name of the xml file with the problem description
    // Check whether problem has been successfully loaded or not
    if ( !SPARK::Problem::DynamicBuild::Load( instanceName.c_str(), xmlFileName.c_str() ) ) { 
        ostringstream ErrMsg;

        ErrMsg << "Could not load the problem \"" << instanceName.c_str() << "\" " << ends;

        SPARK::ExitWithError(
            SPARK::ExitCode_ERROR_INVALID_PROBLEM,
            "Generic Problem Driver",
            ErrMsg.str()
        );
    }

    // Prepare run-time controls and preference settings
    ostringstream ControlsName;
    ControlsName << "Default SPARK driver for problem (" << instanceName.c_str() << ")" << ends;

    SPARK::TRuntimeControls RuntimeControls( 
        ControlsName.str().c_str(),   // controls name
        runFileName.c_str()           // *.run file name
    );

    SPARK::TPreferenceSettings PreferenceSettings( prfFileName.c_str() );

    // Retrieve pointer to SPARK::TProblem instance
    SPARK::TProblem* P = SPARK::Problem::Get( instanceName.c_str() );

    // Setup problem for simulation
    SPARK::Problem::Initialize( P, RuntimeControls );
    SPARK::Problem::LoadPreferenceSettings( P, PreferenceSettings );

    // Perform simulation
    SPARK::TProblem::SimulationFlags SimulationFlag = SPARK::Problem::Simulate( 
        P,
        SPARK::Problem::TRestartFlag(),
        SPARK::Problem::TStopTime(),
        SPARK::Problem::TTimeStep()
    );

    if ( SimulationFlag != SPARK::TProblem::SimulationFlag_OK ) {
        ostringstream ErrMsg;
        ErrMsg << "Cannot recover. Abort simulation." << ends;

        SPARK::ExitWithError( 
            SPARK::ExitCode_ERROR_NUMERICAL,
            "Generic Problem Driver",
            ErrMsg.str(),
            P
        );
    }

    // Cleanup problem object and write statistics out.
    SPARK::Problem::Terminate( P );

}
/////////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////////
//
// Entry point for the SPARK simulation program
//
int main(int argc, char *argv[ ])
{
    try {

        ///////////////////////////////////////////////////////////////////////////
        // Start simulation environment and open global log files
        SPARK::Start( 
            argv[0],        // session name
            "run.log",      // run log file name
            "error.log",    // error log filename
            "debug.log"     // debug log filename (only used with debug libsolver)
        );


        ///////////////////////////////////////////////////////////////////////////
        // Load and solve problem 1 "spring"
        // We assume that the needed files are located in the current working directory
        RunProblem(
            "MySpring1",
            "spring.run",
            "spring.prf",
            "spring.xml"
        );


        ///////////////////////////////////////////////////////////////////////////
        // Re-load and re-solve problem 1 "spring"
        // We assume that the needed files are located in the current working directory
        RunProblem(
            "MySpring2",
            "spring.run",
            "spring.prf",
            "spring.xml"
        );


        ///////////////////////////////////////////////////////////////////////////
        // Load and solve problem 2 "room_fc"
        // We assume that the needed files are located in the current working directory
        RunProblem(
            "MyRoom_fc",
            "room_fc.run",
            "room_fc.prf",
            "room_fc.xml"
        );


        ///////////////////////////////////////////////////////////////////////////
        // Close global log files and perform garbage collection
        SPARK::End();

    } // try {}


    /////////////////////////////////////////////////////////////////////////////////////
    // Catch unprocessed exceptions
    catch (const SPARK::XAssertion& x1) { // SPARK specific exceptions
        SPARK::ExitWithError(
            x1.code(),
            __FILE__,
            x1.what()
        );
    }
    catch (const std::exception& x2) { // std exceptions
        SPARK::ExitWithError(
            SPARK::ExitCode_ERROR_RUNTIME_ERROR,
            __FILE__,
            x2.what()
        );
    }

    return SPARK::ExitCode_OK;
}
/////////////////////////////////////////////////////////////////////////////////////////


        




Generated on 5 Nov 2003 for VisualSPARK 2.01