|
| 1 | +/* |
| 2 | + * This file is part of OpenModelica. |
| 3 | + * |
| 4 | + * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC), |
| 5 | + * c/o Linköpings universitet, Department of Computer and Information Science, |
| 6 | + * SE-58183 Linköping, Sweden. |
| 7 | + * |
| 8 | + * All rights reserved. |
| 9 | + * |
| 10 | + * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR |
| 11 | + * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2. |
| 12 | + * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES |
| 13 | + * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, |
| 14 | + * ACCORDING TO RECIPIENTS CHOICE. |
| 15 | + * |
| 16 | + * The OpenModelica software and the Open Source Modelica |
| 17 | + * Consortium (OSMC) Public License (OSMC-PL) are obtained |
| 18 | + * from OSMC, either from the above address, |
| 19 | + * from the URLs: http://www.ida.liu.se/projects/OpenModelica or |
| 20 | + * http://www.openmodelica.org, and in the OpenModelica distribution. |
| 21 | + * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html. |
| 22 | + * |
| 23 | + * This program is distributed WITHOUT ANY WARRANTY; without |
| 24 | + * even the implied warranty of MERCHANTABILITY or FITNESS |
| 25 | + * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH |
| 26 | + * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. |
| 27 | + * |
| 28 | + * See the full OSMC Public License conditions for more details. |
| 29 | + * |
| 30 | + */ |
| 31 | + |
| 32 | +#ifndef _OMS_ALGLOOP_H_ |
| 33 | +#define _OMS_ALGLOOP_H_ |
| 34 | + |
| 35 | +#include <string> |
| 36 | +#include <vector> |
| 37 | +#include "Types.h" |
| 38 | +#include "DirectedGraph.h" |
| 39 | + |
| 40 | +#include <kinsol/kinsol.h> |
| 41 | +#include <nvector/nvector_serial.h> |
| 42 | +#include <sunlinsol/sunlinsol_dense.h> /* Default dense linear solver */ |
| 43 | + |
| 44 | +namespace oms |
| 45 | +{ |
| 46 | + class System; |
| 47 | + class DirectedGraph; |
| 48 | + |
| 49 | + typedef struct KINSOL_USER_DATA { |
| 50 | + System* syst; |
| 51 | + DirectedGraph* graph; |
| 52 | + const int algLoopNumber; |
| 53 | + }KINSOL_USER_DATA; |
| 54 | + |
| 55 | + class KinsolSolver |
| 56 | + { |
| 57 | + public: |
| 58 | + ~KinsolSolver(); |
| 59 | + static KinsolSolver* NewKinsolSolver(const int algLoopNum, const unsigned int size, double absoluteTolerance); |
| 60 | + oms_status_enu_t kinsolSolve(System& syst, DirectedGraph& graph); |
| 61 | + |
| 62 | + private: |
| 63 | + /* tolerances */ |
| 64 | + double fnormtol; /* function tolerance */ |
| 65 | + |
| 66 | + /* work arrays */ |
| 67 | + N_Vector initialGuess; |
| 68 | + N_Vector uScale; /* Scaling vector for u */ |
| 69 | + N_Vector fScale; /* Scaling vector for f(u) */ |
| 70 | + N_Vector fTmp; /* Vector used for tmp computations */ |
| 71 | + |
| 72 | + /* kinsol internal data */ |
| 73 | + void* kinsolMemory; |
| 74 | + void* userData; |
| 75 | + int size; |
| 76 | + |
| 77 | + /* linear solver data */ |
| 78 | + SUNLinearSolver linSol; /* Linear solver object used by KINSOL */ |
| 79 | + N_Vector y; /* Template for cloning vectors needed inside linear solver */ |
| 80 | + SUNMatrix J; /* (Non-)Sparse matrix template for cloning matrices needed within linear solver */ |
| 81 | + |
| 82 | + /* member function */ |
| 83 | + static int nlsKinsolResiduals(N_Vector uu, N_Vector fval, void *userData); |
| 84 | + static void sundialsErrorHandlerFunction(int error_code, const char *module, const char *function, char *msg, void *userData); |
| 85 | + static void sundialsInfoHandlerFunction(const char *module, const char *function, char *msg, void *userData); |
| 86 | + }; |
| 87 | + |
| 88 | + class AlgLoop |
| 89 | + { |
| 90 | + public: |
| 91 | + AlgLoop(oms_alg_solver_enu_t method, double absTol, oms_ssc_t SCC, const int systNumber); |
| 92 | + |
| 93 | + oms_ssc_t getSCC() {return SCC;} |
| 94 | + oms_status_enu_t solveAlgLoop(System& syst, DirectedGraph& graph); |
| 95 | + std::string getAlgSolverName(); |
| 96 | + std::string dumpLoopVars(DirectedGraph& graph); |
| 97 | + |
| 98 | + private: |
| 99 | + oms_alg_solver_enu_t algSolverMethod; |
| 100 | + oms_status_enu_t fixPointIteration(System& syst, DirectedGraph& graph); |
| 101 | + |
| 102 | + KinsolSolver* kinsolData; |
| 103 | + |
| 104 | + /* Loop data */ |
| 105 | + const oms_ssc_t SCC; ///< Strong connected components |
| 106 | + const int systNumber; |
| 107 | + double absoluteTolerance; |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +#endif // _OMS_ALGLOOP_H_ |
0 commit comments