commit 1dc4b4048d74254e35e36772d47fec4177e23649
parent 64af11c1a4866444225abb8dd3db2fc7280c8d53
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 19 Apr 2017 16:19:59 +0200
Update the Solstice parser spectrum API
The spectrum is now referenced by an "id" and is no more directly
contained into another data structure (e.g. the sun).
Diffstat:
9 files changed, 132 insertions(+), 41 deletions(-)
diff --git a/src/parser/solparser.c b/src/parser/solparser.c
@@ -233,6 +233,7 @@ parser_clear(struct solparser* parser)
darray_anchor_clear(&parser->anchors);
darray_x_pivot_clear(&parser->x_pivots);
darray_zx_pivot_clear(&parser->zx_pivots);
+ darray_spectrum_clear(&parser->spectra);
}
static void
@@ -290,6 +291,7 @@ parser_release(ref_T* ref)
darray_anchor_release(&parser->anchors);
darray_x_pivot_release(&parser->x_pivots);
darray_zx_pivot_release(&parser->zx_pivots);
+ darray_spectrum_release(&parser->spectra);
MEM_RM(parser->allocator, parser);
}
@@ -618,10 +620,11 @@ solparser_create
htable_str2sols_init(mem_allocator, &parser->str2entities);
darray_entity_init(mem_allocator, &parser->entities);
- /* Anchors and pivot(2)s */
+ /* Miscellaneous */
darray_anchor_init(mem_allocator, &parser->anchors);
darray_x_pivot_init(mem_allocator, &parser->x_pivots);
darray_zx_pivot_init(mem_allocator, &parser->zx_pivots);
+ darray_spectrum_init(mem_allocator, &parser->spectra);
exit:
*out_parser = parser;
@@ -1098,6 +1101,15 @@ solparser_get_shape_stl
return darray_impgeom_cdata_get(&parser->stls) + impgeom.i;
}
+const struct solparser_spectrum*
+solparser_get_spectrum
+ (const struct solparser* parser,
+ const struct solparser_spectrum_id spectrum)
+{
+ ASSERT(parser && spectrum.i < darray_spectrum_size_get(&parser->spectra));
+ return darray_spectrum_cdata_get(&parser->spectra) + spectrum.i;
+}
+
const struct solparser_sun*
solparser_get_sun(const struct solparser* parser)
{
diff --git a/src/parser/solparser.h b/src/parser/solparser.h
@@ -17,6 +17,7 @@
#define SOLPARSER_H
#include "solparser_entity.h"
+#include "solparser_spectrum.h"
#include <rsys/rsys.h>
#include <stddef.h>
@@ -204,6 +205,11 @@ solparser_get_zx_pivot
(const struct solparser* parser,
const struct solparser_pivot_id zx_pivot);
+extern LOCAL_SYM const struct solparser_spectrum*
+solparser_get_spectrum
+ (const struct solparser* parser,
+ const struct solparser_spectrum_id spectrum);
+
/*******************************************************************************
* Entity interator
******************************************************************************/
diff --git a/src/parser/solparser_c.h b/src/parser/solparser_c.h
@@ -23,6 +23,7 @@
#include "solparser_medium.h"
#include "solparser_pivot.h"
#include "solparser_shape.h"
+#include "solparser_spectrum.h"
#include "solparser_sun.h"
#include <rsys/dynamic_array.h>
@@ -193,6 +194,15 @@ struct target_alias {
#define DARRAY_FUNCTOR_INIT solparser_zx_pivot_init
#include <rsys/dynamic_array.h>
+/* Declare the array of spectra */
+#define DARRAY_NAME spectrum
+#define DARRAY_DATA struct solparser_spectrum
+#define DARRAY_FUNCTOR_INIT solparser_spectrum_init
+#define DARRAY_FUNCTOR_RELEASE solparser_spectrum_release
+#define DARRAY_FUNCTOR_COPY solparser_spectrum_copy
+#define DARRAY_FUNCTOR_COPY_AND_RELEASE solparser_spectrum_copy_and_release
+#include <rsys/dynamic_array.h>
+
/* Declare the hash table that maps the address of a YAML node to the id of its
* in memory representation. */
#define HTABLE_NAME yaml2sols
@@ -251,6 +261,7 @@ struct solparser {
struct darray_anchor anchors;
struct darray_x_pivot x_pivots;
struct darray_zx_pivot zx_pivots;
+ struct darray_spectrum spectra;
ref_T ref;
struct mem_allocator* allocator;
@@ -394,7 +405,7 @@ parse_spectrum
(struct solparser* parser,
yaml_document_t* doc,
const yaml_node_t* spectrum,
- struct darray_spectrum_data* data);
+ struct solparser_spectrum_id* out_ispectrum);
extern LOCAL_SYM res_T
parse_sun
diff --git a/src/parser/solparser_spectrum.c b/src/parser/solparser_spectrum.c
@@ -116,11 +116,13 @@ parse_spectrum
(struct solparser* parser,
yaml_document_t* doc,
const yaml_node_t* spectrum,
- struct darray_spectrum_data* data)
+ struct solparser_spectrum_id* out_ispectrum)
{
+ struct solparser_spectrum* spec;
+ size_t ispec = SIZE_MAX;
intptr_t i, n;
res_T res = RES_OK;
- ASSERT(doc && spectrum && data);
+ ASSERT(doc && spectrum && out_ispectrum);
if(spectrum->type != YAML_SEQUENCE_NODE) {
log_err(parser, spectrum, "expect a list of spectrum data.\n");
@@ -128,8 +130,17 @@ parse_spectrum
goto error;
}
+ /* Allocate the spectrum */
+ ispec = darray_spectrum_size_get(&parser->spectra);
+ res = darray_spectrum_resize(&parser->spectra, ispec + 1);
+ if(res != RES_OK) {
+ log_err(parser, spectrum, "could not allocate the spectrum.\n");
+ goto error;
+ }
+ spec = darray_spectrum_data_get(&parser->spectra) + ispec;
+
n = spectrum->data.sequence.items.top - spectrum->data.sequence.items.start;
- res = darray_spectrum_data_resize(data, (size_t)n);
+ res = darray_spectrum_data_resize(&spec->data, (size_t)n);
if(res != RES_OK) {
log_err(parser, spectrum, "could not allocate the list of spectrum data.\n");
goto error;
@@ -140,7 +151,7 @@ parse_spectrum
struct solparser_spectrum_data* spectrum_data;
sdata = yaml_document_get_node(doc, spectrum->data.sequence.items.start[i]);
- spectrum_data = darray_spectrum_data_data_get(data) + i;
+ spectrum_data = darray_spectrum_data_data_get(&spec->data) + i;
res = parse_spectrum_data(parser, doc, sdata, spectrum_data);
if(res != RES_OK) goto error;
}
@@ -148,16 +159,16 @@ parse_spectrum
if(n == 1) goto exit;
qsort
- (darray_spectrum_data_data_get(data),
- darray_spectrum_data_size_get(data),
+ (darray_spectrum_data_data_get(&spec->data),
+ darray_spectrum_data_size_get(&spec->data),
sizeof(struct solparser_spectrum_data),
cmp_spectrum_data);
FOR_EACH(i, 1, n) {
const struct solparser_spectrum_data* a;
const struct solparser_spectrum_data* b;
- a = darray_spectrum_data_cdata_get(data) + i - 1;
- b = darray_spectrum_data_cdata_get(data) + i;
+ a = darray_spectrum_data_cdata_get(&spec->data) + i - 1;
+ b = darray_spectrum_data_cdata_get(&spec->data) + i;
ASSERT(cmp_spectrum_data(a, b) <= 0);
if(a->wavelength == b->wavelength) {
log_err(parser, spectrum,
@@ -168,9 +179,13 @@ parse_spectrum
}
exit:
+ out_ispectrum->i = ispec;
return res;
error:
- darray_spectrum_data_clear(data);
+ if(spec) {
+ darray_spectrum_pop_back(&parser->spectra);
+ ispec = SIZE_MAX;
+ }
goto exit;
}
diff --git a/src/parser/solparser_spectrum.h b/src/parser/solparser_spectrum.h
@@ -27,5 +27,44 @@ struct solparser_spectrum_data {
#define DARRAY_DATA struct solparser_spectrum_data
#include <rsys/dynamic_array.h>
+struct solparser_spectrum {
+ struct darray_spectrum_data data;
+};
+
+struct solparser_spectrum_id { size_t i; };
+
+static INLINE void
+solparser_spectrum_init
+ (struct mem_allocator* allocator, struct solparser_spectrum* spectrum)
+{
+ ASSERT(spectrum);
+ darray_spectrum_data_init(allocator, &spectrum->data);
+}
+
+static INLINE void
+solparser_spectrum_release(struct solparser_spectrum* spectrum)
+{
+ ASSERT(spectrum);
+ darray_spectrum_data_release(&spectrum->data);
+}
+
+static INLINE res_T
+solparser_spectrum_copy
+ (struct solparser_spectrum* dst,
+ const struct solparser_spectrum* src)
+{
+ ASSERT(dst && src);
+ return darray_spectrum_data_copy(&dst->data, &src->data);
+}
+
+static INLINE res_T
+solparser_spectrum_copy_and_release
+ (struct solparser_spectrum* dst,
+ struct solparser_spectrum* src)
+{
+ ASSERT(dst && src);
+ return darray_spectrum_data_copy_and_release(&dst->data, &src->data);
+}
+
#endif /* SOLPARSER_SPECTRUM_H */
diff --git a/src/parser/solparser_sun.h b/src/parser/solparser_sun.h
@@ -30,7 +30,7 @@ struct solparser_sun_pillbox { double aperture; };
struct solparser_sun {
double dni; /* In ]0, INF) */
- struct darray_spectrum_data spectrum;
+ struct solparser_spectrum_id spectrum;
enum solparser_sun_radang_distrib_type radang_distrib_type;
union {
struct solparser_sun_buie buie;
@@ -42,38 +42,25 @@ static INLINE void
solparser_sun_init(struct mem_allocator* allocator, struct solparser_sun* sun)
{
ASSERT(sun);
+ (void)allocator;
sun->dni = 1.0;
sun->radang_distrib_type = SOLPARSER_SUN_RADANG_DISTRIB_DIRECTIONAL;
- darray_spectrum_data_init(allocator, &sun->spectrum);
+ sun->spectrum.i = SIZE_MAX;
}
static INLINE void
solparser_sun_release(struct solparser_sun* sun)
{
ASSERT(sun);
- darray_spectrum_data_release(&sun->spectrum);
-}
-
-static INLINE res_T
-solparser_sun_copy(struct solparser_sun* dst, const struct solparser_sun* src)
-{
- ASSERT(dst && src);
- return darray_spectrum_data_copy(&dst->spectrum, &src->spectrum);
-}
-
-static INLINE res_T
-solparser_sun_copy_and_release
- (struct solparser_sun* dst, struct solparser_sun* src)
-{
- ASSERT(dst && src);
- return darray_spectrum_data_copy_and_release(&dst->spectrum, &src->spectrum);
+ (void)sun;
+ /* Do nothing */
}
static INLINE void
solparser_sun_clear(struct solparser_sun* sun)
{
ASSERT(sun);
- darray_spectrum_data_clear(&sun->spectrum);
+ sun->spectrum.i = SIZE_MAX;
}
#endif /* SOLPARSER_SUN_H */
diff --git a/src/parser/test_solparser2.c b/src/parser/test_solparser2.c
@@ -40,6 +40,7 @@ main(int argc, char** argv)
const struct solparser_material_mirror* mirror;
const struct solparser_shape_sphere* sphere;
const struct solparser_sun* sun;
+ const struct solparser_spectrum* spectrum;
size_t nmtls = 0;
size_t ngeoms = 0;
double tmp[3];
@@ -114,7 +115,7 @@ main(int argc, char** argv)
entity_id = solparser_entity_iterator_get(&it);
entity = solparser_get_entity(parser, entity_id);
-
+
CHECK(strcmp("lvl 0", str_cget(&entity->name)), 0);
CHECK(solparser_entity_get_children_count(entity), 2);
CHECK(entity->type, SOLPARSER_ENTITY_GEOMETRY);
@@ -203,9 +204,11 @@ main(int argc, char** argv)
NCHECK(sun, NULL);
CHECK(sun->dni, 1.0);
CHECK(sun->radang_distrib_type, SOLPARSER_SUN_RADANG_DISTRIB_DIRECTIONAL);
- CHECK(darray_spectrum_data_size_get(&sun->spectrum), 1);
- CHECK(darray_spectrum_data_cdata_get(&sun->spectrum)[0].wavelength, 1.0);
- CHECK(darray_spectrum_data_cdata_get(&sun->spectrum)[0].data, 1.0);
+ CHECK(SOLPARSER_ID_IS_VALID(sun->spectrum), 1);
+ spectrum = solparser_get_spectrum(parser, sun->spectrum);
+ CHECK(darray_spectrum_data_size_get(&spectrum->data), 1);
+ CHECK(darray_spectrum_data_cdata_get(&spectrum->data)[0].wavelength, 1.0);
+ CHECK(darray_spectrum_data_cdata_get(&spectrum->data)[0].data, 1.0);
CHECK(solparser_load(parser), RES_BAD_OP);
solparser_ref_put(parser);
diff --git a/src/parser/test_solparser_spectrum.c b/src/parser/test_solparser_spectrum.c
@@ -21,6 +21,7 @@ static void
test_sun(struct solparser* parser)
{
const struct solparser_sun* sun;
+ const struct solparser_spectrum* spectrum;
FILE* stream;
size_t i;
@@ -51,15 +52,30 @@ test_sun(struct solparser* parser)
sun = solparser_get_sun(parser);
CHECK(sun->dni, 123.456);
CHECK(sun->radang_distrib_type, SOLPARSER_SUN_RADANG_DISTRIB_DIRECTIONAL);
- CHECK(darray_spectrum_data_size_get(&sun->spectrum), 9);
+ CHECK(SOLPARSER_ID_IS_VALID(sun->spectrum), 1);
+ spectrum = solparser_get_spectrum(parser, sun->spectrum);
- FOR_EACH(i, 0, darray_spectrum_data_size_get(&sun->spectrum)) {
- CHECK(darray_spectrum_data_cdata_get(&sun->spectrum)[i].wavelength, i+1);
- CHECK(darray_spectrum_data_cdata_get(&sun->spectrum)[i].wavelength, i+1);
+ CHECK(darray_spectrum_data_size_get(&spectrum->data), 9);
+
+ FOR_EACH(i, 0, darray_spectrum_data_size_get(&spectrum->data)) {
+ CHECK(darray_spectrum_data_cdata_get(&spectrum->data)[i].wavelength, i+1);
+ CHECK(darray_spectrum_data_cdata_get(&spectrum->data)[i].wavelength, i+1);
}
CHECK(solparser_load(parser), RES_BAD_OP);
fclose(stream);
+
+ NCHECK(stream = tmpfile(), NULL);
+ fprintf(stream, "- sun: {dni: 1}\n");
+ rewind(stream);
+
+ CHECK(solparser_setup(parser, NULL, stream), RES_OK);
+ CHECK(solparser_load(parser), RES_OK);
+
+ CHECK(SOLPARSER_ID_IS_VALID(sun->spectrum), 0);
+
+ CHECK(solparser_load(parser), RES_BAD_OP);
+ fclose(stream);
}
int
diff --git a/src/solstice_sun.c b/src/solstice_sun.c
@@ -139,6 +139,7 @@ create_sun_spectrum
{
struct ssol_spectrum* spectrum = NULL;
const struct solparser_spectrum_data* data;
+ const struct solparser_spectrum* spec;
size_t nwlens;
res_T res = RES_OK;
ASSERT(solstice && solparser_sun && out_spectrum);
@@ -149,8 +150,9 @@ create_sun_spectrum
goto error;
}
- nwlens = darray_spectrum_data_size_get(&solparser_sun->spectrum);
- data = darray_spectrum_data_cdata_get(&solparser_sun->spectrum);
+ spec = solparser_get_spectrum(solstice->parser, solparser_sun->spectrum);
+ nwlens = darray_spectrum_data_size_get(&spec->data);
+ data = darray_spectrum_data_cdata_get(&spec->data);
res = ssol_spectrum_setup(spectrum, get_wavelength, nwlens, (void*)data);
if(res != RES_OK) {
fprintf(stderr, "Could not setup the spectrum of the solver sun.\n");
@@ -195,7 +197,7 @@ solstice_create_sun(struct solstice* solstice)
}
if(res != RES_OK) goto error;
- if(solparser_sun->spectrum.size) {
+ if(SOLPARSER_ID_IS_VALID(solparser_sun->spectrum)) {
res = create_sun_spectrum(solstice, solparser_sun, &spectrum);
if (res != RES_OK) goto error;