| [4d61ad0] | 1 | #include <string.h>
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 | #include <stdlib.h>
|
|---|
| 4 | #include "contextAD.h"
|
|---|
| 5 |
|
|---|
| 6 | double nextRandom() {
|
|---|
| 7 | dbad_currentSeed += dbad_seed ;
|
|---|
| 8 | if (dbad_currentSeed>1.0) dbad_currentSeed-=1.0 ;
|
|---|
| 9 | return dbad_currentSeed ;
|
|---|
| 10 | }
|
|---|
| 11 |
|
|---|
| 12 | void context_tgt_init(double epsilon, double seed) {
|
|---|
| 13 | dbad_mode = 1 ;
|
|---|
| 14 | dbad_ddeps = epsilon ;
|
|---|
| 15 | dbad_seed = seed ;
|
|---|
| 16 | char* phase = getenv("DBAD_PHASE") ;
|
|---|
| 17 | if (phase==NULL) {
|
|---|
| 18 | printf("Please set DBAD_PHASE environment variable to 1 (perturbed) or 2 (tangent)\n") ;
|
|---|
| 19 | exit(0) ;
|
|---|
| 20 | } else if (strcmp(phase,"2")==0) {
|
|---|
| 21 | printf("Tangent code, seed=%7.1e\n", seed) ;
|
|---|
| 22 | printf("=============================================\n") ;
|
|---|
| 23 | dbad_phase = 2 ;
|
|---|
| 24 | dbad_currentSeed = 0.0 ;
|
|---|
| 25 | } else if (strcmp(phase,"1")==0) {
|
|---|
| 26 | printf("Perturbed run, seed=%7.1e, epsilon=%7.1e\n", seed, epsilon) ;
|
|---|
| 27 | printf("=============================================\n") ;
|
|---|
| 28 | dbad_phase = 1 ;
|
|---|
| 29 | dbad_currentSeed = 0.0 ;
|
|---|
| 30 | } else if (strcmp(phase,"99")==0) {
|
|---|
| 31 | printf("INTERNAL INTERFACE TESTS, seed=%7.1e, epsilon=%7.1e\n", seed, epsilon) ;
|
|---|
| 32 | printf("=============================================\n") ;
|
|---|
| 33 | dbad_phase = 99 ;
|
|---|
| 34 | } else {
|
|---|
| 35 | printf("DBAD_PHASE environment variable must be set to 1 or 2\n") ;
|
|---|
| 36 | exit(0) ;
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 | /** Version of context_tgt_init called from Fortran */
|
|---|
| 40 | void context_tgt_init_(double *epsilon, double *seed) {
|
|---|
| 41 | context_tgt_init(*epsilon, *seed) ;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | void context_tgt_initreal8(char* varname, double *indep, double *indepd) {
|
|---|
| 45 | *indepd = nextRandom() ;
|
|---|
| 46 | if (dbad_phase==1)
|
|---|
| 47 | *indep = (*indep)+dbad_ddeps*(*indepd) ;
|
|---|
| 48 | else if (dbad_phase==99)
|
|---|
| 49 | printf("initreal8_ of %s: %24.16e //%24.16e\n", varname, *indep, *indepd) ;
|
|---|
| 50 | }
|
|---|
| 51 | /** Version of context_tgt_initreal8 called from Fortran */
|
|---|
| 52 | void context_tgt_initreal8_(char* varname, double *indep, double *indepd) {
|
|---|
| 53 | context_tgt_initreal8(varname, indep, indepd) ;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | void context_tgt_initreal8array(char* varname, double *indep, double *indepd, int length) {
|
|---|
| 57 | int i ;
|
|---|
| 58 | for (i=0 ; i<length ; ++i) {
|
|---|
| 59 | indepd[i] = nextRandom() ;
|
|---|
| 60 | }
|
|---|
| 61 | if (dbad_phase==1) {
|
|---|
| 62 | for (i=0 ; i<length ; ++i) {
|
|---|
| 63 | indep[i] = indep[i]+dbad_ddeps*indepd[i] ;
|
|---|
| 64 | }
|
|---|
| 65 | } else if (dbad_phase==99) {
|
|---|
| 66 | printf("initreal8array_ of %s, length=%i:\n", varname, length) ;
|
|---|
| 67 | for (i=0 ; i<length ; ++i)
|
|---|
| 68 | printf(" %i:%24.16e //%24.16e",i,indep[i],indepd[i]) ;
|
|---|
| 69 | printf("\n") ;
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 | /** Version of context_tgt_initreal8array called from Fortran */
|
|---|
| 73 | void context_tgt_initreal8array_(char* varname, double *indep, double *indepd, int *length) {
|
|---|
| 74 | context_tgt_initreal8array(varname, indep, indepd, *length) ;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void context_tgt_concludestart() {
|
|---|
| 78 | dbad_currentSeed= 0.0 ;
|
|---|
| 79 | dbad_condensed_val = 0.0 ;
|
|---|
| 80 | dbad_condensed_tgt = 0.0 ;
|
|---|
| 81 | }
|
|---|
| 82 | /** Version of context_tgt_concludestart called from Fortran */
|
|---|
| 83 | void context_tgt_concludestart_() {
|
|---|
| 84 | context_tgt_concludestart() ;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void context_tgt_concludereal8(char* varname, double dep, double depd) {
|
|---|
| 88 | double depb = nextRandom() ;
|
|---|
| 89 | dbad_condensed_val += depb*(dep) ;
|
|---|
| 90 | if (dbad_phase==2)
|
|---|
| 91 | dbad_condensed_tgt += depb*(depd) ;
|
|---|
| 92 | else if (dbad_phase==99)
|
|---|
| 93 | printf("concludereal8_ %24.16e //%24.16e //%24.16e\n", depb, dep, depd) ;
|
|---|
| 94 | }
|
|---|
| 95 | /** Version of context_tgt_concludereal8 called from Fortran */
|
|---|
| 96 | void context_tgt_concludereal8_(char* varname, double *dep, double *depd) {
|
|---|
| 97 | context_tgt_concludereal8(varname, *dep, *depd) ;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | void context_tgt_concludereal8array(char* varname, double *dep, double *depd, int length) {
|
|---|
| 101 | int i ;
|
|---|
| 102 | double depb ;
|
|---|
| 103 | for (i=0 ; i<length ; ++i) {
|
|---|
| 104 | depb = nextRandom() ;
|
|---|
| 105 | dbad_condensed_val += depb*dep[i] ;
|
|---|
| 106 | if (dbad_phase==2) {
|
|---|
| 107 | dbad_condensed_tgt += depb*depd[i] ;
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | /** Version of context_tgt_concludereal8array called from Fortran */
|
|---|
| 112 | void context_tgt_concludereal8array_(char* varname, double *dep, double *depd, int *length) {
|
|---|
| 113 | context_tgt_concludereal8array(varname, dep, depd, *length) ;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | void context_tgt_conclude() {
|
|---|
| 117 | if (dbad_phase==2) {
|
|---|
| 118 | printf("[seed:%7.1e] Condensed result : %24.16e\n", dbad_seed, dbad_condensed_val) ;
|
|---|
| 119 | printf("[seed:%7.1e] Condensed tangent: %24.16e\n", dbad_seed, dbad_condensed_tgt) ;
|
|---|
| 120 | } else if (dbad_phase==1) {
|
|---|
| 121 | printf("[seed:%7.1e] Condensed perturbed result : %24.16e (epsilon:%7.1e)\n",
|
|---|
| 122 | dbad_seed, dbad_condensed_val, dbad_ddeps) ;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | /** Version of context_tgt_conclude called from Fortran */
|
|---|
| 126 | void context_tgt_conclude_() {
|
|---|
| 127 | context_tgt_conclude() ;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | void context_adj_init(double seed) {
|
|---|
| 131 | dbad_mode = 0 ;
|
|---|
| 132 | dbad_seed = seed ;
|
|---|
| 133 | char* phase = getenv("DBAD_PHASE") ;
|
|---|
| 134 | if (phase==NULL) {
|
|---|
| 135 | dbad_phase = 0 ;
|
|---|
| 136 | } else if (strcmp(phase,"99")==0) {
|
|---|
| 137 | dbad_phase = 99 ;
|
|---|
| 138 | printf("INTERNAL INTERFACE TESTS, seed=%7.1e\n", seed) ;
|
|---|
| 139 | } else {
|
|---|
| 140 | dbad_phase = 0 ;
|
|---|
| 141 | }
|
|---|
| 142 | printf("Adjoint code, seed=%7.1e\n", seed) ;
|
|---|
| 143 | printf("===================================\n") ;
|
|---|
| 144 | dbad_currentSeed = 0.0 ;
|
|---|
| 145 | }
|
|---|
| 146 | /** Version of context_adj_init called from Fortran */
|
|---|
| 147 | void context_adj_init_(double *seed) {
|
|---|
| 148 | context_adj_init(*seed) ;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | void context_adj_initreal8(char* varname, double *dep, double *depb) {
|
|---|
| 152 | *depb = nextRandom() ;
|
|---|
| 153 | if (dbad_phase==99)
|
|---|
| 154 | printf("initreal8_ %24.16e\n", *depb) ;
|
|---|
| 155 | }
|
|---|
| 156 | /** Version of context_adj_initreal8 called from Fortran */
|
|---|
| 157 | void context_adj_initreal8_(char* varname, double *dep, double *depb) {
|
|---|
| 158 | context_adj_initreal8(varname, dep, depb) ;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | void context_adj_initreal8array(char* varname, double *dep, double *depb, int length) {
|
|---|
| 162 | int i ;
|
|---|
| 163 | for (i=0 ; i<length ; ++i) {
|
|---|
| 164 | depb[i] = nextRandom() ;
|
|---|
| 165 | }
|
|---|
| 166 | if (dbad_phase==99) {
|
|---|
| 167 | printf("initreal8array_ length=%i\n", length) ;
|
|---|
| 168 | for (i=0 ; i<length ; ++i)
|
|---|
| 169 | printf(" %i:%24.16e",i,depb[i]) ;
|
|---|
| 170 | printf("\n") ;
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 | /** Version of context_adj_initreal8array called from Fortran */
|
|---|
| 174 | void context_adj_initreal8array_(char* varname, double *dep, double *depb, int *length) {
|
|---|
| 175 | context_adj_initreal8array(varname, dep, depb, *length) ;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | void context_adj_concludestart() {
|
|---|
| 179 | dbad_currentSeed= 0.0 ;
|
|---|
| 180 | dbad_condensed_adj = 0.0 ;
|
|---|
| 181 | }
|
|---|
| 182 | /** Version of context_adj_concludestart called from Fortran */
|
|---|
| 183 | void context_adj_concludestart_() {
|
|---|
| 184 | context_adj_concludestart() ;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | void context_adj_concludereal8(char* varname, double dep, double depb) {
|
|---|
| 188 | double depd = nextRandom() ;
|
|---|
| 189 | dbad_condensed_adj += depd*depb ;
|
|---|
| 190 | if (dbad_phase==99)
|
|---|
| 191 | printf("concludereal8_ %24.16e //%24.16e\n", depb, depd) ;
|
|---|
| 192 | }
|
|---|
| 193 | /** Version of context_adj_concludereal8 called from Fortran */
|
|---|
| 194 | void context_adj_concludereal8_(char* varname, double *dep, double *depb) {
|
|---|
| 195 | context_adj_concludereal8(varname, *dep, *depb) ;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | void context_adj_concludereal8array(char* varname, double *dep, double *depb, int length) {
|
|---|
| 199 | int i ;
|
|---|
| 200 | double depd ;
|
|---|
| 201 | for (i=0 ; i<length ; ++i) {
|
|---|
| 202 | depd = nextRandom() ;
|
|---|
| 203 | dbad_condensed_adj += depd*depb[i] ;
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 | /** Version of context_adj_concludereal8array called from Fortran */
|
|---|
| 207 | void context_adj_concludereal8array_(char* varname, double *dep, double *depb, int *length) {
|
|---|
| 208 | context_adj_concludereal8array(varname, dep, depb, *length) ;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | void context_adj_conclude() {
|
|---|
| 212 | printf("[seed:%7.1e] Condensed adjoint: %24.16e\n", dbad_seed, dbad_condensed_adj) ;
|
|---|
| 213 | }
|
|---|
| 214 | /** Version of context_adj_conclude called from Fortran */
|
|---|
| 215 | void context_adj_conclude_() {
|
|---|
| 216 | context_adj_conclude() ;
|
|---|
| 217 | }
|
|---|