Commit 75d7c04a authored by Ayache Mohamed's avatar Ayache Mohamed
Browse files

src/

parent 0ef7e252
########################################################################
## ##
## M a k e f i l e ##
## ##
## Makefile for C implementation of LPJ ##
## Version with managed land and river routing ##
## ##
## written by Werner von Bloh ##
## Potsdam Institute for Climate Impact Research ##
## P.O. Box 60 12 03 ##
## 14412 Potsdam/Germany ##
## ##
## Last change: $Date:: 2015-07-22 11:01:20 +0200 (mer., 22 juil.#$ ##
## By : $Author:: bloh $ ##
## ##
########################################################################
include ../Makefile.inc
OBJ = lpjml.$O
SRC = lpjml.c
INC = ../include
HDRS = $(INC)/buffer.h $(INC)/coord.h $(INC)/lpj.h $(INC)/pftlist.h\
$(INC)/soil.h $(INC)/manage.h $(INC)/climate.h $(INC)/date.h\
$(INC)/pft.h $(INC)/pftpar.h $(INC)/types.h $(INC)/outfile.h\
$(INC)/header.h $(INC)/landuse.h $(INC)/crop.h $(INC)/tree.h\
$(INC)/grass.h\
$(INC)/errmsg.h $(INC)/numeric.h $(INC)/conf.h $(INC)/swap.h\
$(INC)/soilpar.h $(INC)/managepar.h $(INC)/stand.h\
$(INC)/list.h $(INC)/cell.h $(INC)/units.h $(INC)/output.h\
$(INC)/config.h $(INC)/intlist.h $(INC)/queue.h $(INC)/pnet.h\
$(INC)/discharge.h $(INC)/channel.h $(INC)/biomes.h\
$(INC)/image.h $(INC)/input.h $(INC)/natural.h $(INC)/grassland.h\
$(INC)/biomass_tree.h $(INC)/biomass_grass.h $(INC)/cdf.h\
$(INC)/agriculture.h $(INC)/reservoir.h $(INC)/spitfire.h\
$(INC)/agriculture_tree.h\
$(INC)/nitrogen.h
LIBDIR = ../lib
BINDIR = ../bin
LPJLIBS = $(LIBDIR)/liblpj.$A $(LIBDIR)/libgrass.$A $(LIBDIR)/liblanduse.$A\
$(LIBDIR)/libtree.$A $(LIBDIR)/libimage.$A\
$(LIBDIR)/libspitfire.$A $(LIBDIR)/libsoil.$A\
$(LIBDIR)/libclimate.$A $(LIBDIR)/libnum.$A $(LIBDIR)/libtools.$A\
$(LIBDIR)/libcrop.$A $(LIBDIR)/libreservoir.$A $(LIBDIR)/libpnet.$A\
$(LIBDIR)/libsocket.$A $(LIBDIR)/liblpj.$A $(LIBDIR)/libcdf.$A\
$(LIBDIR)/libnitrogen.$A\
$(LIBDIR)/liblpj.$A
EXE = $(BINDIR)/lpjml$E
bin:
$(MAKE) libs
$(MAKE) $(EXE)
libs:
(cd climate && $(MAKE))
(cd numeric && $(MAKE))
(cd lpj && $(MAKE))
(cd soil && $(MAKE))
(cd tree && $(MAKE))
(cd grass && $(MAKE))
(cd crop && $(MAKE))
(cd tools && $(MAKE))
(cd landuse && $(MAKE))
(cd pnet && $(MAKE))
(cd image && $(MAKE))
(cd socket && $(MAKE))
(cd reservoir && $(MAKE))
(cd spitfire && $(MAKE))
(cd netcdf && $(MAKE))
(cd nitrogen && $(MAKE))
clean:
(cd climate && $(MAKE) clean)
(cd lpj && $(MAKE) clean)
(cd numeric && $(MAKE) clean)
(cd soil && $(MAKE) clean)
(cd tree && $(MAKE) clean)
(cd grass && $(MAKE) clean)
(cd crop && $(MAKE) clean)
(cd tools && $(MAKE) clean)
(cd landuse && $(MAKE) clean)
(cd pnet && $(MAKE) clean)
(cd utils && $(MAKE) clean)
(cd image && $(MAKE) clean)
(cd socket && $(MAKE) clean)
(cd reservoir && $(MAKE) clean)
(cd spitfire && $(MAKE) clean)
(cd netcdf && $(MAKE) clean)
(cd nitrogen && $(MAKE) clean)
$(RM) $(RMFLAGS) $(OBJ) updatebuild.$O updatebuild$E getbuild.$O
(cd $(BINDIR) && $(RM) $(RMFLAGS) lpjml$E)
$(OBJ): $(HDRS)
.c.$O:
$(CC) $(CFLAGS) -I$(INC) -c $*.c
$(EXE): $(LPJLIBS) $(OBJ) updatebuild$E
$(CC) $(CFLAGS) -I$(INC) -c getbuild.c
$(LINKMAIN) $(LNOPTS)$(EXE) $(OBJ) $(LPJLIBS) $(LIBS) getbuild.$O
updatebuild$E: updatebuild.$O
$(LINK) $(LNOPTS)updatebuild$E $(LIBS) updatebuild.$O
########################################################################
## ##
## M a k e f i l e ##
## ##
## Makefile for C implementation of climate part of LPJmL 3.4.019 ##
## Creates library ../../lib/libclimate.a ##
## ##
## written by Werner von Bloh ##
## Potsdam Institute for Climate Impact Research ##
## P.O. Box 60 12 03 ##
## 14412 Potsdam/Germany ##
## ##
## Last change: 01.03.2013 ##
## ##
########################################################################
include ../../Makefile.inc
OBJS = initclimate.$O getclimate.$O freeclimate.$O avgtemp.$O\
prdaily.$O getco2.$O storeclimate.$O dailyclimate.$O\
getmtemp.$O initclimate_monthly.$O openclimate.$O\
getmprec.$O checkvalidclimate.$O readco2.$O\
closeclimate.$O
INC = ../../include
LIBDIR = ../../lib
LIB = libclimate.$A
HDRS = $(INC)/buffer.h $(INC)/lpj.h\
$(INC)/climate.h $(INC)/date.h\
$(INC)/pftpar.h $(INC)/types.h $(INC)/header.h\
$(INC)/errmsg.h $(INC)/numeric.h\
$(INC)/conf.h $(INC)/swap.h $(INC)/soilpar.h\
$(INC)/list.h $(INC)/cell.h $(INC)/units.h\
$(INC)/config.h $(INC)/param.h $(INC)/cdf.h
$(LIBDIR)/$(LIB): $(OBJS)
$(AR) $(ARFLAGS)$(LIBDIR)/$(LIB) $(OBJS)
$(OBJS): $(HDRS)
.c.$O:
$(CC) $(CFLAGS) -I$(INC) -c $*.c
clean:
$(RM) $(RMFLAGS) $(OBJS)
(cd $(LIBDIR) && $(RM) $(RMFLAGS) $(LIB))
/***************************************************************************/
/** **/
/** a v g t e m p . c **/
/** **/
/** C implementation of LPJ, derived from the Fortran/C++ version **/
/** **/
/** Function gets annual average temperature **/
/** **/
/** written by Werner von Bloh, Sibyll Schaphoff **/
/** Potsdam Institute for Climate Impact Research **/
/** PO Box 60 12 03 **/
/** 14412 Potsdam/Germany **/
/** **/
/** Last change: 06.01.2009 **/
/** **/
/***************************************************************************/
#include "lpj.h"
Real avgtemp(const Climate *climate,int cell)
{
int i,n;
Real avg;
n=(climate->file_temp.isdaily) ? NDAYYEAR : NMONTH;
avg=0;
for(i=0;i<n;i++)
avg+=climate->data.temp[cell*n+i];
return avg/n;
} /* of 'avgtemp' */
/***************************************************************************/
/** **/
/** c h e c k v a l i d c l i m a t e . c **/
/** **/
/** C implementation of LPJ, derived from the Fortran/C++ version **/
/** **/
/** Function checks NetCDF climate files for missing values and skips **/
/** corresponding LPJ cells **/
/** **/
/** written by Werner von Bloh, Sibyll Schaphoff **/
/** Potsdam Institute for Climate Impact Research **/
/** PO Box 60 12 03 **/
/** 14412 Potsdam/Germany **/
/** **/
/** Last change: 04.02.2013 **/
/** **/
/***************************************************************************/
#include "lpj.h"
static Bool checkvalid(Climatefile *climate, /* climate data file */
Cell grid[], /* LPJ grid */
Config *config /* LPJ configuration */
) /* returns TRUE on error */
{
Bool iserr;
#ifdef USE_MPI
int i,*counts;
#endif
int invalid,invalid_total;
if(climate->fmt==CDF)
{
/* check data for first year */
invalid=checkvalidclimate_netcdf(climate,grid,0,config);
iserr=(invalid==-1);
if(iserror(iserr,config))
return TRUE;
config->count-=invalid;
#ifdef USE_MPI
MPI_Reduce(&invalid,&invalid_total,1,MPI_INT,MPI_SUM,0,config->comm);
counts=newvec(int,config->ntask);
if(counts==NULL)
{
printallocerr("counts");
iserr=TRUE;
}
else
iserr=FALSE;
if(iserror(iserr,config))
return TRUE;
MPI_Allgather(&config->count,1,MPI_INT,counts,1,MPI_INT,
config->comm);
config->offset=0;
for(i=0;i<config->rank;i++)
config->offset+=counts[i];
MPI_Allreduce(&config->count,&config->total,1,MPI_INT,MPI_SUM,
config->comm);
free(counts);
#else
invalid_total=invalid;
config->total=config->count;
#endif
if(config->total==0)
{
if(isroot(*config))
fputs("ERROR207: No cell with valid climate data found.\n",stderr);
return TRUE;
}
if(isroot(*config) && invalid_total)
fprintf(stderr,"WARNING008: No climate data for %d cells, will be skipped.\n",
invalid_total);
}
return FALSE;
} /* of 'checkvalid' */
Bool checkvalidclimate(Climate *climate, /* pointer to climate file */
Cell grid[], /* LPJ grid */
Config *config /* LPJ configuration */
) /* returns TRUE on error */
{
/* check data in temperature file */
if(checkvalid(&climate->file_temp,grid,config))
return TRUE;
return FALSE;
} /* of 'checkvalidclimate' */
/***************************************************************************/
/** **/
/** c l o s e c l i m a t e . c **/
/** **/
/** C implementation of LPJ, derived from the Fortran/C++ version **/
/** **/
/** Function closes open file **/
/** **/
/** written by Werner von Bloh, Sibyll Schaphoff **/
/** Potsdam Institute for Climate Impact Research **/
/** PO Box 60 12 03 **/
/** 14412 Potsdam/Germany **/
/** **/
/** Last change: 19.02.2013 **/
/** **/
/***************************************************************************/
#include "lpj.h"
void closeclimatefile(Climatefile *file, /* pointer to climate data file */
Bool isroot /* task is root task (TRUE/FALSE) */
)
{
if(file->fmt!=FMS)
{
if(file->fmt==CDF)
closeclimate_netcdf(file,isroot);
else
fclose(file->file);
}
} /* of 'closeclimatefile' */
/***************************************************************************/
/** **/
/** d a i l y c l i m a t e . c **/
/** **/
/** C implementation of LPJ, derived from the Fortran/C++ version **/
/** **/
/** Function dailyclimate gets daily value for temperature, **/
/** precipitation, cloudiness and wet days. **/
/** The pointer to the climate data has to be initialized by the **/
/** function getclimate for the specified year. **/
/** **/
/** written by Werner von Bloh, Sibyll Schaphoff **/
/** Potsdam Institute for Climate Impact Research **/
/** PO Box 60 12 03 **/
/** 14412 Potsdam/Germany **/
/** **/
/** Last change: 19.02.2013 **/
/** **/
/***************************************************************************/
#include "lpj.h"
void dailyclimate(Dailyclimate *daily,
const Climate *climate, /* climate data pointer */
Climbuf *climbuf,
int cell, /* cell index */
int day, /* day (1..365) */
int month, /* month (0..11) */
int dayofmonth)
{
Real tamp;
if(climate->file_temp.fmt!=FMS)
{
if(climate->file_temp.isdaily)
{
daily->temp=climate->data.temp[cell*NDAYYEAR+day-1];
climbuf->mtemp+=daily->temp;
}
else
daily->temp=interpolate(getcelltemp(climate,cell),month,dayofmonth);
}
else
climbuf->mtemp+=daily->temp;
if(climate->data.sun!=NULL)
daily->sun=(climate->file_cloud.isdaily) ? climate->data.sun[cell*NDAYYEAR+day-1]
: interpolate(getcellsun(climate,cell),month,dayofmonth);
if(climate->data.lwnet!=NULL)
daily->lwnet=(climate->file_lwnet.isdaily) ? climate->data.lwnet[cell*NDAYYEAR+day-1]
: interpolate(getcelllwnet(climate,cell),month,dayofmonth);
if(climate->data.swdown!=NULL)
daily->swdown=(climate->file_swdown.isdaily) ? climate->data.swdown[cell*NDAYYEAR+day-1]
: interpolate(getcellswdown(climate,cell),month,dayofmonth);
if(climate->data.wind!=NULL)
{
if(climate->file_wind.isdaily)
daily->windspeed=climate->data.wind[cell*NDAYYEAR+day-1];
else
daily->windspeed=interpolate(getcellwind(climate,cell),month,dayofmonth);
}
if(climate->data.tamp!=NULL)
{
#ifdef TMAX_SPITFIRE
daily->tmin=(climate->file_tamp.isdaily) ? climate->data.tamp[cell*NDAYYEAR+day-1] : interpolate(getcelltamp(climate,cell),month,dayofmonth);
daily->tmax=(climate->file_tmax.isdaily) ? climate->data.tmax[cell*NDAYYEAR+day-1] : interpolate(getcelltmax(climate,cell),month,dayofmonth);
#else
tamp=(climate->file_tamp.isdaily) ? climate->data.tamp[cell*NDAYYEAR+day-1] : interpolate(getcelltamp(climate,cell),month,dayofmonth);
daily->tmin=daily->temp-tamp*0.5;
daily->tmax=daily->temp+tamp*0.5;
#endif
}
if(climate->data.lightning!=NULL)
{
if(climate->file_lightning.isdaily)
daily->lightning=climate->data.lightning[cell*NDAYYEAR+day-1];
else
daily->lightning=interpolate(getcelllightning(climate,cell),month,dayofmonth);
}
if(climate->file_prec.fmt!=FMS)
{
if(climate->file_prec.isdaily)
{
daily->prec=climate->data.prec[cell*NDAYYEAR+day-1];
climbuf->mprec+=daily->prec;
}
else
{
daily->prec=(israndomprec(climate)) ? climbuf->dval_prec[dayofmonth+1] :
interpolate(getcellprec(climate,cell),month,dayofmonth)*
ndaymonth1[month];
daily->prec=(daily->prec>0.000001) ? daily->prec : 0.0;
}
}
else
climbuf->mprec+=daily->prec;
} /* of 'dailyclimate' */
/***************************************************************************/
/** **/
/** f r e e c l i m a t e . c **/
/** **/
/** C implementation of LPJ, derived from the Fortran/C++ version **/
/** **/
/** Function closes open files and frees allocated memory **/
/** **/
/** written by Werner von Bloh, Sibyll Schaphoff **/
/** Potsdam Institute for Climate Impact Research **/
/** PO Box 60 12 03 **/
/** 14412 Potsdam/Germany **/
/** **/
/** Last change: 20.09.2011 **/
/** **/
/***************************************************************************/
#include "lpj.h"
void freeclimatedata(Climatedata *data)
{
free(data->tmax);
free(data->prec);
free(data->temp);
free(data->sun);
free(data->lwnet);
free(data->swdown);
free(data->wet);
free(data->wind);
free(data->tamp);
free(data->lightning);
} /* of 'freeclimatedata' */
void freeclimate(Climate *climate,Bool isroot)
{
if(climate!=NULL)
{
closeclimatefile(&climate->file_temp,isroot);
closeclimatefile(&climate->file_prec,isroot);
if(climate->data.tmax!=NULL)
closeclimatefile(&climate->file_tmax,isroot);
if(climate->data.sun!=NULL)
closeclimatefile(&climate->file_cloud,isroot);
if(climate->data.lwnet!=NULL)
closeclimatefile(&climate->file_lwnet,isroot);
if(climate->data.swdown!=NULL)
closeclimatefile(&climate->file_swdown,isroot);
if(climate->data.wind!=NULL)
closeclimatefile(&climate->file_wind,isroot);
if(climate->data.tamp!=NULL)
closeclimatefile(&climate->file_tamp,isroot);
if(climate->data.wet!=NULL)
closeclimatefile(&climate->file_wet,isroot);
#ifdef IMAGE
if(climate->file_temp_var.file!=NULL)
closeclimatefile(&climate->file_temp_var,isroot);
if(climate->file_prec_var.file!=NULL)
closeclimatefile(&climate->file_prec_var,isroot);
#endif
free(climate->co2.data);
freeclimatedata(&climate->data);
}
free(climate);
} /* of 'freeclimate' */
/***************************************************************************/
/** **/
/** g e t c l i m a t e . c **/
/** **/
/** C implementation of LPJ, derived from the Fortran/C++ version **/
/** **/
/** Function getclimate reads monthly climate data (temperature, **/
/** precipitation, cloudiness and wet days) for a specified year. **/
/** The pointer to the climate data has to be initialized by the **/
/** function initclimate. **/
/** **/
/** written by Werner von Bloh, Sibyll Schaphoff **/
/** Potsdam Institute for Climate Impact Research **/
/** PO Box 60 12 03 **/
/** 14412 Potsdam/Germany **/
/** **/
/** Last change: 19.02.2013 **/
/** **/
/***************************************************************************/
#include "lpj.h"
Bool readclimate(Climatefile *file, /* climate data file */
Real data[], /* climate data read */
Real offset, /* offset for data */
Real scale, /* scale factor for data*/
const Cell grid[], /* LPJ grid */
int year, /* year (AD) */
const Config *config /* LPJ configuration */
) /* return TRUE on error */
{
short *vec;
int i,rc;
long long index;
if(file->fmt==FMS)
return FALSE;
index=year-file->firstyear;
if(index<0 || index>=file->nyear)
{
if(isroot(*config))
fprintf(stderr,"ERROR130: Invalid year %d in readclimate().\n",year);
return TRUE;
}
if(file->fmt==CDF)
rc=readclimate_netcdf(file,data,grid,index,config);
else
{
if(fseek(file->file,index*file->size+file->offset,SEEK_SET))
rc=TRUE;
else
{
vec=newvec(short,file->n);
if(vec==NULL)
{
printallocerr("vec");
rc=TRUE;
}
else
{
if(fread(vec,sizeof(short),file->n,file->file)!=file->n)
rc=TRUE;
else
{
if(file->swap)
for(i=0;i<file->n;i++)
data[i]=offset+swapshort(vec[i])*scale;
else
for(i=0;i<file->n;i++)
data[i]=offset+vec[i]*scale;
rc=FALSE;
}
free(vec);
}
}
}
return iserror(rc,config);
} /* of'readclimate' */
Bool getclimate(Climate *climate, /* pointer to climate data */
const Cell grid[], /* LPJ grid */
int year, /* year of climate data to be read */
const Config *config /* LPJ configuration */
) /* returns TRUE on error */
{
Real *wet;
int i,index;
Bool rc;
if(readclimate(&climate->file_temp,climate->data.temp,0,climate->file_temp.scalar,grid,year,config))
{
if(isroot(*config))
fprintf(stderr,"ERROR131: Cannot read temperature of year %d in getclimate().\n",
year);
return TRUE;
}
if(readclimate(&climate->file_prec,climate->data.prec,0,climate->file_prec.scalar,grid,year,config))
{
if(isroot(*config))
fprintf(stderr,"ERROR131: Cannot read precipitation of year %d in getclimate().\n",
year);
return TRUE;
}
if(climate->data.tmax!=NULL)
{
if(readclimate(&climate->file_tmax,climate->data.tmax,0,climate->file_tmax.scalar,grid,year,config))
{
if(isroot(*config))
fprintf(stderr,"ERROR131: Cannot read tmax of year %d in getclimate().\n",
year);
return TRUE;
}
}
if(climate->data.sun!=NULL)
{
if(readclimate(&climate->file_cloud,climate->data.sun,100,-climate->file_cloud.scalar,grid,year,config))
{
if(isroot(*config))
fprintf(stderr,"ERROR131: Cannot read cloudiness of year %d in getclimate().\n",
year);
return TRUE;
}
if(config->cloud_filename.fmt==CDF)
for(i=0;i<climate->file_cloud.n;i++)