solppraw.c (6707B)
1 /* Copyright (C) 2017, 2018, 2025 |Méso|Star> 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include "solpp.h" 17 18 static inline void 19 print_rcv_side(FILE* output, const enum side side, struct rcv* rcv) 20 { 21 #define W(Name) rcv->Name[side].E, rcv->Name[side].SE 22 fprintf(output, " | [Incoming] "); 23 fprintf(output, " [Absorbed] \n"); 24 fprintf(output, " Flux | %16g +/- %-16g | %16g +/- %-16g\n", 25 W(in.flux), W(abs.flux)); 26 fprintf(output, " Material loss | %16g +/- %-16g | %16g +/- %-16g\n", 27 W(in.flux_mat_loss), W(abs.flux_mat_loss)); 28 fprintf(output, "Atmospheric loss | %16g +/- %-16g | %16g +/- %-16g\n", 29 W(in.flux_atm_loss), W(abs.flux_atm_loss)); 30 fprintf(output, "No Material loss | %16g +/- %-16g | %16g +/- %-16g\n", 31 W(in.flux_no_mat_loss), W(abs.flux_no_mat_loss)); 32 fprintf(output, " No Atmos. loss | %16g +/- %-16g | %16g +/- %-16g\n", 33 W(in.flux_no_atm_loss), W(abs.flux_no_atm_loss)); 34 fprintf(output, " |\n"); 35 fprintf(output, " Efficiency | %16g +/- %-16g\n", W(efficiency)); 36 #undef W 37 } 38 39 static inline void 40 print_rcvXprim_side(FILE* output, const enum side side, struct rcvXprim* rXp) 41 { 42 #define W(Name) rXp->Name[side].E, rXp->Name[side].SE 43 fprintf(output, " | [Incoming] "); 44 fprintf(output, " [Absorbed] \n"); 45 fprintf(output, " Flux | %16g +/- %-16g | %16g +/- %-16g\n", 46 W(in.flux), W(abs.flux)); 47 fprintf(output, " Material loss | %16g +/- %-16g | %16g +/- %-16g\n", 48 W(in.flux_mat_loss), W(abs.flux_mat_loss)); 49 fprintf(output, "Atmospheric loss | %16g +/- %-16g | %16g +/- %-16g\n", 50 W(in.flux_atm_loss), W(abs.flux_atm_loss)); 51 fprintf(output, "No Material loss | %16g +/- %-16g | %16g +/- %-16g\n", 52 W(in.flux_no_mat_loss), W(abs.flux_no_mat_loss)); 53 fprintf(output, " No Atmos. loss | %16g +/- %-16g | %16g +/- %-16g\n", 54 W(in.flux_no_atm_loss), W(abs.flux_no_atm_loss)); 55 #undef W 56 } 57 58 static inline void 59 print_simulation(FILE* output, struct simul* simul) 60 { 61 size_t i; 62 63 /* Global results */ 64 #define W(Name) simul->Name.E, simul->Name.SE 65 fprintf(output, " Overall results (#Samples = %zu)\n", simul->nsamps); 66 fprintf(output, "------------------------------------------------------------"); 67 fprintf(output, "------------------------------\n"); 68 fprintf(output, " Potential flux | %16g +/- %-16g\n", W(potential_flux)); 69 fprintf(output, " Absorbed flux | %16g +/- %-16g\n", W(absorbed_flux)); 70 fprintf(output, " Cosine factor | %16g +/- %-16g\n", W(cos_factor)); 71 fprintf(output, " Shadow loss | %16g +/- %-16g\n", W(shadow_loss)); 72 fprintf(output, " Missing loss | %16g +/- %-16g\n", W(missing_loss)); 73 fprintf(output, " Materials loss | %16g +/- %-16g\n", W(materials_loss)); 74 fprintf(output, "Atmospheric loss | %16g +/- %-16g\n", W(atmospheric_loss)); 75 fprintf(output, "\n"); 76 #undef W 77 /* Per receivers results */ 78 FOR_EACH(i, 0, BUF_SZ(simul->rcvs)) { 79 struct rcv* rcv = &BUF_AT(simul->rcvs, i); 80 fprintf(output, " Receiver `%s' (Area = %g)\n", rcv->name, rcv->area); 81 if(rcv->in.flux[FRONT].E >= 0) { 82 fprintf(output, "-----------------------------------------------------[Front]"); 83 fprintf(output, "------------------------------\n"); 84 print_rcv_side(output, FRONT, rcv); 85 } 86 if(rcv->in.flux[BACK].E >= 0) { 87 fprintf(output, "------------------------------------------------------[Back]"); 88 fprintf(output, "------------------------------\n"); 89 print_rcv_side(output, BACK, rcv); 90 } 91 fprintf(output, "\n"); 92 } 93 /* Per primary results */ 94 FOR_EACH(i, 0, BUF_SZ(simul->prims)) { 95 struct prim* prim = &BUF_AT(simul->prims, i); 96 fprintf(output, " Primary `%s' (Area = %g; #Samples = %zu)\n", 97 prim->name, prim->area, prim->nsamps); 98 #define W(Name) prim->Name.E, prim->Name.SE 99 fprintf(output, "------------------------------------------------------------"); 100 fprintf(output, "------------------------------\n"); 101 fprintf(output, " Cosine factor | %16g +/- %-16g\n", W(cos_factor)); 102 fprintf(output, " Shadow loss | %16g +/- %-16g\n", W(shadow_loss)); 103 fprintf(output, "\n"); 104 #undef W 105 } 106 /* Per receiverXprimary results */ 107 FOR_EACH(i, 0, BUF_SZ(simul->rcvXprims)) { 108 struct rcvXprim* rXp = &BUF_AT(simul->rcvXprims, i); 109 struct rcv* rcv = find_receiver_by_id(simul, rXp->rcv_id); 110 struct prim* prim = find_primary_by_id(simul, rXp->prim_id); 111 fprintf(output, " Receiver `%s' X Primary `%s'\n", rcv->name, prim->name); 112 if(rXp->in.flux[FRONT].E >= 0) { 113 fprintf(output, "-----------------------------------------------------[Front]"); 114 fprintf(output, "------------------------------\n"); 115 print_rcvXprim_side(output, FRONT, rXp); 116 } 117 if(rXp->in.flux[BACK].E >= 0) { 118 fprintf(output, "------------------------------------------------------[Back]"); 119 fprintf(output, "------------------------------\n"); 120 print_rcvXprim_side(output, BACK, rXp); 121 } 122 fprintf(output, "\n"); 123 } 124 } 125 126 int 127 main(int argc, char** argv) 128 { 129 char s[128]; 130 buf_char_T buf = BUF_NULL; 131 FILE* input = stdin; 132 char* line = NULL; 133 134 if(argc > 1 && !(input = fopen(argv[1], "r"))) { 135 fprintf(stderr, "Could not open the file `%s'.\n", argv[1]); 136 return 1; 137 } 138 while((line = read_line(&buf, input))) { 139 struct simul simul; 140 FILE* output = NULL; 141 142 if(strncmp(line, "#--- Sun direction:", 19)) continue; 143 144 simul_init(&simul); 145 146 CHK(!strncmp(line, "#--- Sun direction:", 19)); 147 CHK(sscanf(line+19, "%lf %lf (%*f %*f %*f)", 148 &simul.azimuth, &simul.elevation)==2); 149 CHK(snprintf(s, sizeof(s), "%g-%g-raw-results.txt", 150 simul.azimuth, simul.elevation) < sizeof(s)); 151 read_simulation(&simul, input); 152 153 printf("Writing `%s'\n", s); 154 CHK(output = fopen(s, "w")); 155 print_simulation(output, &simul); 156 fclose(output); 157 158 simul_release(&simul); 159 } 160 161 BUF_RELEASE(buf); 162 if(input && input != stdin) fclose(input); 163 return 0; 164 } 165