/*************************************
*                                    *
*    外枠システム  simlqkyos.c       *
*  （旧共済被保険者数の推計、制度別）*
*                                    *
*************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "set.h"
#include "cntl.h"
#include "kyos.h"
#include "file_open.h"

void simlqkyos(void) {

/*  変数定義  */
  double  TMP      ;
  double  TMQ      ;
  double  TMR      ;
  double  BUPT     ;
  int     XEND     ;
  int     ii       ;
  int     jj       ;



    /*  ファイル名セット＋開く  */
      sprintf(filename, "/rev31/wakuc/data/kyos/kou31-kisor.csv" ) ;
      if ((fp_setakyo = fopen(filename, "r")) == NULL) {
          fprintf(stderr, "旧共済の基礎率データファイルを開けません!\n");
          exit (2);
      }

    /*  ２行カラ読み  */
        karayomi( fp_setakyo );
        karayomi( fp_setakyo );

    for ( nenrei=15; nenrei<=69; nenrei++ ) {
        read_csv( buffer, fp_setakyo, &data_number );
        ii = buffer[0] ; /*  年齢  */
        dattai[4][KIJUNMAP-STARTY][1][ii] = buffer[1] ; 
        dattai[4][KIJUNMAP-STARTY][2][ii] = buffer[2] ;
        jj = buffer[3] ; /*  年齢  */
        bup[4][KIJUNMAP-STARTY][1][jj] = buffer[4] ; 
        bup[4][KIJUNMAP-STARTY][2][jj] = buffer[5] ; 
    }

    /*  ファイルクローズ  */
    fclose( fp_setakyo ) ;

      for ( nendo=KIJUNMAP+1; nendo<=KE; nendo++ ) {
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=84; nenrei++ ) {
            dattai[4][nendo-STARTY][sei][nenrei] = dattai[4][KIJUNMAP-STARTY][sei][nenrei] ;
          }
        }
      }

      for ( sei=1; sei<=2; sei++ ) {
        BUPT = 0.0 ;
        for ( nenrei=15; nenrei<=69; nenrei++ ) {
            BUPT += bup[4][KIJUNMAP-STARTY][sei][nenrei] ;
        }
        for ( nenrei=15; nenrei<=69; nenrei++ ) {
            bup[4][KIJUNMAP-STARTY][sei][nenrei] /= BUPT ;
            for ( nendo=KIJUNMAP+1; nendo<=KE; nendo++ ) {
              bup[4][nendo-STARTY][sei][nenrei] = bup[4][KIJUNMAP-STARTY][sei][nenrei] ;
            }
        }
      }



/*  制度別ループ  */
  for ( seido=4; seido<=7 ; seido++ ) {

      for ( nendo=STARTY+1; nendo<=KIJUNMAP ; nendo++ ) {
        for ( sei=1; sei<=2; sei++ ) {
          for ( nenrei=15; nenrei<=100; nenrei++ ) {
              akyos[seido][nendo-STARTY][sei] += kyosai[seido][nendo-STARTY][sei][nenrei] ;
          }
        }
      }

      for ( nendo=KIJUNMAP+1; nendo<=KE-1; nendo++ ){
        for ( sei=1; sei<=2; sei++ ) {
          TMP = 0.0 ; TMQ = 0.0 ;
          for ( nenrei=15; nenrei<=69; nenrei++ ) {  
            TMP += kou12syu[nendo-STARTY][sei][nenrei] ;
            TMQ += kou12syu[KIJUNMAP-STARTY][sei][nenrei] ;
          }
          if ( TMQ > 0.0 ) {
            akyos[seido][nendo-STARTY][sei] = akyos[seido][KIJUNMAP-STARTY][sei] * TMP / TMQ ;
          }
          else {
            fprintf(fp_err, "旧共済%dの性%dの年度%dの総人数が変です!\n", seido, sei, nendo );
            akyos[seido][nendo-STARTY][sei] = akyos[seido][KIJUNMAP-STARTY][sei] ;
          }
        }
      }



    for ( nendo=KIJUNMAP+1; nendo<=KE; nendo++ ) {
      for ( sei=1; sei<=2; sei++ ) {

      /*  上限の設定  */
        XEND = 70 ;

        TMP = 0 ; TMR = 0 ;
        for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] 
             = kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * exp( -dattai[4][nendo-STARTY][sei][nenrei] ) ;
            TMP += zanzon[seido][nendo-STARTY][sei][nenrei] ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1]
                * ( 1.0 - exp( -dattai[4][nendo-STARTY][sei][nenrei] )) ;
        }
        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            zanzon[seido][nendo-STARTY][sei][nenrei] = 0 ;
            TMR += kyosai[seido][nendo-1-STARTY][sei][nenrei-1] ;
        }

        sinki[seido][nendo-STARTY][sei] = akyos[seido][nendo-STARTY][sei] 
                                 - akyos[seido][nendo-1-STARTY][sei] + TMR ;
        TMQ = sinki[seido][nendo-STARTY][sei] ;



        if ( TMQ < 0.0 ) {
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  * akyos[seido][nendo-STARTY][sei] / TMP ;
            }
        }
        else {
            for ( nenrei=XEND-1; nenrei>=15; nenrei-- ) {
                kyosai[seido][nendo-STARTY][sei][nenrei]
                = zanzon[seido][nendo-STARTY][sei][nenrei] 
                  + sinki[seido][nendo-STARTY][sei] * bup[4][nendo-STARTY][sei][nenrei] ;
            }
        }
        for ( nenrei=XEND; nenrei<=110; nenrei++ ) {
            kyosai[seido][nendo-STARTY][sei][nenrei] = 0.0 ;
        }

        TMP = 0.0 ;
        for ( nenrei=14; nenrei<=XEND; nenrei++ ) {
            TMP += kyosai[seido][nendo-STARTY][sei][nenrei] ;
        }
        if ( nendo > KIJUNMAP ) {
          if (( TMP - akyos[seido][nendo-STARTY][sei] < -2.0 ) ||
              ( TMP - akyos[seido][nendo-STARTY][sei] >  2.0 ) ) {
              fprintf(stderr, "共済%dの性%dの年度%dの人数シミュレーションが変です!\n", seido, sei, nendo );
              fprintf(fp_err, "共済%dの性%dの年度%dの人数シミュレーションが変です!\n", seido, sei, nendo );
              /* exit (2); */
          }
        }
      }
    }

 
    for ( nendo=KIJUNMAP+1; nendo<=KE; nendo++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {
        if ( kou12syu[nendo-STARTY][2][nenrei] > 0.0 ) {
          kyosai[seido][nendo-STARTY][3][nenrei] = kyosai[seido][nendo-STARTY][2][nenrei]
            * kou12syu[nendo-STARTY][3][nenrei] / kou12syu[nendo-STARTY][2][nenrei] ;
          kyosai[seido][nendo-STARTY][4][nenrei] = kyosai[seido][nendo-STARTY][2][nenrei]
            * ( 1.0 - kou12syu[nendo-STARTY][3][nenrei] / kou12syu[nendo-STARTY][2][nenrei] );
        }
        /* 男女計 */
        kyosai[seido][nendo-STARTY][0][nenrei]
          = kyosai[seido][nendo-STARTY][1][nenrei] + kyosai[seido][nendo-STARTY][2][nenrei] ;
      }
    }

  }


  for ( nendo=KIJUNMAP+1; nendo<=KE; nendo++ ) {
    for ( sei=0; sei<=4; sei++ ) {
      for ( nenrei=15; nenrei<=100; nenrei++ ) {

        qkou12syu[nendo-STARTY][sei][nenrei] = kou12syu[nendo-STARTY][sei][nenrei] ;
        qkounen[nendo-STARTY][sei][nenrei] = kounen[nendo-STARTY][sei][nenrei] ;
        for ( seido=4; seido<=6; seido++ ) {
          qkou12syu[nendo-STARTY][sei][nenrei] -= kyosai[seido][nendo-STARTY][sei][nenrei] ;
          qkounen[nendo-STARTY][sei][nenrei] -= kyosai[seido][nendo-STARTY][sei][nenrei] ;
        }
        if ( nendo>=2002 ) {
          qkou12syu[nendo-STARTY][sei][nenrei] -= kyosai[7][nendo-STARTY][sei][nenrei] ;
          qkounen[nendo-STARTY][sei][nenrei] -= kyosai[7][nendo-STARTY][sei][nenrei] ;
        }

      }
    }
  }

}

