Skip to content

Commit

Permalink
Added first version of nonlinear solver Nox.
Browse files Browse the repository at this point in the history
cannot be compilated at the moment, because Trilinos is not part of 3rdParty yet.
  • Loading branch information
qichenghua authored and OpenModelica-Hudson committed Dec 19, 2016
1 parent ca147d7 commit 3f1dccb
Show file tree
Hide file tree
Showing 12 changed files with 836 additions and 2 deletions.
104 changes: 102 additions & 2 deletions SimulationRuntime/cpp/CMakeLists.txt
Expand Up @@ -77,6 +77,7 @@ OPTION(RUNTIME_PROFILING "RUNTIME_PROFILING" OFF)
OPTION(FMU_SUNDIALS "FMU_SUNDIALS" OFF)
OPTION(SUNDIALS_ROOT "SUNDIALS ROOT" "")
OPTION(KLU_ROOT "KLU ROOT" "")
OPTION(TRILINOS_ROOT "TRILINOS ROOT" "")
OPTION(USE_CPP_03 "USE_CPP_03" OFF)

#Set Variables
Expand Down Expand Up @@ -117,6 +118,7 @@ SET(RTEulerName ${LIBPREFIX}RTEuler${LIBSUFFIX})
SET(IdaName ${LIBPREFIX}Ida${LIBSUFFIX})
SET(IdasName ${LIBPREFIX}Idas${LIBSUFFIX})
SET(KinsolName ${LIBPREFIX}Kinsol${LIBSUFFIX})
SET(NoxName ${LIBPREFIX}Nox${LIBSUFFIX})
SET(LinearSolverName ${LIBPREFIX}LinearSolver${LIBSUFFIX})
SET(DgesvSolverName ${LIBPREFIX}DgesvSolver${LIBSUFFIX})
SET(ModelicaName ${LIBPREFIX}Modelica${LIBSUFFIX})
Expand All @@ -134,6 +136,7 @@ SET(DgesvName ${LIBPREFIX}Dgesv) # only static version

SET(USE_MICO OFF)
SET(USE_KLU OFF)
SET(USE_TRILINOS OFF)
SET(REDUCE_DAE OFF)
IF(NOT FMU_TARGET OR FMU_SUNDIALS)
SET(USE_SUNDIALS ON)
Expand Down Expand Up @@ -575,6 +578,8 @@ IF(USE_KLU)
#set klu solvers include and library directories
IF(MSVC)
SET(Klu_Path "${CMAKE_SOURCE_DIR}/../../3rdParty/SuiteSparse/install_msvc")
# SET(KLU_INLCUDE_HOME ${Klu_Path}/include/omc/c/suitesparse/Include)
# SET(KLU_LIBRARY_RELEASE_HOME ${Klu_Path}/lib/omc)
ELSE(MSVC)
SET(Klu_Path "${CMAKE_SOURCE_DIR}/../../3rdParty/SuiteSparse/build")
ENDIF(MSVC)
Expand Down Expand Up @@ -615,11 +620,97 @@ IF(USE_KLU)
SET(KLU_LIBRARIES ${KLU_LIB} ${AMD_LIB} ${BTF} ${COLAMD})

MESSAGE(STATUS "Klu Libraries:")
MESSAGE(STATUS "${KNU_LIBRARIES}")

MESSAGE(STATUS "${KLU_LIBRARIES}")
ADD_DEFINITIONS(-Dklu)
ENDIF(USE_KLU)



# Find Trilinos/Nox solvers
IF(USE_TRILINOS)
IF(MSVC)
SET(Trilinos_Path "")
ELSE(MSVC)
SET(Trilinos_Path "C:/Projekte/Trilinos_install")
ENDIF(MSVC)
SET(TRILINOS_INLCUDE_HOME ${Trilinos_Path}/include)
SET(TRILINOS_LIBRARY_RELEASE_HOME ${Trilinos_Path}/bin)
MESSAGE(STATUS "Trilinos include:")
MESSAGE(STATUS "${TRILINOS_INLCUDE_HOME}")
MESSAGE(STATUS "Trilinos library:")
MESSAGE(STATUS "${TRILINOS_LIBRARY_RELEASE_HOME}")

FIND_PATH(TRILINOS_INCLUDE_DIR NOX.h PATHS ${TRILINOS_INLCUDE_HOME} $ENV{TRILINOS_ROOT}/include)
IF (NOT TRILINOS_INCLUDE_DIR)
MESSAGE(FATAL_ERROR "Could not find Trilinos, specify environment variable TRILINOS_ROOT")
ELSE(NOT TRILINOS_INCLUDE_DIR)
INCLUDE_DIRECTORIES(${TRILINOS_INCLUDE_DIR})
ENDIF(NOT TRILINOS_INCLUDE_DIR)

FIND_LIBRARY(NOX_LIB "nox" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT NOX_LIB)
MESSAGE(FATAL_ERROR "Could not find nox library!")
ENDIF()

FIND_LIBRARY(NOXLAPACK_LIB "noxlapack" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT NOXLAPACK_LIB)
MESSAGE(FATAL_ERROR "Could not find noxlapack library!")
ENDIF()

FIND_LIBRARY(BELOS_LIB "belos" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT BELOS_LIB)
MESSAGE(FATAL_ERROR "Could not find belos library!")
ENDIF()

FIND_LIBRARY(LOCA_LIB "loca" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT LOCA_LIB)
MESSAGE(FATAL_ERROR "Could not find loca library!")
ENDIF()

FIND_LIBRARY(LOCALAPACK_LIB "localapack" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT LOCALAPACK_LIB)
MESSAGE(FATAL_ERROR "Could not find localapack library!")
ENDIF()

FIND_LIBRARY(ML_LIB "ml" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT ML_LIB)
MESSAGE(FATAL_ERROR "Could not find ml library!")
ENDIF()

FIND_LIBRARY(TEUCHOSCOMM_LIB "teuchoscomm" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT TEUCHOSCOMM_LIB)
MESSAGE(FATAL_ERROR "Could not find teuchoscomm library!")
ENDIF()

FIND_LIBRARY(TEUCHOSCORE_LIB "teuchoscore" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT TEUCHOSCORE_LIB)
MESSAGE(FATAL_ERROR "Could not find teuchoscore library!")
ENDIF()

FIND_LIBRARY(TEUCHOSNUMERICS_LIB "teuchosnumerics" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT TEUCHOSNUMERICS_LIB)
MESSAGE(FATAL_ERROR "Could not find teuchosnumerics library!")
ENDIF()

FIND_LIBRARY(TEUCHOSPARAMETERLIST_LIB "teuchosparameterlist" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT TEUCHOSPARAMETERLIST_LIB)
MESSAGE(FATAL_ERROR "Could not find teuchosparameterlist library!")
ENDIF()

FIND_LIBRARY(TEUCHOSREMAINDER_LIB "teuchosremainder" NO_DEFAULT_PATH NO_SYSTEM_ENVIRONMENT_PATH PATHS ${TRILINOS_LIBRARY_RELEASE_HOME} $ENV{TRILINOS_ROOT}/lib)
IF(NOT TEUCHOSREMAINDER_LIB)
MESSAGE(FATAL_ERROR "Could not find teuchosremainder library!")
ENDIF()

SET(TRILINOS_LIBRARIES ${NOX_LIB} ${NOXLAPACK_LIB} ${BELOS_LIB} ${LOCA_LIB} ${LOCALAPACK_LIB} ${ML_LIB} ${TEUCHOSCOMM_LIB} ${TEUCHOSCORE_LIB} ${TEUCHOSNUMERICS_LIB} ${TEUCHOSPARAMETERLIST_LIB} ${TEUCHOSREMAINDER_LIB} )

MESSAGE(STATUS "Trilinos Libraries:")
MESSAGE(STATUS "${TRILINOS_LIBRARIES}")

ADD_DEFINITIONS(-Dtrilinos)
ENDIF(USE_TRILINOS)


#Handle umfpack
#FIND_PACKAGE(SuiteSparse)
IF(SUITESPARSE_UMFPACK_FOUND)
Expand Down Expand Up @@ -778,6 +869,15 @@ IF(USE_SUNDIALS)
#set(IDA_LIB ${libIdaName})
ENDIF(USE_SUNDIALS)

IF(USE_TRILINOS)
ADD_SUBDIRECTORY(Solver/Nox)

GET_TARGET_PROPERTY(libNox ${NoxName} LOCATION)
GET_FILENAME_COMPONENT(libNoxName ${libNox} NAME)

SET(NOX_LIB ${libNoxName})
ENDIF(USE_TRILINOS)

GET_TARGET_PROPERTY(libPeer ${PeerName} LOCATION)
GET_FILENAME_COMPONENT(libPeerName ${libPeer} NAME)

Expand Down
Expand Up @@ -83,6 +83,19 @@ struct NonLinSolverOMCFactory : virtual public ObjectFactory<CreationPolicy>
}
nonlin_solver_key.assign("extension_export_hybrj");
}
else if(nonlin_solver.compare("nox")==0)
{
fs::path nox_path = ObjectFactory<CreationPolicy>::_library_path;
fs::path nox_name(NOX_LIB);
nox_path/=nox_name;
LOADERRESULT result = ObjectFactory<CreationPolicy>::_factory->LoadLibrary(nox_path.string(),*_non_linsolver_type_map);
if (result != LOADER_SUCCESS)
{

throw ModelicaSimulationError(MODEL_FACTORY,"Failed loading Nox solver library!");
}
nonlin_solver_key.assign("extension_export_nox");
}
else
throw ModelicaSimulationError(MODEL_FACTORY,"Selected nonlinear solver is not available");
_last_selected_solver = nonlin_solver;
Expand Down
17 changes: 17 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Nox/FactoryExport.h
@@ -0,0 +1,17 @@
#pragma once
/** @defgroup solverNox Solver.Nox
* Nox class wrapper from Trilinos package
* @{
*/
#if defined(__vxworks) || defined(__TRICORE__) || defined(RUNTIME_STATIC_LINKING)
#define BOOST_EXTENSION_LOGGER_DECL
#define BOOST_EXTENSION_SOLVER_DECL
#define BOOST_EXTENSION_SOLVERSETTINGS_DECL
#elif defined(OMC_BUILD) || defined(SIMSTER_BUILD)
#define BOOST_EXTENSION_LOGGER_DECL BOOST_EXTENSION_IMPORT_DECL
#define BOOST_EXTENSION_SOLVER_DECL BOOST_EXTENSION_IMPORT_DECL
#define BOOST_EXTENSION_SOLVERSETTINGS_DECL BOOST_EXTENSION_IMPORT_DECL
#else
error "operating system not supported"
#endif
/** @} */ // end of solverNox
75 changes: 75 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Nox/Nox.h
@@ -0,0 +1,75 @@
#pragma once
/** @addtogroup solverNox
*
* @{
*/



class Nox : public IAlgLoopSolver
{
public:
Nox(INonLinearAlgLoop* algLoop, INonLinSolverSettings* settings);
virtual ~Nox();

/// (Re-) initialize the solver
virtual void initialize();

/// Solution of a (non-)linear system of equations
virtual void solve();

/// Returns the status of iteration
virtual ITERATIONSTATUS getIterationStatus();
virtual void stepCompleted(double time);
virtual void restoreOldValues();
virtual void restoreNewValues();

private:

void getsolutionandevaluate(NOX::LAPACK::Group grp);
void solverinit();
void createStatusTests();
void createSolverParameters();

//void check4EventRetry(double* y)

// Member variables
//---------------------------------------------------------------
INonLinSolverSettings
*_noxSettings; ///< Settings for the solver

INonLinearAlgLoop
*_algLoop; ///< Algebraic loop to be solved

ITERATIONSTATUS
_iterationStatus; ///< Output - Denotes the status of iteration

long int _dimSys;

double
*_y,
*_y0,
*_y_old,
*_y_new,
*_yScale;

NoxLapackInterface *_noxLapackInterface;

Teuchos::RCP<NOX::LAPACK::Group> _grp;

//used for status tests
Teuchos::RCP<NOX::StatusTest::NormF> _statusTestNormF;
Teuchos::RCP<NOX::StatusTest::MaxIters> _statusTestMaxIters;
Teuchos::RCP<NOX::StatusTest::Combo> _statusTestsCombo;

//list of solver parameters
Teuchos::RCP<Teuchos::ParameterList> _solverParametersPtr;

//solver
Teuchos::RCP<NOX::Solver::Generic> _solver;

bool _firstCall;
bool _generateoutput;
bool _useScale;
};
/** @} */ // end of solverNox
27 changes: 27 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Nox/NoxLapackInterface.h
@@ -0,0 +1,27 @@
#include "NOX_LAPACK_Group.H"

class NoxLapackInterface : public NOX::LAPACK::Interface {

public:

//! Constructor
NoxLapackInterface(INonLinearAlgLoop *algLoop);

//! Destructor
~NoxLapackInterface();

const NOX::LAPACK::Vector& getInitialGuess();

bool computeF(NOX::LAPACK::Vector& f, const NOX::LAPACK::Vector &x);

bool computeJacobian(NOX::LAPACK::Matrix<double>& J, const NOX::LAPACK::Vector & x);

private:
//! Initial guess
Teuchos::RCP<NOX::LAPACK::Vector> _initialGuess;
INonLinearAlgLoop *_algLoop;///< Algebraic loop to be solved, required to obtain value of f
double _yScale;
int _dimSys;
bool _generateoutput;
bool _useScale;
};
36 changes: 36 additions & 0 deletions SimulationRuntime/cpp/Include/Solver/Nox/NoxSettings.h
@@ -0,0 +1,36 @@
#pragma once
/** @addtogroup solverNox
*
* @{
*/
#include <Core/Solver/INonLinSolverSettings.h>

class NoxSettings :public INonLinSolverSettings
{
public:
NoxSettings();
/*max. Anzahl an Newtonititerationen pro Schritt (default: 25)*/
virtual long int getNewtMax();
virtual void setNewtMax(long int);
/* Relative Toleranz für die Newtoniteration (default: 1e-6)*/
virtual double getRtol();
virtual void setRtol(double);
/*Absolute Toleranz für die Newtoniteration (default: 1e-6)*/
virtual double getAtol();
virtual void setAtol(double);
/*Dämpfungsfaktor (default: 0.9)*/
virtual double getDelta();
virtual void setDelta(double);
virtual void load(string);

virtual void setContinueOnError(bool);
virtual bool getContinueOnError();
private:
long int _iNewt_max; ///< max. Anzahl an Newtonititerationen pro Schritt (default: 25)

double _dRtol; ///< Relative Toleranz für die Newtoniteration (default: 1e-6)
double _dAtol; ///< Absolute Toleranz für die Newtoniteration (default: 1e-6)
double _dDelta; ///< Dämpfungsfaktor (default: 0.9)
bool _continueOnError;
};
/** @} */ // end of solverNox
1 change: 1 addition & 0 deletions SimulationRuntime/cpp/LibrariesConfig.h.in
Expand Up @@ -19,6 +19,7 @@
#define SOLVER_LIB "@SOLVER_LIB@"
#define MATH_LIB "@MATH_LIB@"
#define KINSOL_LIB "@KINSOL_LIB@"
#define NOX_LIB "@NOX_LIB@"
#define LINEARSOLVER_LIB "@LINEARSOLVER_LIB@"
#define HYBRJ_LIB "@HYBRJ_LIB@"
#define SIMOBJFACTORY_LIB "@SIMOBJFACTORY_LIB@"
Expand Down
21 changes: 21 additions & 0 deletions SimulationRuntime/cpp/Solver/Nox/CMakeLists.txt
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 2.8.9)

project(${NoxName})

add_library(${NoxName} Nox.cpp NoxSettings.cpp FactoryExport.cpp NoxLapackInterface.cpp)

if(NOT BUILD_SHARED_LIBS)
set_target_properties(${NoxName} PROPERTIES COMPILE_DEFINITIONS "RUNTIME_STATIC_LINKING;ENABLE_SUNDIALS_STATIC")
endif(NOT BUILD_SHARED_LIBS)

add_precompiled_header(${NoxName} Include/Core/Modelica.h)
target_link_libraries(${NoxName} ${ExtensionUtilitiesName} ${Boost_LIBRARIES} ${TRILINOS_LIBRARIES} ) #C:/OpenModelica/OMCompiler/SimulationRuntime/cpp/Solver/KLU/OMCppklu_static.lib
#target_link_libraries(${NoxName} ${ExtensionUtilitiesName} ${Boost_LIBRARIES} ${SUNDIALS_LIBRARIES} ${LAPACK_LIBRARIES} ${kluName})

install(TARGETS ${NoxName} DESTINATION ${LIBINSTALLEXT})
install(FILES
${CMAKE_SOURCE_DIR}/Include/Solver/Nox/Nox.h
${CMAKE_SOURCE_DIR}/Include/Solver/Nox/NoxSettings.h
${CMAKE_SOURCE_DIR}/Include/Solver/Nox/FactoryExport.h
${CMAKE_SOURCE_DIR}/Include/Solver/Nox/NoxLapackInterface.h
DESTINATION include/omc/cpp/Solver/Nox)

0 comments on commit 3f1dccb

Please sign in to comment.