fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. #define MAXYR 200
  7. #define MAXEST 100
  8. #define MSIZE (MAXEST * (MAXEST + 1) / 2)
  9.  
  10. #define MAXSIM 600000
  11. #define MAXSTP 500
  12. #define CLCPAR "CLC.PAR"
  13. #define CLCDAT "CLC.DAT"
  14. #define CLCOUT "CLC.OUT"
  15.  
  16. void RDPARS(void);
  17. void PHOUT(double *CL, double RAWCL, int ILAST, int IY);
  18. double CLIMIT(double *CATCH, double *SIGHT, double *FMATRX, int *ISYR, int *IZYR,
  19. double *ZMULT,double *POP, double *G);
  20. double CONTRL(double D, double Y, double PLEVEL, double PSLOPE);
  21. void DEVIAN(double *SS0, double *SS1, double *SS2, double *SS3, double *SIGHT, double *FMATRX,
  22. int *ISYR, int *IZYR, double *ZMULT, double *POP, double *G);
  23. double STKSIM(double RK, double R, double *POP, double *CATCH);
  24. void SORT(double *ARRAY, double *ARRAY2, int N);
  25. int _getline(char *buff, size_t size, FILE *fp);
  26.  
  27. /* from Fortran COMMON VARIABLE */
  28. struct MANPAR {
  29. double PPROB; /* Probability level */
  30. double PYMAX; /* Maximum value of the productivity parameter (Y) for integration */
  31. double PNYSTP; /* Number of step in integration over Y */
  32. double PKSTEP; /* Maximum relative step size in integration over K */
  33. double PDSTEP; /* Target step size for integration over depletion */
  34. double PNBSTP; /* Number of steps for integration over the bias parameter */
  35. double PBMIN; /* Minimum multiplicative bias eg 0.5 means 50% downward bias */
  36. double PBMAX; /* Maximum multiplicative bias eg 1.5 means 50% upward bias */
  37. double PSCALE; /* Raw deviance scaling factor S = 1/PSCALE**2 */
  38. double PHASET; /* Number of years without surveys before phaseout invoked */
  39. double PHASEP; /* Phaseout: anual reduction proportion */
  40. double PCYCLE; /* Maximum number of years before a new CLC = number of years a CLC is valid */
  41. double PLEVEL; /* Internal protection level */
  42. double PSLOPE; /* 'Slope' of catch control law */
  43. } mp;
  44.  
  45. /* All years are scaled so 0 = a year prior to the 1st input data */
  46. struct MANDAT {
  47. int ISTART; /* Year of first input data (catch or abundance data) */
  48. int IYEAR; /* Year for which to set first catch limit */
  49. int NS; /* Number of non-zero estimates */
  50. int NZ; /* Number of zero estimates */
  51. double RKLO; /* Lower bound used in integration over K */
  52. double RKHI; /* Upper bound used in integration over K */
  53. } md;
  54.  
  55. int main(void)
  56. {
  57. /* ***** LOCAL VARIABLES */
  58. /*
  59. CHARACTER STOCK*30, FORMT*50
  60. INTEGER IY, INITYR, I, IS, IYRCL, ILAST, N, IN, IOUT
  61. */
  62. double CATCH[MAXYR + 1], /* Catch array, indexed by year */
  63. SIGHT[MAXEST + 1], /* Abundance estimate array, indexed by estimate number */
  64. FMATRX[MSIZE + 1], /* Information matrix (H) of the log sightings estimates */
  65. /* (excludeing zero estimates, stored as a lower triangle */
  66. ZMULT[MAXEST + 1], /* Poisson multiplier for Nth zero estimate */
  67. POP[MAXYR + 1], /* Modelled population size in year I (set in STKSIM) */
  68. G[MAXYR + 1], /* set & used in DEVIAN */
  69. RAWCL, /* Nominal catch limit i.e. output from the Catch Limit Algorithm */
  70. CL; /* The catch limit for the area considerd */
  71. int ISYR[MAXEST + 1], /* Year of Nth abundance estimate SIGHT(N). N=0, NS-1 */
  72. IZYR[MAXEST + 1], /* Year of Nth zero estimate. N=0, NZ-1 */
  73. POUT; /* Parameter determing whether phaseout may be applied if */
  74. /* necessary. Test for phaseout if POUT=1. (Phaseout is not */
  75. /* applied to Medium of Large area nominal catch limits) */
  76. int INITYR, /* Year in which first premanagement catch taken */
  77. ILAST, /* Year of the most recent abundance estimate */
  78. IY, I, IS, IYRCL, N, J, N1;
  79. double C, TOTALC;
  80. int numlen, len, ret = 0;
  81. char /* STOCK[30], FORMT[50],*/ buff[300];
  82.  
  83. /* ***** Read in the data required for application of the CLA */
  84.  
  85. /* DATA IN/2/, IOUT/3/ */
  86. FILE *IN = NULL, *IOUT = NULL;
  87. /* OPEN (IN, FILE = 'CLC.DAT') */
  88. if ((IN = fopen(CLCDAT, "rt")) == NULL) {
  89. fprintf(stderr, "Can't open " CLCDAT "\n");
  90. ret = 1;
  91. goto exit;
  92. }
  93. /* OPEN (IOUT, FILE = 'CLC.OUT') */
  94. if ((IOUT = fopen(CLCOUT, "w")) == NULL) {
  95. fprintf(stderr, "Can't open " CLCOUT "\n");
  96. ret = 1;
  97. goto exit;
  98. }
  99.  
  100. RDPARS();
  101.  
  102. /* READ (IN, '(A30/)') STOCK */
  103. _getline(buff, sizeof(buff), IN);
  104. /* WRITE (IOUT, '(A30/)') STOCK */
  105. fprintf(IOUT, "%s by C\n\n", buff);
  106. fprintf(stdout, "%s by C\n", buff);
  107. _getline(buff, sizeof(buff), IN); /* Discard empty lines */
  108.  
  109. /* Read the year of the first catch, the first year for which catch */
  110. /* limits are to be set & the phaseout option */
  111.  
  112. /* READ (IN, '(T30,I4)') INITYR */
  113. _getline(buff, sizeof(buff), IN);
  114. numlen = 4; len = strlen(buff);
  115. INITYR = atoi(&buff[len-numlen]);
  116.  
  117. /* WRITE (IOUT, '(A,T30,I4)') 'Year of first input data', INITYR */
  118. fprintf(IOUT, "%.*s %4d\n", len - numlen, buff, INITYR);
  119. fprintf(stdout, "%.*s %4d\n", len - numlen, buff, INITYR);
  120.  
  121. /* READ (IN, '(T30,I4)') IYRCL */
  122. _getline(buff, sizeof(buff), IN);
  123. numlen = 4; len = strlen(buff);
  124. IYRCL = atoi(&buff[len-numlen]);
  125.  
  126. /* WRITE (IOUT, '(A, T30, I4)') 'Year of first catch limit', IYRCL */
  127. fprintf(IOUT, "%.*s %4d\n", len - numlen, buff, IYRCL);
  128. fprintf(stdout, "%.*s %4d\n", len - numlen, buff, IYRCL);
  129.  
  130. /* READ (IN, '(T30, I4)') POUT */
  131. _getline(buff, sizeof(buff), IN);
  132. numlen = 4; len = strlen(buff);
  133. POUT = atoi(&buff[len-numlen]);
  134. if (POUT == 1) {
  135. /* WRITE (IOUT, '(A, T30,A4)') 'Apply phaseout if necessary', 'YES' */
  136. fprintf(IOUT, "Apply phaseout if necessary YES\n");
  137. fprintf(stdout, "Apply phaseout if necessary YES\n");
  138. } else {
  139. /* WRITE (IOUT, '(A, T30,A4)') 'Apply phaseout', 'No' */
  140. fprintf(IOUT, "Apply phaseout No\n");
  141. fprintf(stdout, "Apply phaseout No\n");
  142. }
  143.  
  144. /* Re-scale IYRCL such that 0 is the year prior to the first input data */
  145.  
  146. md.ISTART = 0;
  147. md.IYEAR = IYRCL - INITYR;
  148. /* IF (IYEAR .LE. 0 .OR. IYEAR .GT. MAXYR) STOP 'INVALID YEAR' */
  149. if ((md.IYEAR <= 0) || (md.IYEAR > MAXYR)) {
  150. fprintf(stderr, "INVALID YEAR");
  151. ret = 1;
  152. goto exit;
  153. }
  154.  
  155. /* Initialize the catch array */
  156.  
  157. /* DO 10 I = 0, MAXYR */
  158. for (I = 0; I <= MAXYR; I++) {
  159. CATCH[I] = 0.0;
  160. } /* 10 CONTINUE */
  161.  
  162. /* Read in the catch data, scaling each year to the initial year */
  163. /* READ (IN, '(// A)') FORMT (I4,F8.0) */
  164. _getline(buff, sizeof(buff), IN); /* Discard empty lines */
  165. _getline(buff, sizeof(buff), IN); /* Discard empty lines */
  166. _getline(buff, sizeof(buff), IN); /* Discard empty lines */
  167.  
  168. /* WRITE (IOUT, '(/A)') 'Historic catches:' */
  169. fprintf(IOUT, "\nHistoric catches:\n");
  170. fprintf(stdout, "\nHistoric catches:\n");
  171. TOTALC = 0;
  172. /* DO 20 I = 0, MAXYR */
  173. for (I = 0; I <= MAXYR; I++) {
  174. /* READ (IN, FORMT) IY, C */
  175. _getline(buff, sizeof(buff), IN);
  176. sscanf(buff,"%4d%7lf", &IY, &C);
  177. /* IF (IY .LT. 0) GO TO 25 */
  178. if (IY < 0) break; /* EOD */
  179.  
  180. /* WRITE (IOUT, FORMT) IY, C */
  181. fprintf(stdout, "%4d%6.f\n", IY, C);
  182. fprintf(IOUT, "%4d%6.f\n", IY, C);
  183. IY = IY - INITYR;
  184. /* IF (IY .LT. 0 .OR. IY .GE. IYEAR) STOP */
  185. if (IY < 0 || IY > md.IYEAR) {
  186. fprintf(stderr, " ERROR: IY is out of range(%d)\n", IY);
  187. ret = 1;
  188. goto exit;
  189. }
  190. CATCH[IY] = C;
  191. TOTALC = TOTALC + C;
  192. } /* 20 CONTINUE */
  193.  
  194. /* 25 IF (TOTALC .LT. 0) STOP ' ERROR: No historic catch input' */
  195. if (TOTALC < 0) {
  196. fprintf(stderr, " ERROR: No historic catch input\n");
  197. ret = 1;
  198. goto exit;
  199. }
  200.  
  201. /* Read in the non-zero sightings estimates and information matrix */
  202.  
  203. /* READ (IN, '(//T30, I4 / A)') NS, FORMT (I4,F10.0,10F8.0) */
  204. _getline(buff, sizeof(buff), IN); /* Discard empty lines */
  205. _getline(buff, sizeof(buff), IN); /* Discard empty lines */
  206. _getline(buff, sizeof(buff), IN);
  207. numlen = 4; len = strlen(buff);
  208. md.NS = atoi(&buff[len-numlen]);
  209.  
  210. /* WRITE (IOUT, '(/A)') 'Abundance estimates:' */
  211. fprintf(IOUT, "\nAbundance estimates:\n");
  212. fprintf(stdout, "\nAbundance estimates:\n");
  213. /* IF (NS .GT. MAXEST) STOP 'ERROR: Abundance year out of range' */
  214. if (md.NS > MAXEST) {
  215. fprintf(stderr, "ERROR: Abundance year out of range\n");
  216. ret = 1;
  217. goto exit;
  218. }
  219. _getline(buff, sizeof(buff), IN);
  220. /* DO 30 N=0, NS-1 */
  221. for (N = 0; N <= md.NS - 1; N++) {
  222. N1 = (N * (N + 1)) / 2;
  223. /* READ (IN, FORMT) ISYR(N), SIGHT(N), (FMATRX(N1 + J), J = 0, N) */
  224. _getline(buff, sizeof(buff), IN);
  225. sscanf(buff, "%4d%10lf", &ISYR[N], &SIGHT[N]);
  226. fprintf(IOUT, "%4d %8.f", ISYR[N], SIGHT[N]);
  227. fprintf(stdout, "%4d %8.f", ISYR[N], SIGHT[N]);
  228. len = strlen(buff);
  229. for (J = 0; J < N + 1; J++) {
  230. if (len > J * 7 + 14) {
  231. sscanf(&buff[0] + J * 7 + 14,"%7lf", &FMATRX[N1 + J]);
  232. } else {
  233. FMATRX[N1 + J] = 0.0;
  234. }
  235. fprintf(IOUT, "%7.f", FMATRX[N1 + J]);
  236. fprintf(stdout, "%7.f", FMATRX[N1 + J]);
  237. }
  238. fprintf(IOUT, "\n");
  239. fprintf(stdout, "\n");
  240. /* WRITE (IOUT, FORMT) ISYR(N), SIGHT(N), (FMATRX (N1 + J), J = 0, N) */
  241. ISYR[N] = ISYR[N] - INITYR;
  242. if (ISYR[N] < 0 || ISYR[N] >= md.IYEAR) {
  243. /*STOP 'ERROR: Sight year out of range'*/
  244. fprintf(stderr, "ERROR: Sight year out of range\n");
  245. ret = 1;
  246. goto exit;
  247. }
  248. if (SIGHT[N] <= 0.0) {
  249. /* STOP ' ERROR: Estimate not positive' */
  250. fprintf(stderr, "ERROR: Estimate not positive\n");
  251. ret = 1;
  252. goto exit;
  253. }
  254. } /* 30 CONTINUE */
  255.  
  256. /* Read in the Poisson multipliers for any zero sightings estimates */
  257.  
  258. /* READ (IN, '(/T30, I3, /A)') NZ, FORMAT */
  259. _getline(buff, sizeof(buff), IN);
  260. _getline(buff, sizeof(buff), IN);
  261. numlen = 3; len = strlen(buff);
  262. md.NZ = atoi(&buff[len-numlen]);
  263. if (md.NZ > 0) {
  264. /* WRITE (IOUT, '(/A)') 'Zero abundance estimates:' */
  265. fprintf(IOUT, "\nZero abundance estimates:\n");
  266. fprintf(stdout, "\nZero abundance estimates:\n");
  267. }
  268. if (md.NZ > MAXEST) {
  269. /* STOP ' ERROR: Zero estimate array too small' */
  270. fprintf(stderr, "ERROR: Zero estimate array too small\n");
  271. ret = 1;
  272. goto exit;
  273. }
  274. _getline(buff, sizeof(buff), IN);
  275. /* DO 40 N = 0, NZ - 1 */
  276. for (N = 0; N <= md.NZ - 1; N++) {
  277. N1 = (N * (N + 1)) / 2;
  278. /*READ (IN, FORMT) IZYR(N), ZMULT(N)*/
  279. _getline(buff, sizeof(buff), IN);
  280. sscanf(buff, "%4d%10lf", &IZYR[N], &ZMULT[N]);
  281. /* WRITE (IOUT, FORMT) IZYR(N), ZMULT(N) */
  282. fprintf(IOUT, "%4d%7.f\n", IZYR[N], ZMULT[N]);
  283. fprintf(stdout, "%4d%7.f\n", IZYR[N], ZMULT[N]);
  284. IZYR[N] = IZYR[N] - INITYR;
  285. if (IZYR[N] < 0 || IZYR[N] >= md.IYEAR) {
  286. /* STOP ' Sight year out of range' */
  287. fprintf(stderr, "Sight year out of range\n");
  288. ret = 1;
  289. goto exit;
  290. }
  291.  
  292. if (ZMULT[N] <= 0.0) {
  293. /* STOP ' ERROR: Multiplier not positive' */
  294. fprintf(stderr, "ERROR: Multiplier not positive\n");
  295. ret = 1;
  296. goto exit;
  297. }
  298. } /* 40 CONTINUE */
  299. /*WRITE (IOUT, '()')*/
  300. fprintf(IOUT,"\n");
  301. fprintf(stdout,"\n");
  302. /* Bound the range for the integration over K */
  303.  
  304. md.RKHI = 1.0E7;
  305. md.RKLO = 0.0;
  306. IS = 0; /* orignal not initialized bug? */
  307. /* ****** */
  308. /* ****** Run the CLA to obtain the nominal catch limit */
  309. /* ****** */
  310. RAWCL = CLIMIT(CATCH, SIGHT, FMATRX, ISYR, IZYR, ZMULT, POP, G);
  311. /* Set the catch limits for PCYCLE years. If the catch limit may be */
  312. /* subject to phaseout, call POUT to apply the phaseout rule. */
  313.  
  314. /* First set ILAST = year of the most recent abundance estimate */
  315.  
  316. if (md.NS > 0) ILAST = ISYR[md.NS - 1];
  317. /* IF (NZ .GT. 0) ILAST = MAX(IS, IZYR(md.NZ - 1)) */
  318. if (md.NZ > 0) ILAST = (IS > IZYR[md.NZ - 1])? IS: IZYR[md.NZ - 1];
  319.  
  320. /* DO 100 IY = IYEAR, IYEAR + mp.PCYCLE - f1 */
  321. for (IY = md.IYEAR; IY <= md.IYEAR + mp.PCYCLE - 1; IY++) {
  322. if (POUT == 1) {
  323. PHOUT(&CL, RAWCL, ILAST, IY);
  324. } else {
  325. CL = RAWCL;
  326. }
  327. /* WRITE (IOUT, '(A6, I5, A17, I6)') 'Year:', IY + INITYR,'Catch limit:', NINT(CL) */
  328. fprintf(IOUT, "Year: %4d Catch limit:%6d\n", IY + INITYR, (int)(CL + 0.5));
  329. fprintf(stdout, "Year: %4d Catch limit:%6d\n", IY + INITYR, (int)(CL + 0.5));
  330.  
  331. } /* 100 CONTINUE */
  332.  
  333. if (IN != NULL) fclose(IN);
  334. if (IOUT != NULL) fclose(IOUT);
  335. return ret;
  336. /* STOP */
  337. /* END */
  338. }
  339.  
  340. /*
  341. C **************************************************************************************
  342. C
  343. C CLC version 6
  344. C
  345. C **************************************************************************************
  346. C
  347. C 31 January 1994
  348. C
  349. C **************************************************************************************
  350. */
  351. /*
  352. SUBROUTINE RDPARS
  353. Read the file of input parameters
  354. */
  355. void RDPARS(void)
  356. {
  357. double *p[] = {
  358. &mp.PPROB, &mp.PYMAX, &mp.PNYSTP,&mp.PKSTEP, &mp.PDSTEP, &mp.PBMIN, &mp.PBMAX,
  359. &mp.PNBSTP, &mp.PSCALE, &mp.PHASET, &mp.PHASEP, &mp.PCYCLE, &mp.PLEVEL, &mp.PSLOPE
  360. };
  361. FILE *IFILE;
  362. char buff[80];
  363. int count;
  364.  
  365. /* Open the input file (only read once) */
  366. IFILE = fopen(CLCPAR, "rt");
  367. if (IFILE == NULL) {
  368. fprintf(stderr,"Can't open " CLCPAR "\n");
  369. exit(1);
  370. }
  371. count = 0;
  372. while (_getline(buff, sizeof(buff), IFILE) != 0) {
  373. /* READ (IFILE, '(T30, F10.0)') PPROB, PYMAX, PNYSTP, PKSTEP, PDSTEP, PBMIN, PBMAX,
  374. PNBSTP, PSCALE, PHASET, PHASEP, PCYCLE, PLEVEL, PSLOPE */
  375. sscanf(buff,"%*28c%lf", p[count]);
  376. count++;
  377. }
  378. /* CLOSE (IFILE) */
  379. fclose(IFILE);
  380. return;
  381. /* RETURN
  382. END */
  383. }
  384.  
  385. /*
  386. SUBROUTINE PHOUT (CL, RAWCL, ILAST, IY)
  387. */
  388. void PHOUT(double *CL, double RAWCL, int ILAST, int IY)
  389. {
  390. /*
  391. PHASET Number of years without surveys before phaseout invoked
  392. PHASEP Phaseout annual reduction proportion
  393. Phaseout: Reduce catch limit if there is no survey data in the
  394. last PHASET years
  395. */
  396. if (IY >= ILAST + mp.PHASET) {
  397. *CL = RAWCL * (1.0 - mp.PHASEP * (IY - ILAST - mp.PHASET));
  398. if (*CL < 0.0) *CL = 0.0;
  399. } else {
  400. *CL = RAWCL;
  401. }
  402. /* RETURN
  403. END */
  404. }
  405.  
  406. /*
  407. REAL FUNCTION CLIMIT (CATCH,SIGHT,FMATRX,ISYR,IZYR,ZMULT,POP,G)
  408. Run the CLA to obtain to nominal catch limit
  409. */
  410. double CLIMIT(double *CATCH, double *SIGHT, double *FMATRX, int *ISYR, int *IZYR, double *ZMULT,double *POP, double *G)
  411. {
  412. /*
  413. Local variables:
  414.  
  415. REAL PRES(0:MAXSIM), QRES(0:MAXSIM), SS0, SS1, SS2, SS3
  416. REAL SF, Y(0:MAXSTP), B(0:MAXSTP), RLGB(0:MAXSTP)
  417. INTEGER NB, NR
  418. NB Number of bias steps
  419. NR Number of steps for Y (the productivity parameter)
  420. SF Deviance scale factor (SF = .5 / PSCALE**2)
  421. */
  422. double SF, SS0, SS1, SS2, SS3;
  423. double Y[MAXSTP + 1], B[MAXSTP + 1], RLGB[MAXSTP + 1];
  424. double *PRES = NULL, *QRES = NULL;
  425.  
  426. int NB, NR;
  427. double BINC, YINC, DK, R, RK, D, DD, P, Q, PTOT;
  428. int I, J, N, N2;
  429.  
  430. double _CLIMIT = 0.0;
  431. if (md.NS <= 0) return _CLIMIT;
  432.  
  433. PRES = (double *) malloc(sizeof(double) * (MAXSIM + 1));
  434. if (PRES == NULL) {
  435. fprintf(stderr, "Can't Allocate memory (PRES)\n");
  436. goto err_exit;
  437. }
  438. memset(PRES, 0, sizeof(double) * (MAXSTP + 1));
  439.  
  440. QRES = (double *) malloc(sizeof(double) * (MAXSIM + 1));
  441. if (QRES == NULL) {
  442. fprintf(stderr, "Can't Allocate memory (QRES)\n");
  443. goto err_exit;
  444. }
  445. memset(QRES, 0, sizeof(double) * (MAXSTP + 1));
  446.  
  447. /* Set deviance scale factor S = 1/PSCALE**2 */
  448. SF = 0.5 / (mp.PSCALE * mp.PSCALE);
  449.  
  450. /* Check the sizes of the Y and B arrays are large enough */
  451. if (mp.PNBSTP > MAXSTP || mp.PNYSTP > MAXSTP) {
  452. fprintf(stderr, "Y &/or b array sizes too small\n");
  453. goto err_exit;
  454. }
  455. /* Set sightings bias step sizes & their log values. BINC = Bias increment */
  456. NB = (int)mp.PNBSTP;
  457. BINC = (mp.PBMAX - mp.PBMIN) / NB;
  458. /* DO 10 I = 0, NB - 1 */
  459. for (I = 0; I <= NB - 1; I++) {
  460. *(B + I) = mp.PBMIN + (I + 0.5) * BINC;
  461. *(RLGB + I) = -log(*(B + I));
  462. } /* 10 CONTINUE */
  463.  
  464. /* Set productivity parameter step sizes (midpoints) */
  465. NR = (int)mp.PNYSTP;
  466. YINC = mp.PYMAX / NR;
  467. /* DO 20 I = 0, NR - 1 */
  468. for (I = 0; I <= NR - 1; I++) {
  469. *(Y + I) = (I + 0.5) * YINC;
  470. }/* 20 CONTINUE */
  471.  
  472. PTOT = 0;
  473. N = 0;
  474. /* DO 50 I = 0, NR - 1 */
  475. for (I = 0; I <= NR - 1; I++) {
  476. /* Set R from the productivity parameter, Y */
  477. R = 1.4184 * *(Y + I);
  478.  
  479. /* Step size for K */
  480. DK = mp.PKSTEP;
  481. D = 1.0;
  482. RK = md.RKHI;
  483.  
  484. /* Use function STKSIM to set up the Nth population trajectory */
  485. /* i.e. set up the pop array */
  486.  
  487. while (1) {
  488. /* 30 IF (RK .LE. RKLO .OR. STKSIM (RK, R, POP, CATCH) .LE. 0.) GOTO 40 */
  489. if ((RK <= md.RKLO) || (STKSIM(RK, R, POP, CATCH) <= 0.0)) break;
  490. /* IF (N .GE. MAXSIM) STOP 'ERROR: TOO MANY SIMULATIONS' */
  491. if (N >= MAXSIM) {
  492. fprintf(stderr, "ERROR: TOO MANY SIMULATIONS");
  493. goto err_exit;
  494. }
  495. /* How much depletion covered? */
  496. DD = D - POP[md.IYEAR] / RK;
  497. D = POP[md.IYEAR] / RK;
  498. P = 0.0;
  499.  
  500. if (DD > 0.0) {
  501. /* Compute the internal catch limit corresponding to D and Y(I) */
  502. QRES[N] = CONTRL(D, Y[I], mp.PLEVEL, mp.PSLOPE) * POP[md.IYEAR];
  503.  
  504. /* Calculate deviance */
  505. DEVIAN(&SS0, &SS1, &SS2, &SS3, SIGHT, FMATRX, ISYR,IZYR, ZMULT, POP, G);
  506.  
  507. /* Scale likelihood and integrate over values for the bias parameter */
  508. /* DO 35 J = 0, NB - 1 */
  509. for (J = 0; J <= NB - 1; J++) {
  510. P = P + exp(-SF * (SS0 + *(RLGB + J) * (SS1 + *(RLGB + J) * SS2) + SS3 * *(B + J)));
  511. } /* 35 CONTINUE */
  512.  
  513. /* Calculate the weight for this point (& total area under likelihood) */
  514. PRES[N] = P * DD;
  515. PTOT = PTOT + PRES[N];
  516.  
  517. /* Update counter */
  518. N = N + 1;
  519.  
  520. /* Find the next K */
  521. DK = DK * mp.PDSTEP / DD;
  522. /* IF (DK .GT. PKSTEP) DK = PKSTEP */
  523. if (DK > mp.PKSTEP) DK = mp.PKSTEP;
  524.  
  525. } else {
  526. /* IF DD = 0 change the step size only */
  527. DK = mp.PKSTEP;
  528. }
  529. /* Set the new value of K */
  530. RK = RK / (1.0 + DK);
  531. } /* GOTO 30 */ /* 40 CONTINUE */
  532. } /* 50 CONTINUE */
  533. /* IF (PTOT .LE. 0.) STOP 'ERROR: PROB INTEGRATES TO ZERO' */
  534. if (PTOT < 0.0) {
  535. fprintf(stderr,"ERROR: PROB INTEGRATES TO ZERO");
  536. goto err_exit;
  537. }
  538.  
  539. /* Sort the QRES and PRES arrays in ascending order of QRES */
  540. N2 = N;
  541. SORT(QRES, PRES, N2);
  542.  
  543. /* Normalize the relative likelihoods */
  544. /* DO 60 I = 0, N - 1 */
  545. for (I = 0; I <= N - 1; I++) {
  546. PRES[I] = PRES[I] / PTOT;
  547. } /* 60 CONTINUE */
  548.  
  549. /* Extract the desired probability level: the nominal catch limit (NCL) */
  550. /* is the lower PROB% of the distribution. */
  551. /* First calculate PRES(I), the probability that the NCL is between */
  552. /* QRES(I) & QRES(I + 1). */
  553.  
  554. P = 0;
  555. /* DO 70 I = 0, N - 1 */
  556. for (I = 0; I <= N - 1; I++) {
  557. P = P + PRES[I];
  558. /* IF (P .GT. PPROB) GOTO 80 */
  559. if (P > mp.PPROB) break;
  560. } /* 70 CONTINUE */
  561.  
  562. /* Interpolate to set the nominal catch limit */
  563. /* 80 IF (I .GE. N - 1) THEN */
  564. if (I >= N - 1) {
  565. Q = QRES[N - 1];
  566. } else {
  567. Q = (QRES[I + 1] * (mp.PPROB - P + PRES[I]) + QRES[I] * (P - mp.PPROB)) / PRES[I];
  568. }
  569.  
  570. _CLIMIT = Q;
  571.  
  572. if (QRES != NULL) free(QRES);
  573. if (PRES != NULL) free(PRES);
  574. /* if (Y != NULL) free(Y);
  575. if (B != NULL) free(B);
  576. if (RLGB != NULL) free(RLGB);*/
  577. return _CLIMIT;
  578.  
  579. err_exit:
  580. if (QRES != NULL) free(QRES);
  581. if (PRES != NULL) free(PRES);
  582. /* if (Y != NULL) free(Y);
  583. if (B != NULL) free(B);
  584. if (RLGB != NULL) free(RLGB);*/
  585. exit(1);
  586. return 0.0; /* not reached */
  587. /* RETURN */
  588. /* END */
  589. }
  590.  
  591. /*
  592. FUNCTION CONTRL (D, Y, PLEVEL, PSLOPE)
  593. Catch control law
  594. */
  595. double CONTRL(double D, double Y, double PLEVEL, double PSLOPE)
  596. {
  597. double ret;
  598. if (D < PLEVEL) {
  599. ret = 0.0;
  600. } else if (D < 1.0) {
  601. ret = PSLOPE * Y * (D - PLEVEL);
  602. } else {
  603. ret = PSLOPE * Y * (1.0 - PLEVEL);
  604. }
  605. return ret;
  606. /* END */
  607. }
  608.  
  609. /*
  610. SUBROUTINE DEVIAN (SS0, SS1, SS2, SS3, SIGHT, FMATRX, ISYR,
  611. Calculate deviance (-2 log likelihood) in terms of coefficients for
  612. the bias and log bias parameters
  613. */
  614. void DEVIAN(double *SS0, double *SS1, double *SS2, double *SS3, double *SIGHT,
  615. double *FMATRX, int *ISYR, int *IZYR, double *ZMULT, double *POP, double *G)
  616. {
  617. /* REAL SS0,SS1,SS2,SIGHT(0:*),FMATRX(0:*),ZMULT(0:*),POP(0:*),G(0:*) */
  618. /* INTEGER ISYR(0:*), IZYR(0:*) */
  619. int N, J, K;
  620.  
  621. *SS0 = 0.0;
  622. *SS1 = 0.0;
  623. *SS2 = 0.0;
  624. *SS3 = 0.0;
  625.  
  626. /* DO 100 N = 0, NS - 1 */
  627. for (N = 0; N <= md.NS - 1; N++) {
  628.  
  629. *(G + N) = log(*(SIGHT + N) / *(POP+*(ISYR + N)));
  630. K = N * (N + 1) / 2;
  631.  
  632. /* DO 10 J = 0, N - 1 */
  633. for (J = 0; J <= N - 1; J++) {
  634. /*1st add non diagonal contributions (which are doubled up)*/
  635. *SS0 = *SS0 + 2.0 * *(G + J) * *(G + N) * *(FMATRX + K);
  636. *SS1 = *SS1 + 2.0 * (*(G + J) + *(G + N)) * *(FMATRX + K);
  637. *SS2 = *SS2 + *(FMATRX + K) + *(FMATRX + K);
  638. K = K + 1;
  639. } /* 10 CONTINUE*/
  640.  
  641. /* Now add diagnoal contribution */
  642. *SS0 = *SS0 + *(G + N) * *(G + N) * *(FMATRX + K);
  643. *SS1 = *SS1 + 2.0 * *(G + N) * *(FMATRX + K);
  644. *SS2 = *SS2 + *(FMATRX + K);
  645.  
  646. } /* 100 CONTINUE */
  647.  
  648. /* Now do the zero estimates */
  649. /* DO 200 N = 0, NZ - 1 */
  650. for (N =0; N <= md.NZ - 1; N++) {
  651. *SS3 = *SS3 + 2.0 * *(POP + *(IZYR + N)) / *(ZMULT + N);
  652. } /* 200 CONTINUE */
  653.  
  654. /* RETURN */
  655. /* END */
  656. return;
  657. }
  658. /*
  659. FUNCTION STKSIM (RK, R, POP, CATCH)
  660. Calculate the stock trajectory with parameter RK and R: return
  661. the current stock size
  662. RK = notional carrying capacity; R = productivity parameter * 1.4184
  663. */
  664. double STKSIM(double RK, double R, double *POP, double *CATCH)
  665. {
  666. /*INTEGER ISTART, IYEAR, NS, NZ, RKLO, RKHI */
  667. /* REAL CATCH(0:*), POP(0:*) */
  668. int I;
  669. double D;
  670. *(POP+md.ISTART) = RK;
  671. /*DO 10 I = md.ISTART + 1, md.IYEAR*/
  672. for (I = md.ISTART + 1; I <= md.IYEAR; I++) {
  673. D = *(POP + I - 1) / RK;
  674. *(POP + I) = *(POP + I - 1) * (1.0 + R * (1.0 - D * D)) - *(CATCH + I -1);
  675. if (*(POP + I) <= 0) { /*GOTO 20*/
  676. return 0.0;
  677. /* 20 STKSIM = 0. */
  678. }
  679. } /* 10 CONTINUE */
  680. /* STKSIM = POP(md.IYEAR) */
  681. return *(POP + md.IYEAR);
  682. /* RETURN */
  683. /* END */
  684. }
  685. /*
  686. SUBROUTINE SORT (ARRAY, ARRAY2, N)
  687. SORT sorts a pair of arrays in ascending order of the first array
  688.   (using the heapsort algorithm)
  689. */
  690. void SORT(double *ARRAY, double *ARRAY2, int N)
  691. {
  692. double /*ARRAY(0:*), ARRAY2(0:*),*/ TEMP, TEMP2;
  693. int /*N,*/ K, IR, I, J;
  694. if (N < 2) return;
  695. K = N / 2;
  696. IR = N - 1;
  697. while (1) { /*10*/
  698. if (K != 0) {
  699. K = K - 1;
  700. TEMP = *(ARRAY + K);
  701. TEMP2 = *(ARRAY2 + K);
  702. } else {
  703. TEMP = *(ARRAY + IR);
  704. TEMP2 = *(ARRAY2 + IR);
  705. *(ARRAY + IR) = *ARRAY;
  706. *(ARRAY2 + IR) = *ARRAY2;
  707. IR = IR - 1;
  708. if (IR == 0) {
  709. *ARRAY = TEMP;
  710. *ARRAY2 = TEMP2;
  711. return;
  712. }
  713. }
  714. I = K;
  715. J = K + K + 1;
  716. while (1) {/*20*/
  717. if (J > IR) break;
  718. if ((J < IR) && (*(ARRAY + J) < *(ARRAY+ J + 1))) J = J + 1;
  719. if (TEMP < *(ARRAY + J)) {
  720. *(ARRAY + I) = *(ARRAY + J);
  721. *(ARRAY2 + I) = *(ARRAY2 + J);
  722. I = J;
  723. J = J + I + 1;
  724. } else {
  725. J = IR + 1;
  726. }
  727. } /*GOTO 20*/
  728. *(ARRAY + I) = TEMP;
  729. *(ARRAY2 + I) = TEMP2;
  730. }/*GOTO 10*/
  731. /*END*/
  732. }
  733. int _getline(char *buff, size_t size, FILE *fp){
  734. char *ret;
  735. ret = fgets(buff, size, fp);
  736. if (ret != NULL) {
  737. if (buff[strlen(buff) - 1] == '\n')
  738. buff[strlen(buff) - 1] = 0;
  739. if (buff[strlen(buff) - 1] == '\r')
  740. buff[strlen(buff) - 1] = 0;
  741. }
  742. return (ret != NULL);
  743. }
  744.  
Runtime error #stdin #stdout #stderr 0s 9432KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Can't open CLC.DAT