| 1 | /***
|
|---|
| 2 | * IMPORTANT: Change the values of the variables marked by 'CHANGEME'
|
|---|
| 3 | ***/
|
|---|
| 4 |
|
|---|
| 5 | #include <stdio.h>
|
|---|
| 6 | #include <stdlib.h>
|
|---|
| 7 | #include <math.h>
|
|---|
| 8 |
|
|---|
| 9 | #ifdef _CIVL
|
|---|
| 10 | $input double global_x[6];
|
|---|
| 11 | $output int global_retval;
|
|---|
| 12 | $output double global_f;
|
|---|
| 13 | $output double global_grad[6];
|
|---|
| 14 | #else
|
|---|
| 15 | //#pragma CIVL input
|
|---|
| 16 | double global_x[6];
|
|---|
| 17 | //#pragma CIVL output
|
|---|
| 18 | int global_retval;
|
|---|
| 19 | //#pragma CIVL output
|
|---|
| 20 | double global_f;
|
|---|
| 21 | //#pragma CIVL output
|
|---|
| 22 | double global_grad[6];
|
|---|
| 23 | #endif
|
|---|
| 24 |
|
|---|
| 25 | int g_fcn(double *obj, double g_x[6], const double x[6]);
|
|---|
| 26 |
|
|---|
| 27 | int main(void) {
|
|---|
| 28 | double x[6],obj,gradient[6];
|
|---|
| 29 | int retval;
|
|---|
| 30 | int i;
|
|---|
| 31 |
|
|---|
| 32 | #ifndef _CIVL
|
|---|
| 33 | // for (i=0;i<6;i++) global_x[i] = pow(i,0.6);
|
|---|
| 34 | FILE* f1 = fopen("x.in", "r");
|
|---|
| 35 | double in = 0;
|
|---|
| 36 | i = 0;
|
|---|
| 37 | while( fscanf(f1, "%lf,", &in) > 0 ) // parse %d followed by ','
|
|---|
| 38 | {
|
|---|
| 39 | global_x[i++] = in;
|
|---|
| 40 | }
|
|---|
| 41 | fclose(f1);
|
|---|
| 42 |
|
|---|
| 43 | #endif
|
|---|
| 44 | // printf("==================== INPUT STARTS HERE ====================\n");
|
|---|
| 45 | // for (i=0;i<6;i++) printf("global_x[%d] = %f\n", i, global_x[i]);
|
|---|
| 46 | // printf("==================== INPUT ENDS HERE ====================\n");
|
|---|
| 47 | for (i=0;i<6;i++) x[i] = global_x[i];
|
|---|
| 48 | // Flip second and third vertices if determinant is negative
|
|---|
| 49 | if ((x[1]-x[0])*(x[5]-x[3])-(x[4]-x[3])*(x[2]-x[0]) < 0.0)
|
|---|
| 50 | {
|
|---|
| 51 | obj = x[1];
|
|---|
| 52 | x[1] = x[2];
|
|---|
| 53 | x[2] = obj;
|
|---|
| 54 | obj = x[4];
|
|---|
| 55 | x[4] = x[5];
|
|---|
| 56 | x[5] = obj;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | retval = g_fcn(&obj,gradient,x);
|
|---|
| 60 | global_retval = retval;
|
|---|
| 61 | #ifdef _CIVL
|
|---|
| 62 | // printf("==================== RESULT STARTS HERE ====================\n");
|
|---|
| 63 | #endif
|
|---|
| 64 | // printf("global_retval=%d\n", retval);
|
|---|
| 65 | global_f = obj;
|
|---|
| 66 | //printf("global_f=%f\n", obj);
|
|---|
| 67 | for (i=0;i<6;i++) global_grad[i] = gradient[i];
|
|---|
| 68 |
|
|---|
| 69 | FILE* f2 = fopen("g.out", "w");
|
|---|
| 70 | fprintf(f2, "%d\n", retval);
|
|---|
| 71 | for (i=0;i<6;i++) fprintf(f2, "%.17g\n", gradient[i]);
|
|---|
| 72 | fclose(f2);
|
|---|
| 73 |
|
|---|
| 74 | // for (i=0;i<6;i++) printf("global_grad[%d]=%f\n", i, gradient[i]);
|
|---|
| 75 | #ifdef _CIVL
|
|---|
| 76 | // printf("==================== RESULT ENDS HERE ====================\n");
|
|---|
| 77 | #endif
|
|---|
| 78 | return 0;
|
|---|
| 79 | }
|
|---|