Subversion Repositories public iLand

Rev

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

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