gdrsclib
gdrs.h
Go to the documentation of this file.
1 /***************************************************
2 Apache License Version 2.0
3 
4 Copyright 2007-2011 EcoEquity and Stockholm Environment Institute
5 
6 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
7 file except in compliance with the License. You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software distributed under
12 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
13 KIND, either express or implied. See the License for the specific language governing
14 permissions and limitations under the License.
15 ****************************************************/
16 
21 #ifndef GDRSHEADER
22 #define GDRSHEADER
23 
24 #include "mainpage.h"
25 #include "datastruct.h"
26 #include "blockmath.h"
27 #include "sqlite3/sqlite3.h"
28 
29 #define GDRS_OK 0
30 #define GDRS_CFGREADERROR 255
31 #define GDRS_CFGCLOSEERROR 256
32 #define GDRS_FREADERROR 258
33 #define GDRS_FCLOSEERROR 259
34 #define GDRS_NOMEM 260
35 #define GDRS_DIDNOTRUN 261
36 #define GDRS_TOO_MANY_TAX_ENTRIES 262
37 
38 #define GDRS_ID_TABLE "id='gdrstable'"
39 #define GDRS_ROW_ID "calcrow"
40 #define GDRS_CLASS_REGION "class='gdrsregion'"
41 #define GDRS_ID_WORLD "class='gdrsregion' id='gdrsworld'"
42 #define GDRS_TEXT_COL "class='gdrstextcol'"
43 
44 #define GDRS_SQLITE_OFFSET 1024
45 #define GDRS_SQLITE_CANTDELETEDB 1023
46 #define GDRS_SQLITE_BUFSIZE 2048
47 #define GDRS_SQLITE_HALFBUFSIZE 1024
48 #define GDRS_NUM_PARAMS 12
49 #define GDRS_NUM_TAX_PARAMS 6
50 
51 #define EPS_NEWTON 1.0e-9
52 // Initial value of income for finding quintiles
53 #define INIT_Y 500
54 
55 #define PER_BEFORE_EP 0
56 #define PER_SEQUENCE 1
57 #define PER_GDRS 2
58 #define ANNEX1 1
59 #define ANNEX2 2
60 
61 #define XR_PPP 0
62 #define XR_MER 1
63 
64 #define TAX_MAX_ENTRIES 100
65 #define TAX_TYPE_VALUE 0
66 #define TAX_TYPE_NATL_QUINTILE 1
67 #define TAX_TYPE_GLOBAL_QUINTILE 2
68 
69 #define GAP_COMBINE_NONE 0
70 #define GAP_COMBINE_ADD 1
71 #define GAP_COMBINE_MAX 2
72 #define GAP_COMBINE_MIN 3
73 
79 struct tax_entry {
80  int seq_no;
81  int is_ppp;
82  int type;
83  double value;
84  double quintile;
85 };
86 
92 struct gdrs_struct {
93  struct appdata ad;
94  char **iso3;
95  int thisyear;
96  int cumsince;
97  int emergstart;
98  float billpercgwp;
99  float respweight;
100  float dt_low;
101  float dt_high;
104  float capfrac;
105  float emisselast;
108  float a1_perc_rdxn;
109  int a1refyear;
110  int use_lag;
123  int do_luxcap;
124  int use_kab;
129  struct datablock gdp;
130  struct datablock pop;
134  struct datablock gini;
135  struct datablock sdlog;
138  struct datablock r;
139  struct datablock c;
140  struct datablock rci;
147  struct datablock zl;
148  struct datablock zu;
150  struct datablock zu_adj;
163 };
164 
165 void handle_sql3error(sqlite3 *db, sqlite3_stmt *stmt);
166 int loadgdrs(struct gdrs_struct *gdrs, const char *fname);
167 int readtable(sqlite3 *db, const char *column, struct datablock *table, struct appdata ad);
168 void cleargdrs(struct gdrs_struct gdrs);
169 void nextrci(int year, struct gdrs_struct *gdrs);
170 double luxcap(int year, struct gdrs_struct *gdrs);
171 void combine_gap(struct fixedfactor* gap, int i, double newgap, int combine);
172 void calc_luxemiss(struct fixedfactor* gap, double* totgap, int year, int period, int combine, struct gdrs_struct *gdrs);
173 void calc_kab(struct fixedfactor* gap, double* totgap, int year, int period, int combine, struct gdrs_struct *gdrs);
174 void calcallocs(struct gdrs_struct *gdrs);
175 void calcybar(struct gdrs_struct *gdrs);
176 // Note that formally z can be calculated from y, ybar, and sigma, but for speed
177 // the value of z is calculated once for each country and then reused
178 struct datablock z(double y, struct gdrs_struct *gdrs, int ppp_or_mer);
179 double M(double a, double zc, double ybar, double sigma);
180 double globaldist(double y, int year, struct gdrs_struct *gdrs, int ppp_or_mer, double *dist_deriv);
181 double globalquintile(double q, int year, struct gdrs_struct *gdrs, int ppp_or_mer);
182 double a_gamma(double gamma, double y, double a, double b);
183 double b_gamma(double gamma, double y, double b);
184 double A_gamma(double gamma, double za, double zb, double yl, double yu, double ybar, double sigma);
185 double B_gamma(double gamma, double za, double zb, double yu, double ybar, double sigma);
186 double capbetween(double za, double zb, double zl, double zu, double yl, double yu_ppp, double ybar, double sigma, double pi, double luxmult);
187 double respbetween(double gamma, double za, double zb, double zl, double zu, double yl, double yu_ppp, double ybar, double sigma, double A);
188 double rhat(double E, double gamma, double sigma, double ybar);
189 double c_indiv(double y, double yl, double yu, int do_interp);
190 double r_indiv(double gamma, double y, double yl, double yu, double A, int do_interp);
191 
192 // sqlite3 output
193 int dump2sql3(const char *fname, struct gdrs_struct gdrs);
194 
195 #endif
For the application, basic dimensions: number of countries and bounding years.
Definition: datastruct.h:48
float emisselast
Emissions elasticity.
Definition: gdrs.h:105
int a1refyear
The year compared to which A1 reduces emissions (e.g., 1990 or 2005)
Definition: gdrs.h:109
int emergstart
Start year of "emergency progam".
Definition: gdrs.h:97
int dump2sql3(const char *fname, struct gdrs_struct gdrs)
After the calculation, write all results to the SQLite3 database.
Definition: gdrs.c:1800
double capbetween(double za, double zb, double zl, double zu, double yl, double yu_ppp, double ybar, double sigma, double pi, double luxmult)
Calculate capacity per capita between two income limits.
Definition: gdrs.c:1408
int use_lulucf
Flag whether to include land-use change emissions.
Definition: gdrs.h:117
struct datablock zu
Luxury threshold converted to "z" variable .
Definition: gdrs.h:148
struct datablock z(double y, struct gdrs_struct *gdrs, int ppp_or_mer)
Calculate the "z" transformed version of an income level for all countries and years.
Definition: gdrs.c:1754
void combine_gap(struct fixedfactor *gap, int i, double newgap, int combine)
Calculate and store luxury emissions.
Definition: gdrs.c:782
void calcybar(struct gdrs_struct *gdrs)
Calculate mean income from GDP and population.
Definition: gdrs.c:1714
float capfrac
Income counted toward capacity between thresholds.
Definition: gdrs.h:104
int cumsince
Calculate responsibility from this year.
Definition: gdrs.h:96
float a1_shape_param
If sequencing, exponent on tfrac^a1_shape_param that affects how rapidly A1 countries approach target...
Definition: gdrs.h:111
struct datablock pop
Population in people.
Definition: gdrs.h:130
struct datablock lux_emiss_applied
Luxury emissions actually used in adjusted baseline (calculated)
Definition: gdrs.h:143
double b_gamma(double gamma, double y, double b)
Calculate the function.
Definition: gdrs.c:1325
struct datablock gdp
GDP in MER.
Definition: gdrs.h:129
struct timeseries emergpath
The emergency pathway in GtC.
Definition: gdrs.h:137
void nextrci(int year, struct gdrs_struct *gdrs)
Calculate the GDRs Responsibility-Capacity Indicator for a particular year.
Definition: gdrs.c:1107
float lux_thresh_mult
Tax multiplier for incomes above luxury threshold.
Definition: gdrs.h:102
A one-dimensional array that contains information on countries that is the same for all years...
Definition: datastruct.h:84
double value
Definition: gdrs.h:83
char ** iso3
The ISO 3-letter code for the country.
Definition: gdrs.h:94
struct datablock r
Responsibility (calculated)
Definition: gdrs.h:138
This is the main struct that contains data from the sqlite3 database and calculated values...
Definition: gdrs.h:92
struct datablock ppp2mer
Ratio of GDP in PPP to GDP in MER.
Definition: gdrs.h:132
int use_nonco2
Flag whether to include non-CO2 gases.
Definition: gdrs.h:119
#define TAX_MAX_ENTRIES
Definition: gdrs.h:64
int is_ppp
Definition: gdrs.h:81
float respweight
Responsibility weight.
Definition: gdrs.h:99
struct datablock tax_revenue_ppp_dens[TAX_MAX_ENTRIES]
Table to hold contribution to tax in different countries & years for the different tax levels (multip...
Definition: gdrs.h:158
void cleargdrs(struct gdrs_struct gdrs)
Clear out the GDRs data structure.
Definition: gdrs.c:621
int sequenceyear
End of the first committment period with sequencing: only A1 up to here.
Definition: gdrs.h:107
A one-dimensional array that contains global information that is the same for all countries...
Definition: datastruct.h:93
float dt_high
Luxury threshold – in MER.
Definition: gdrs.h:101
struct fixedfactor kyoto_startep_frac
For Annex I countries, Kyoto committments at the start of the emergency program as a fraction of BaU...
Definition: gdrs.h:153
struct fixedfactor kyoto_ratified
A filter equal to 1.0 if ratified and 0.0 if not.
Definition: gdrs.h:154
int num_tax_levels
Number of entries in the tax level table in the database.
Definition: gdrs.h:155
double A_gamma(double gamma, double za, double zb, double yl, double yu, double ybar, double sigma)
Calculate the function.
Definition: gdrs.c:1347
double r_indiv(double gamma, double y, double yl, double yu, double A, int do_interp)
Calculate individual per capita annual responsibility at a specified income level.
Definition: gdrs.c:1546
void handle_sql3error(sqlite3 *db, sqlite3_stmt *stmt)
Function that wraps repetitive code for SQLite3 errors.
Definition: gdrs.c:38
struct datablock ybar_ppp
Mean income in PPP.
Definition: gdrs.h:133
struct sqlite3_stmt sqlite3_stmt
Definition: sqlite3.c:2777
float billpercgwp
Bill as percent of GWP.
Definition: gdrs.h:98
struct fixedfactor annex2
Filter for Annex 2 countries.
Definition: gdrs.h:114
struct datablock pop_above_lux
Population above luxury threshold (calculated)
Definition: gdrs.h:146
struct datablock tax_income_mer_dens[TAX_MAX_ENTRIES]
Table to hold tax income in MER terms (multiplied by pop & prob density–summable over countries) ...
Definition: gdrs.h:159
struct fixedfactor rci_lagged
Averaged RCI for mitigation smoothing.
Definition: gdrs.h:141
struct datablock bl_nonco2
Baseline non-CO2 gases.
Definition: gdrs.h:120
int do_luxcap
Flag whether to use luxury-capped baselines.
Definition: gdrs.h:123
struct datablock tax_income_ppp_dens[TAX_MAX_ENTRIES]
Table to hold tax income in PPP terms (multiplied by pop & prob density–summable over countries) ...
Definition: gdrs.h:160
int thisyear
Store the current year.
Definition: gdrs.h:95
void calc_kab(struct fixedfactor *gap, double *totgap, int year, int period, int combine, struct gdrs_struct *gdrs)
Calculate and store "Kyoto gap" for Kyoto-adjusted baselines.
Definition: gdrs.c:812
struct datablock zl
Development threshold converted to "z" variable .
Definition: gdrs.h:147
struct datablock zu_adj
Adjusted luxury threshold as "z" variable .
Definition: gdrs.h:150
struct datablock scalpop
Scaled population, in billions (calculated as 1.0e-9 * population)
Definition: gdrs.h:131
struct datablock kab_gap
Gap between Kyoto-adjusted baseline and standard baseline.
Definition: gdrs.h:151
double luxcap(int year, struct gdrs_struct *gdrs)
If calculating luxury-capped baselines adjust the luxury threshold if needed to stay above emergency ...
Definition: gdrs.c:1572
struct datablock gini
Gini coefficient.
Definition: gdrs.h:134
struct datablock bl_lulucf
Baseline land use, land-use change, and forestry.
Definition: gdrs.h:118
struct datablock baseline
Baseline emissions, the sum of all selected baseline components.
Definition: gdrs.h:126
void calcallocs(struct gdrs_struct *gdrs)
Calculate allocations for all years.
Definition: gdrs.c:947
struct fixedfactor frozen
Emissions that are "frozen" following the first sequencing period.
Definition: gdrs.h:115
double c_indiv(double y, double yl, double yu, int do_interp)
Calculate individual per capita capacity at a specified income level.
Definition: gdrs.c:1519
double globalquintile(double q, int year, struct gdrs_struct *gdrs, int ppp_or_mer)
Calculate the income at a given quintile for the world as a whole.
Definition: gdrs.c:739
struct datablock alloc_gdrs
Emissions allocation (calculated)
Definition: gdrs.h:144
int use_kab
Flag whether to use Kyoto-adjusted baselines (KABs)
Definition: gdrs.h:124
struct datablock lux_emiss
Emissions over the luxury threshold (calculated)
Definition: gdrs.h:142
struct tax_entry tax_levels[TAX_MAX_ENTRIES]
Table of tax levels with associated information.
Definition: gdrs.h:156
int readtable(sqlite3 *db, const char *column, struct datablock *table, struct appdata ad)
Helper function to read a column from the "core" table into a datablock.
Definition: gdrs.c:576
struct datablock tax_pop_below[TAX_MAX_ENTRIES]
Table to hold population below the tax line.
Definition: gdrs.h:162
struct datablock bl_fossil
Baseline fossil emissions.
Definition: gdrs.h:116
int usesequence
Boolean: true if Annex 1 countries act first (sequencing)
Definition: gdrs.h:106
struct datablock a1domrdxn
Annex 1 domestic reductions (if sequencing)
Definition: gdrs.h:128
struct datablock pop_above_dl
Population above development threshold (calculated)
Definition: gdrs.h:145
The tax table has a dynamically allocated array of entries of this form.
Definition: gdrs.h:79
struct datablock volrdxn
Voluntary reductions.
Definition: gdrs.h:127
float dt_low
Development threshold – in PPP.
Definition: gdrs.h:100
struct appdata ad
Basic information about dimensions: number of countries, start year, end year.
Definition: gdrs.h:93
double respbetween(double gamma, double za, double zb, double zl, double zu, double yl, double yu_ppp, double ybar, double sigma, double A)
Calculate responsibility between two income limits.
Definition: gdrs.c:1459
int assign_mitgap_to
If sequencing: either 1 (Annex 1) or 2 (Annex 2) for who bears responsibility for mitigation gap...
Definition: gdrs.h:112
struct datablock tax_revenue_mer_dens[TAX_MAX_ENTRIES]
Table to hold contribution to tax in different countries & years for the different tax levels (multip...
Definition: gdrs.h:157
double M(double a, double zc, double ybar, double sigma)
Calculate a function that is repeatedly used in calculations.
Definition: gdrs.c:1270
double B_gamma(double gamma, double za, double zb, double yu, double ybar, double sigma)
Calculate the function.
Definition: gdrs.c:1379
double rhat(double E, double gamma, double sigma, double ybar)
Calculate the factor for a country.
Definition: gdrs.c:1503
struct datablock c
Capacity (calculated)
Definition: gdrs.h:139
int interp_between_thresholds
If true, interpolate capacity and responsibility between thresholds.
Definition: gdrs.h:103
struct fixedfactor annex1
Filter for Annex 1 countries.
Definition: gdrs.h:113
int seq_no
Definition: gdrs.h:80
int loadgdrs(struct gdrs_struct *gdrs, const char *fname)
Function to translate from SQLite3 database to internal data structures.
Definition: gdrs.c:54
float a1_perc_rdxn
If sequencing, % below ref year that A1 must reduce it's emissions by sequence year.
Definition: gdrs.h:108
A two-dimensional array that contains information for country/year combinations.
Definition: datastruct.h:59
double globaldist(double y, int year, struct gdrs_struct *gdrs, int ppp_or_mer, double *dist_deriv)
Calculate the fraction of global population below a specified income level in the given year...
Definition: gdrs.c:687
struct datablock sdlog
Standard deviation of log income, estimated from Gini assuming lognormal distribution.
Definition: gdrs.h:135
struct timeseries yu_adj
Adjusted luxury threshold to ensure that adjusted baseline is not below emergency pathway...
Definition: gdrs.h:149
struct datablock rci
Responsibility-Capacity Indicator (calculated)
Definition: gdrs.h:140
int type
Definition: gdrs.h:82
double quintile
Definition: gdrs.h:84
struct datablock bl_netexports
Baseline net exports, for embodied carbon.
Definition: gdrs.h:122
struct fixedfactor kyoto_startep_frac_1990
For Annex I countries, Kyoto committments at the start of the emergency program as a fraction of 1990...
Definition: gdrs.h:152
double a_gamma(double gamma, double y, double a, double b)
Calculate the function.
Definition: gdrs.c:1299
struct datablock tax_pop_dens[TAX_MAX_ENTRIES]
Table to hold pop & prob dens at different tax levels.
Definition: gdrs.h:161
int emerg_path_id
Integer ID indicating which emergency path to use.
Definition: gdrs.h:136
int use_netexports
Flag whether to include emissions embodied in traded goods.
Definition: gdrs.h:121
int use_lag
Apply a (varying) time lag between mitigation investment and realized reductions. ...
Definition: gdrs.h:110
void calc_luxemiss(struct fixedfactor *gap, double *totgap, int year, int period, int combine, struct gdrs_struct *gdrs)
Calculate and store luxury emissions.
Definition: gdrs.c:892
int kab_only_ratified
Flag whether to only include countries that ratified the Kyoto agreement.
Definition: gdrs.h:125