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

sparksolver.cpp

The following code snippet is an example of a driver function for a single problem. It is inspired from the sparksolver.cpp file that comes with the SPARK distribution and that is used by default when building a simulator unless a customized driver function is specified by the user.

The code snippet shows:

The sequence of operations required to load a problem changes depending on whether you are loading a statically-built problem or you are loading a problem at runtime from its XML description. The preprocessor macro SPARK_STATIC_BUILD controls which branch is executed at runtime. For example compiling the driver function with the preprocessor macro SPARK_STATIC_BUILD defined produces a SPARK simulator that will rely on the static build loading scheme.
/////////////////////////////////////////////////////////////////////////////////////////
// sparksolver.cpp  
//
// Implementation of the default SPARK driver function. This is the entry point to the
// simulator program. It supports both statically-built problems and problems loaded at
// runtime through the definition of the preprocessor macro SPARK_STATIC_BUILD when
// compiling this file.
//
/////////////////////////////////////////////////////////////////////////////////////////
//
// Author  Dimitri Curtil (LBNL/SRG)
// Date    May 21, 2002
//
/////////////////////////////////////////////////////////////////////////////////////////
//
// VisualSPARK version 2.0
// 
// Copyright (c) 1999-2003, The Regents of the University of California,
// through the Lawrence Berkeley National Laboratory (subject to receipt of
// any required approvals from the U.S. Department of Energy). All rights
// reserved.
// 
// Portions of VisualSPARK version 2.0  were authored by Ayres Sowell
// Associates and are protected by copyright and international treaties.
// 
// U.S. GOVERNMENT RIGHTS
// Portions of VisualSPARK version 2.0 (the Software) was developed under
// funding from the U.S. Department of Energy and the U.S. Government
// consequently retains certain rights as follows: the U.S. Government has
// been granted for itself and others acting on its behalf a paid-up,
// nonexclusive, irrevocable, worldwide license in the Software to reproduce,
// prepare derivative works, and perform publicly and display publicly.
// Beginning five (5) years after the date permission to assert copyright is
// obtained from the U.S. Department of Energy, and subject to any subsequent
// five (5) year renewals, the U.S. Government is granted for itself and
// others acting on its behalf a paid-up, nonexclusive, irrevocable, worldwide
// license in the Software to reproduce, prepare derivative works, distribute
// copies to the public, perform publicly and display publicly, and to permit
// others to do so.
// 
// THIS SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND.  LAWRENCE
// BERKELEY NATIONAL LABORATORY, ITS LICENSORS, THE UNITED STATES, THE UNITED 
// STATES DEPARTMENT OF ENERGY, AYRES SOWELL ASSOCIATES, AND THEIR EMPLOYEES: 
// (1) DISCLAIM ANY WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, 
// TITLE OR NON-INFRINGEMENT, (2) DO NOT ASSUME ANY LEGAL LIABILITY OR 
// RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR USEFULNESS OF THE SOFTWARE, 
// (3) DO NOT REPRESENT THAT USE OF THE SOFTWARE WOULD NOT INFRINGE PRIVATELY 
// OWNED RIGHTS, (4) DO NOT WARRANT THAT THE SOFTWARE WILL FUNCTION UNINTERRUPTED,
// THAT IT IS ERROR-FREE OR THAT ANY ERRORS WILL BE CORRECTED.
//
/////////////////////////////////////////////////////////////////////////////////////////
// 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 <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 SPARK::TProblem definition
#include "ctrls.h"       // for SPARK::TRuntimeControls definition
#include "prefs.h"       // for SPARK::TPreferenceSettings definition
#include "exitcode.h"    // for SPARK::ExitCodes and SPARK::ExitWithError()
#include "exceptions.h"  // for SPARK exception classes
/////////////////////////////////////////////////////////////////////////////////////////

#include "spark.h"


/////////////////////////////////////////////////////////////////////////////////////////
//
// Entry point for the SPARK simulation program
//
int main(unsigned argc, char** argv)
{
// To support Mac platform
#ifdef MacVersion
    argc = ccommand(&argv);
#endif

    SPARK::TProblem* P = 0;
    char* RunFileName = 0;
    char* PrfFileName = 0;
    char* XmlFileName = 0;

    try {

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


        ///////////////////////////////////////////////////////////////////////////
        // Load the problem under study
        ///////////////////////////////////////////////////////////////////////////
        //
        // Note that if the driver is relying on a statically-built problem, then 
        // there is no need to specify the *.xml file name at the command-line, hence 
        // the two different calls controlled with the macro preprocessor 
        // SPARK_STATIC_BUILD.
        //
        // WARNING: Never explicitly delete the problem object because the garbage 
        //          collection is performed internally upon calling the function 
        //          SPARK::End().
        //
        ///////////////////////////////////////////////////////////////////////////

#if defined(SPARK_STATIC_BUILD)
        ///////////////////////////////////////////////////////////////////////////
        // Process the command line arguments and retrieve :
        //    - the name of the "*.run" file
        //    - the name of the "*.prf" file
        //
        // Usage at the command line :
        //      ProgramName prefFile.prf runFile.run  <enter>
        // or 
        //      ProgramName runFile.run  prefFile.prf <enter>
        // ...
        //
        ///////////////////////////////////////////////////////////////////////////
        // NOTE: This function will throw an exception if any of the required file names 
        //       cannot be found in argv[]
        //
        SPARK::Problem::StaticBuild::ParseCommandLine(
            argc-1,       // number of entries in argv[]
            argv+1,       // we skip first entry which is full program name with path
            RunFileName,  // points to full name of "*.run" file in argv[]
            PrfFileName   // points to full name of "*.prf" file in argv[]
        );


        ///////////////////////////////////////////////////////////////////////////
        // Load the statically-built problem named after ProblemName
        //
        // NOTE: We use the session name by default as unique problem name.
        const string ProblemName( SPARK::GetBaseName() );

        if ( !SPARK::Problem::StaticBuild::Load(ProblemName.c_str()) ) {
            ostringstream ErrMsg;

            ErrMsg << "Could not load the statically-built problem \"" << ProblemName.c_str() << "\"." << ends;

            SPARK::ExitWithError(
                SPARK::ExitCode_ERROR_INVALID_PROBLEM,
                __FILE__,
                ErrMsg.str()
            );
        } 

#else
        ///////////////////////////////////////////////////////////////////////////
        // Process the command line arguments and retrieve :
        //    - the name of the "*.run" file
        //    - the name of the "*.prf" file
        //    - the name of the "*.xml" file
        //
        // Usage at the command line :
        //      ProgramName prefFile.prf runFile.run  xmlFile.xml  <enter>
        // or 
        //      ProgramName runFile.run  prefFile.prf xmlFile.xml  <enter>
        // or 
        //      ProgramName prefFile.prf xmlFile.xml  runFile.run  <enter>
        // ...
        //
        ///////////////////////////////////////////////////////////////////////////
        // NOTE: This function will throw an exception if any of the required file names 
        //       cannot be found in argv[]
        //
        SPARK::Problem::DynamicBuild::ParseCommandLine(
            argc-1,       // number of entries in argv[]
            argv+1,       // we skip first entry which is program name with path
            RunFileName,  // points to full name of "*.run" file in argv[]
            PrfFileName,  // points to full name of "*.prf" file in argv[]
            XmlFileName   // points to full name of "*.xml" file in argv[]
        );


        ///////////////////////////////////////////////////////////////////////////
        // Derive "unique" problem name from name of the XML file (without extension)
        string ProblemName;

        string Temp( XmlFileName ); // XmlFileName is a correct file name with XML extension
        string::size_type pos = Temp.find( "." );
        if ( pos != string::npos ) {
            ProblemName = Temp.substr(0, pos); // Truncate XML file name to form problem name
        }
        else {
            ProblemName = "My Problem"; // Default problem name
        }


        ///////////////////////////////////////////////////////////////////////////
        // Load the problem named ProblemName from the problem description defined in XML file
        //
        //      ProblemName :  unique problem name, we use the base name of the XML file by default!
        //      XmlFileName :  name of the XML file containing the problem description

        if ( !SPARK::Problem::DynamicBuild::Load( ProblemName.c_str(), XmlFileName ) ) {
            ostringstream ErrMsg;

            ErrMsg << "Could not load the problem \"" << ProblemName.c_str() << "\" at runtime from " 
                   << "the XML description in file \"" << XmlFileName << "\"" << ends;

            SPARK::ExitWithError(
                SPARK::ExitCode_ERROR_INVALID_PROBLEM,
                __FILE__,
                ErrMsg.str()
            );
        }

#endif // defined(SPARK_STATIC_BUILD)


        ///////////////////////////////////////////////////////////////////////////
        // Get address fo SPARK::TProblem instance
        //
        P = SPARK::Problem::Get( ProblemName.c_str() );


        ///////////////////////////////////////////////////////////////////////////
        // Initialize problem with run-time controls
        //
        SPARK::TRuntimeControls RuntimeControls( 
            "Default SPARK driver",  // name of controls set
            RunFileName              // *.run file name
        );
        P->Initialize( RuntimeControls );


        ///////////////////////////////////////////////////////////////////////////
        // Load preference settings
        //
        SPARK::TPreferenceSettings PreferenceSettings(  PrfFileName );
        P->LoadPreferenceSettings( PreferenceSettings );


        ///////////////////////////////////////////////////////////////////////////
        // Perform simulation
        //
        if ( P->Simulate() != SPARK::TProblem::SimulationFlag_OK ) {
            SPARK::ExitWithError( 
                SPARK::ExitCode_ERROR_NUMERICAL,
                __FILE__,
                "Abort simulation.",
                P // Specifiy active problem to ensure proper diagnostic
            );
        }

        ///////////////////////////////////////////////////////////////////////////
        // Cleanup problem object and write statistics out.
        P->Terminate();


        ///////////////////////////////////////////////////////////////////////////
        // Close global log files and perform automatic garbage collection for all 
        // problem instances
        ///////////////////////////////////////////////////////////////////////////
        SPARK::End();

    }


    /////////////////////////////////////////////////////////////////////////////////////
    // Catch unprocessed exceptions
    catch (const SPARK::XAssertion& x1) { // SPARK specific exceptions
        SPARK::ExitWithError(
            x1.code(),
            __FILE__,
            x1.message(),
            P // Specifiy active problem to ensure proper diagnostic
        );
    }
    catch (const std::exception& x2) { // std exceptions
        SPARK::ExitWithError(
            SPARK::ExitCode_ERROR_RUNTIME_ERROR,
            __FILE__,
            x2.what(),
            P // Specifiy active problem to ensure proper diagnostic
        );
    }

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


        




Generated on 5 Nov 2003 for VisualSPARK 2.01