Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pga: development output tracks abandoned and step #32

Merged
merged 2 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
pga: development output tracks abandoned and step
adaptation raster tracks adaptation's return period or staying trapped
  • Loading branch information
petrasovaa committed Apr 23, 2021
commit a577091a86ec88d6c3139cf2b80514bf74a4fd91
12 changes: 11 additions & 1 deletion r.futures/r.futures.simulation/climate.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void initialize_flood_response(struct ACDamageRelation *response_relation)
response_relation->vulnerability_a = -1;
response_relation->vulnerability_b = 0;
}

/*!
* \brief Adapt pixel to flooding.
*
Expand Down Expand Up @@ -77,6 +76,17 @@ bool is_adapted(SEGMENT *adaptation, float flood_probability, int row, int col)
return flood_probability >= (1. / adapted);
}

/*!
* \brief Stay trapped - saves state into adaptation segment
* \param adaptation
* \param row
* \param col
*/
void stay(SEGMENT *adaptation, int row, int col)
{
int stay = 0;
Segment_put(adaptation, (void *)&stay, row, col);
}
/*!
* \brief Converts water depth to structural damage
* using depth-damage-function.
Expand Down
1 change: 1 addition & 0 deletions r.futures/r.futures.simulation/climate.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ enum FloodResponse flood_response(float damage, float adaptive_capacity,
void initialize_flood_response(struct ACDamageRelation *response_relation);
bool is_adapted(SEGMENT *adaptation, float flood_probability, int row, int col);
void adapt(SEGMENT *adaptation, float flood_probability, int row, int col);
void stay(SEGMENT *adaptation, int row, int col);

#endif // FUTURES_CLIMATE_H
15 changes: 15 additions & 0 deletions r.futures/r.futures.simulation/inputs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
#include "map.h"
#include "utils.h"

/*!
* \brief Get developed value depending on step and
* whether abandonment is being tracked
* \param step
* \param abandon
* \return
*/
int get_developed_val_from_step(int step, bool abandon)
{
if (abandon)
return -step - 2;
else
return step + 1;
}

/*!
* \brief Initialize arrays for transformation of probability values
* \param potential_info
Expand Down
7 changes: 2 additions & 5 deletions r.futures/r.futures.simulation/inputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
#include <grass/segment.h>

#include "map.h"

enum development_type {DEV_TYPE_INITIAL = 0,
DEV_TYPE_UNDEVELOPED = -1,
DEV_TYPE_ABANDONED = -2,
DEV_TYPE_TRAPPED = -3};
DEV_TYPE_UNDEVELOPED = -1};

enum DDF_subregions_source {DDF_DEFAULT = 0,
DDF_POTENTIAL = -1,
Expand Down Expand Up @@ -171,7 +168,7 @@ struct FloodInputs
bool depth;
};


int get_developed_val_from_step(int step, bool abandon);
void initialize_incentive(struct Potential *potential_info, float exponent);
void read_input_rasters(struct RasterInputs inputs, struct Segments *segments,
struct SegmentMemory segment_info, map_int_t *region_map,
Expand Down
5 changes: 3 additions & 2 deletions r.futures/r.futures.simulation/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ int main(int argc, char **argv)
if (opt.outputSeries->answer) {
name_step = name_for_step(opt.outputSeries->answer, step, num_steps);
output_developed_step(&segments.developed, name_step,
demand_info.years[step], -1, num_steps, false, false);
demand_info.years[step], -1, num_steps,
segments.use_climate ? false : true);
}
/* export density for that step */
if (opt.outputDensity->answer) {
Expand All @@ -875,7 +876,7 @@ int main(int argc, char **argv)
/* write */
output_developed_step(&segments.developed, opt.output->answer,
demand_info.years[0], demand_info.years[step-1],
num_steps, false, false);
num_steps, segments.use_climate ? false : true);

/* close segments and free memory */
Segment_close(&segments.developed);
Expand Down
54 changes: 23 additions & 31 deletions r.futures/r.futures.simulation/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ char *name_for_step(const char *basename, const int step, const int nsteps)
representing the step when it was developed
*/
void output_developed_step(SEGMENT *developed_segment, const char *name,
int year_from, int year_to, int nsteps, bool undeveloped_as_null, bool developed_as_one)
int year_from, int year_to, int nsteps, bool output_undeveloped)
{
int out_fd;
int row, col, rows, cols;
Expand All @@ -103,12 +103,14 @@ void output_developed_step(SEGMENT *developed_segment, const char *name,
if (Rast_is_c_null_value(&developed)) {
continue;
}
/* this handles undeveloped cells */
if (undeveloped_as_null && developed == DEV_TYPE_UNDEVELOPED)
continue;
/* this handles developed cells */
if (developed_as_one)
developed = 1;
if (!output_undeveloped) {
if (developed < DEV_TYPE_UNDEVELOPED) {
developed += 1;
}
else if (developed == DEV_TYPE_UNDEVELOPED) {
continue;
}
}
out_row[col] = developed;
}
Rast_put_c_row(out_fd, out_row);
Expand All @@ -119,36 +121,26 @@ void output_developed_step(SEGMENT *developed_segment, const char *name,
Rast_init_colors(&colors);
// TODO: the map max is 36 for 36 steps, it is correct?

if (!undeveloped_as_null) {
val1 = DEV_TYPE_TRAPPED;
val2 = DEV_TYPE_TRAPPED;
Rast_add_c_color_rule(&val1, 220, 150, 250, &val2, 220, 150, 250,
&colors);
val1 = DEV_TYPE_ABANDONED;
val2 = DEV_TYPE_ABANDONED;
Rast_add_c_color_rule(&val1, 120, 155, 200, &val2, 120, 155, 200,
if (!output_undeveloped) {
val1 = -nsteps;
val2 = -1;
Rast_add_c_color_rule(&val1, 230, 163, 255, &val2, 60, 0, 80,
&colors);
}
else {
val1 = DEV_TYPE_UNDEVELOPED;
val2 = DEV_TYPE_UNDEVELOPED;
Rast_add_c_color_rule(&val1, 180, 255, 160, &val2, 180, 255, 160,
&colors);
}
if (developed_as_one) {
val1 = 1;
val2 = 1;
Rast_add_c_color_rule(&val1, 255, 100, 50, &val2, 255, 100, 50,
&colors);
}
else {
val1 = 0;
val2 = 0;
Rast_add_c_color_rule(&val1, 200, 200, 200, &val2, 200, 200, 200,
&colors);
val1 = 1;
val2 = nsteps;
Rast_add_c_color_rule(&val1, 255, 100, 50, &val2, 255, 255, 0,
&colors);
}
val1 = 0;
val2 = 0;
Rast_add_c_color_rule(&val1, 200, 200, 200, &val2, 200, 200, 200,
&colors);
val1 = 1;
val2 = nsteps;
Rast_add_c_color_rule(&val1, 255, 100, 50, &val2, 255, 255, 0,
&colors);

mapset = G_find_file2("cell", name, "");

Expand Down
2 changes: 1 addition & 1 deletion r.futures/r.futures.simulation/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

char *name_for_step(const char *basename, const int step, const int nsteps);
void output_developed_step(SEGMENT *developed_segment, const char *name, int year_from, int year_to,
int nsteps, bool undeveloped_as_null, bool developed_as_one);
int nsteps, bool output_undeveloped);
void output_step(SEGMENT *output_segment, SEGMENT *developed_segment,
const char *name, RASTER_MAP_TYPE data_type);
#endif // FUTURES_OUTPUT_H
7 changes: 3 additions & 4 deletions r.futures/r.futures.simulation/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ bool can_develop(CELL development, enum patch_type type, int step, int lag)
}
else if (type == PATCH_TYPE_REDEVELOP) {
if (development == DEV_TYPE_INITIAL
|| development == DEV_TYPE_TRAPPED
|| (development < (step + 1) - lag))
return true;
}
else if (type == PATCH_TYPE_ABANDON) {
if (development >= DEV_TYPE_INITIAL || development == DEV_TYPE_TRAPPED)
if (development >= DEV_TYPE_INITIAL)
return true;
}
return false;
Expand Down Expand Up @@ -333,9 +332,9 @@ int grow_patch(int seed_row, int seed_col, int patch_size, int step, int region,
found_in_this_region = 1;
if (type == PATCH_TYPE_NEW || type == PATCH_TYPE_REDEVELOP)
/* e.g. first step=0 will be saved as 1 */
patch_develop_value = step + 1;
patch_develop_value = get_developed_val_from_step(step, false);
else
patch_develop_value = DEV_TYPE_ABANDONED;
patch_develop_value = get_developed_val_from_step(step, true);

/* set seed as developed */
Segment_put(&segments->developed, (void *)&patch_develop_value, seed_row, seed_col);
Expand Down
6 changes: 4 additions & 2 deletions r.futures/r.futures.simulation/simulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,11 @@ void climate_step(struct Segments *segments, struct Demand *demand,
// TODO: get damage only for developed
damage = get_damage(segments, ddf, flood_probability, depth, row, col);
if (damage > 0) {
if (developed_value >= 0 || developed_value == DEV_TYPE_TRAPPED) {
if (developed_value >= DEV_TYPE_INITIAL) {
Segment_get(&segments->adaptive_capacity, (void *)&ac, row, col);
response = flood_response(damage, ac, response_relation);
if (response == Retreat) {
developed_value = DEV_TYPE_ABANDONED;
developed_value = get_developed_val_from_step(step, true);
Segment_put(&segments->developed, (void *)&developed_value, row, col);
/* redistribute */
Segment_get(&segments->subregions, (void *)&region_from_idx, row, col);
Expand All @@ -514,6 +514,8 @@ void climate_step(struct Segments *segments, struct Demand *demand,
else if (response == Adapt) {
adapt(&segments->adaptation, flood_probability, row, col);
}
else
stay(&segments->adaptation, row, col);
}
// decrease potential
}
Expand Down