#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"

#define DIVIDE 60
#define STEP 20

#define V_MAX 0.8
#define LENGTH 1.0

double get_delta(double *);

void calc(double *current, double *next, int divide){
  int i;
  for(i = 1; i < divide - 1; i++){
    current++;
    next++;
   
    *next = *current + get_delta(current) * V_MAX * (1 - (*current) * LENGTH);
  }
}

void apply_init_condition(double *target, int divide){
  int i;
  for(i = 0; i < divide; i++){
    *target++ = (double)(i < divide / 3 ? 0.1 : i < divide * 2 / 3 ? 0.95 : 0.1);
  }  
}

void apply_boundary_condition(double *target, double *next, int divide){
   *next = *target;
   *(next + divide - 1) = *(target + divide - 1);
}

void display(double *target, int size){
  int i;
  for(i = 0; i < size; i++){
    printf("%lf\n", *target++);
  }
}

int main(){

  //Initialize Target Variable
  int size = sizeof(double) * DIVIDE;
  double *target = (double *)malloc(size);
  double *next = (double *)malloc(size);

  //Set Initial Condition
  apply_init_condition(target, DIVIDE);

  //Apply Scheme
  int i, j;
  for(i = 0; i < STEP; i++){
    apply_boundary_condition(target, next, DIVIDE);
    calc(target, next, DIVIDE);
    memcpy(target, next, size);
  }

  display(next, DIVIDE);

  return 0;
}
