solstice

Compute collected power and efficiencies of a solar plant
git clone git://git.meso-star.com/solstice.git
Log | Files | Refs | README | LICENSE

solstice_spectrum.c (2896B)


      1 /* Copyright (C) 2018-2026 |Meso|Star> (contact@meso-star.com)
      2  * Copyright (C) 2016-2018 CNRS
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 3 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program. If not, see <http://www.gnu.org/licenses/>. */
     16 
     17 #include "solstice_c.h"
     18 
     19 struct context {
     20   double scale_factor;
     21   const struct solparser_spectrum_data* spectrum_data;
     22 };
     23 
     24 /*******************************************************************************
     25  * Helper function
     26  ******************************************************************************/
     27 static void
     28 get_wavelength(const size_t i, double* wlen, double* data, void* context)
     29 {
     30   const struct context* ctx = context;
     31   ASSERT(wlen && data && context);
     32   *wlen = ctx->spectrum_data[i].wavelength;
     33   *data = ctx->spectrum_data[i].data * ctx->scale_factor;
     34 }
     35 
     36 /*******************************************************************************
     37  * Local functions
     38  ******************************************************************************/
     39 res_T
     40 solstice_create_ssol_spectrum
     41   (struct solstice* solstice,
     42    const struct solparser_spectrum_id spectrum_id,
     43    struct ssol_spectrum** out_spectrum)
     44 {
     45   return solstice_create_scaled_ssol_spectrum
     46     (solstice, spectrum_id, 1, out_spectrum);
     47 }
     48 
     49 res_T
     50 solstice_create_scaled_ssol_spectrum
     51   (struct solstice* solstice,
     52    const struct solparser_spectrum_id spectrum_id,
     53    const double scale_factor,
     54    struct ssol_spectrum** out_spectrum)
     55 {
     56   struct context ctx;
     57   struct ssol_spectrum* spectrum = NULL;
     58   const struct solparser_spectrum* spec;
     59   size_t nwlens;
     60   res_T res = RES_OK;
     61   ASSERT(solstice && SOLPARSER_ID_IS_VALID(spectrum_id) && out_spectrum);
     62 
     63   res = ssol_spectrum_create(solstice->ssol, &spectrum);
     64   if(res != RES_OK) {
     65     fprintf(stderr, "Could not create the Solstice Solver spectrum.\n");
     66     goto error;
     67   }
     68 
     69   spec = solparser_get_spectrum(solstice->parser, spectrum_id);
     70   nwlens = darray_spectrum_data_size_get(&spec->data);
     71   ctx.spectrum_data = darray_spectrum_data_cdata_get(&spec->data);
     72   ctx.scale_factor = scale_factor;
     73   res = ssol_spectrum_setup(spectrum, get_wavelength, nwlens, &ctx);
     74   if(res != RES_OK) {
     75     fprintf(stderr, "Could not setup the Solstice Solver spectrum.\n");
     76     goto error;
     77   }
     78 
     79 exit:
     80   *out_spectrum = spectrum;
     81   return res;
     82 error:
     83   if(spectrum) {
     84     SSOL(spectrum_ref_put(spectrum));
     85     spectrum = NULL;
     86   }
     87   goto exit;
     88 }
     89