Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
671 werner 1
/********************************************************************************************
2
**    iLand - an individual based forest landscape and disturbance model
3
**    http://iland.boku.ac.at
4
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**
6
**    This program is free software: you can redistribute it and/or modify
7
**    it under the terms of the GNU General Public License as published by
8
**    the Free Software Foundation, either version 3 of the License, or
9
**    (at your option) any later version.
10
**
11
**    This program is distributed in the hope that it will be useful,
12
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    GNU General Public License for more details.
15
**
16
**    You should have received a copy of the GNU General Public License
17
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
********************************************************************************************/
19
 
526 werner 20
#ifndef GLOBALSETTINGS_H
21
#define GLOBALSETTINGS_H
22
 
23
#include <QtCore>
24
#include <QtSql>
25
#include <QtSql/QSqlDatabase>
26
 
705 werner 27
#include "global.h"
526 werner 28
#include "settingmetadata.h"
29
#include "xmlhelper.h"
705 werner 30
// The favorite random number generator:
31
// use either the MersenneTwister or the WELLS algoritm:
32
// #include "randomwell.h"
33
// #include "../3rdparty/MersenneTwister.h"
526 werner 34
// use faster method to concatenate strings (see qt - documentation on QString)
35
#define QT_USE_FAST_CONCATENATION
36
#define QT_USE_FAST_OPERATOR_PLUS
37
 
38
typedef QList<QVariant> DebugList;
39
 
40
class Model;
41
class OutputManager;
590 werner 42
class ModelController; // forward
615 werner 43
class SystemStatistics;
793 werner 44
class QJSEngine; // forward
526 werner 45
 
46
/// General settings and globally available data
47
class GlobalSettings
48
{
49
public:
50
    // singleton-access
51
    static GlobalSettings *instance() { if (mInstance) return mInstance; mInstance = new GlobalSettings(); return mInstance; }
52
    ~GlobalSettings();
53
    // Access
54
    // model and clock
590 werner 55
    Model *model() const { return mModel; }
56
    ModelController *controller() const { return mModelController; }
57
 
526 werner 58
    void setModel(Model *model) {mModel = model; }
590 werner 59
    void setModelController(ModelController *mc) {mModelController = mc; }
60
 
526 werner 61
    int currentYear() const { return mRunYear; }
62
    void setCurrentYear(const int year) { mRunYear = year; }
584 werner 63
 
767 werner 64
    /// access the global QScriptEngine used throughout the model
65
    /// for all Javascript related functionality.
66
    QString executeJavascript(const QString &command);
1157 werner 67
    /// execute a javasript function in the global context
68
    QString executeJSFunction(const QString function_name);
793 werner 69
    QJSEngine *scriptEngine() const { return mScriptEngine; }
767 werner 70
    void resetScriptEngine(); ///< re-creates the script engine (when the Model is re-created)
71
 
615 werner 72
    // system statistics
73
    SystemStatistics *systemStatistics() { return mSystemStatistics; }
74
 
526 werner 75
    // debugging fain grained debug outputs
76
    enum DebugOutputs { dTreeNPP=1, dTreePartition=2, dTreeGrowth=4,
1196 werner 77
                        dStandGPP=8, dWaterCycle=16, dDailyResponses=32,
1168 werner 78
                        dEstablishment=64, dSaplingGrowth=128, dCarbonCycle=256,
79
                        dPerformance=512}; ///< defines available debug output types.
526 werner 80
    void setDebugOutput(const int debug) { mDebugOutputs = GlobalSettings::DebugOutputs(debug); }
81
    void setDebugOutput(const DebugOutputs dbg, const bool enable=true); ///< enable/disable a specific output type.
82
    bool isDebugEnabled(const DebugOutputs dbg) {return int(dbg) & mDebugOutputs;} ///< returns true, if a specific debug outut type is enabled.
83
    int currentDebugOutput() const { return mDebugOutputs; }
599 werner 84
    QString debugOutputName(const DebugOutputs d); ///< returns the name attached to 'd' or an empty string if not found
85
    DebugOutputs debugOutputId(const QString debug_name); ///< returns the DebugOutputs bit or 0 if not found
526 werner 86
    DebugList &debugList(const int ID, const DebugOutputs dbg); ///< returns a ref to a list ready to be filled with debug output of a type/id combination.
630 werner 87
    const QList<const DebugList*> debugLists(const int ID, const DebugOutputs dbg); ///< return a list of debug outputs
526 werner 88
    QStringList debugListCaptions(const DebugOutputs dbg); ///< returns stringlist of captions for a specific output type
89
    QList<QPair<QString, QVariant> > debugValues(const int ID); ///< all debug values for object with given ID
90
    void clearDebugLists(); ///< clear all debug data
613 werner 91
    QStringList debugDataTable(GlobalSettings::DebugOutputs type, const QString separator, const QString fileName=QString()); ///< output for all available items (trees, ...) in table form
584 werner 92
 
93
    // database access functions
526 werner 94
    QSqlDatabase dbin() { return QSqlDatabase::database("in"); }
95
    QSqlDatabase dbout() { return QSqlDatabase::database("out"); }
96
    QSqlDatabase dbclimate() { return QSqlDatabase::database("climate"); }
584 werner 97
 
526 werner 98
    // setting-meta-data
99
    /// access an individual SettingMetaData named @p name.
584 werner 100
    const SettingMetaData *settingMetaData(const QString &name); // unused??
526 werner 101
    /// retrieve the default value of the setting @p name.
584 werner 102
    QVariant settingDefaultValue(const QString &name); // unused?
526 werner 103
    QList<QString> settingNames() { return mSettingMetaData.keys(); } ///< retrieve list of all names of settings.
104
 
105
    // path and directory
106
    QString path(const QString &fileName, const QString &type="home");
107
    bool fileExists(const QString &fileName, const QString &type="home");
108
 
109
    // xml project file
110
    const XmlHelper &settings() const { return mXml; }
111
 
112
    // setup and maintenance
113
 
114
    // xml project settings
115
    void loadProjectFile(const QString &fileName);
116
 
117
    // meta data of settings
118
    void loadSettingsMetaDataFromFile(const QString &fileName);
119
    void loadSettingsMetaDataFromXml(const QDomElement &topNode);
120
 
121
    // Database connections
122
    bool setupDatabaseConnection(const QString& dbname, const QString &fileName, bool fileMustExist);
123
    void clearDatabaseConnections(); ///< shutdown and clear connections
124
    // output manager
125
    OutputManager *outputManager() { return mOutputManager; }
126
 
127
    // path
128
    void setupDirectories(QDomElement pathNode, const QString &projectFilePath);
1157 werner 129
    void printDirectories() const;
526 werner 130
 
131
 
132
private:
133
    GlobalSettings(); // private ctor
134
    static GlobalSettings *mInstance;
135
    Model *mModel;
590 werner 136
    ModelController *mModelController;
526 werner 137
    OutputManager *mOutputManager;
793 werner 138
    QJSEngine *mScriptEngine;
526 werner 139
    int mRunYear;
615 werner 140
    SystemStatistics *mSystemStatistics;
526 werner 141
 
142
    // special debug outputs
143
    QMultiHash<int, DebugList> mDebugLists;
144
    int mDebugOutputs; // "bitmap" of enabled debugoutputs.
145
 
146
    SettingMetaDataList mSettingMetaData; ///< storage container (QHash) for settings.
147
    QHash<QString, QString> mFilePath; ///< storage for file paths
148
 
149
    XmlHelper mXml; ///< xml-based hierarchical settings
150
};
151
 
152
// constants
153
// We assume:
154
// Light-Grid: 2x2m
155
// Height-Grid: 10x10m
156
// Resource-Unit: 100x100m
157
const int cPxSize = 2; // size of light grid (m)
158
const int cRUSize = 100; // size of resource unit (m)
1157 werner 159
const double cRUArea = 10000.; // area of a resource unit (m2)
912 werner 160
const int cHeightSize = 10; // size of a height grid pixel (m)
526 werner 161
const int cPxPerHeight = 5; // 10 / 2 LIF pixels per height pixel
162
const int cPxPerRU = 50; // 100/2
163
const int cHeightPerRU = 10; // 100/10 height pixels per resource unit
164
const int cPxPerHectare = 2500; // pixel/ha ( 10000 / (2*2) )
574 werner 165
const double cHeightPixelArea = 100.; // 100m2 area of a height pixel
526 werner 166
 
167
// other constants
168
const double biomassCFraction = 0.5; // fraction of (dry) biomass which is carbon
608 werner 169
const double cAutotrophicRespiration = 0.47;
526 werner 170
 
171
/// shortcut to the GlobalSettings Singleton object.
172
#define Globals (GlobalSettings::instance())
173
 
911 werner 174
// provide a hashing function for the QPoint type (needed from stand init functions, ABE, ...)
175
inline uint qHash(const QPoint &key)
176
{
177
    return qHash(key.x()) ^ qHash(key.y());
178
}
526 werner 179
#endif // GLOBALSETTINGS_H