commit 4fdd40178f5d747c20e78227d856299550a19873
parent 718eab90f6d20ac85bc702ab921f708ed60bd311
Author: Christophe Coustet <christophe.coustet@meso-star.com>
Date: Fri, 7 Oct 2016 18:42:04 +0200
CommitFix: add missing files
Diffstat:
3 files changed, 268 insertions(+), 0 deletions(-)
diff --git a/src/ssol_estimator.c b/src/ssol_estimator.c
@@ -0,0 +1,135 @@
+/* Copyright (C) CNRS 2016
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "ssol.h"
+#include "ssol_estimator_c.h"
+#include "ssol_device_c.h"
+
+#include <rsys/rsys.h>
+#include <rsys/mem_allocator.h>
+#include <rsys/ref_count.h>
+
+#include <math.h>
+
+/*******************************************************************************
+* Helper functions
+******************************************************************************/
+static void
+estimator_release(ref_T* ref)
+{
+ struct ssol_device* dev;
+ struct ssol_estimator* estimator = CONTAINER_OF(ref, struct ssol_estimator, ref);
+ ASSERT(ref);
+ dev = estimator->dev;
+ ASSERT(dev && dev->allocator);
+ MEM_RM(dev->allocator, estimator);
+ SSOL(device_ref_put(dev));
+}
+
+/*******************************************************************************
+* Exported ssol_estimator functions
+******************************************************************************/
+res_T
+ssol_estimator_create
+(struct ssol_device* dev,
+ struct ssol_estimator** out_estimator)
+{
+ struct ssol_estimator* estimator = NULL;
+ res_T res = RES_OK;
+ if (!dev || !out_estimator) {
+ return RES_BAD_ARG;
+ }
+
+ estimator = (struct ssol_estimator*)MEM_CALLOC
+ (dev->allocator, 1, sizeof(struct ssol_estimator));
+ if (!estimator) {
+ res = RES_MEM_ERR;
+ goto error;
+ }
+
+ SSOL(device_ref_get(dev));
+ estimator->dev = dev;
+ ref_init(&estimator->ref);
+
+exit:
+ if (out_estimator) *out_estimator = estimator;
+ return res;
+error:
+ if (estimator) {
+ SSOL(estimator_ref_put(estimator));
+ estimator = NULL;
+ }
+ goto exit;
+}
+
+res_T
+ssol_estimator_get_status
+ (const struct ssol_estimator* estimator,
+ enum status_type type,
+ struct ssol_estimator_status* status)
+{
+ const struct mc_data* data;
+ if (!estimator || type >= STATUS_TYPES_COUNT__ || !status)
+ return RES_BAD_ARG;
+
+ switch (type) {
+ case STATUS_SHADOW:
+ data = &estimator->shadow;
+ break;
+ case STATUS_MISSING:
+ data = &estimator->missing;
+ break;
+ default: FATAL("Unreachable code.\n"); break;
+ }
+ status->N = estimator->realisation_count;
+ status->E = data->weight / status->N;
+ status->V = data->sqr_weight / status->N - status->E * status->E;
+ status->SE = (status->V > 0) ? sqrt(status->V / status->N) : 0;
+
+ return RES_OK;
+}
+
+res_T
+ssol_estimator_clear
+(struct ssol_estimator* estimator)
+{
+ if (!estimator)
+ return RES_BAD_ARG;
+
+ estimator->realisation_count = 0;
+ CLEAR_MC_DATA(estimator->shadow);
+ CLEAR_MC_DATA(estimator->missing);
+ return RES_OK;
+}
+
+res_T
+ssol_estimator_ref_get
+(struct ssol_estimator* estimator)
+{
+ if (!estimator)
+ return RES_BAD_ARG;
+ ref_get(&estimator->ref);
+ return RES_OK;
+}
+
+res_T
+ssol_estimator_ref_put
+(struct ssol_estimator* estimator)
+{
+ if (!estimator)
+ return RES_BAD_ARG;
+ ref_put(&estimator->ref, estimator_release);
+ return RES_OK;
+}
diff --git a/src/ssol_estimator_c.h b/src/ssol_estimator_c.h
@@ -0,0 +1,45 @@
+/* Copyright (C) CNRS 2016
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef SSOL_ESTIMATOR_C_H
+#define SSOL_ESTIMATOR_C_H
+
+#include <star/smc.h>
+
+#include <rsys/ref_count.h>
+
+/* Monte carlo data */
+struct mc_data {
+ double weight;
+ double sqr_weight;
+};
+
+#define CLEAR_MC_DATA(d) ((d).weight=0,(d).sqr_weight=0)
+
+#define MC_DATA_NULL__ { 0, 0 }
+static const struct mc_data MC_DATA_NULL = MC_DATA_NULL__;
+
+struct ssol_estimator {
+ size_t realisation_count;
+ /* the implicit MC computations */
+ struct mc_data shadow;
+ struct mc_data missing;
+ /* 2 global MC per receiver: one for P, one for cos effect losses */
+
+ struct ssol_device* dev;
+ ref_T ref;
+};
+
+#endif /* SSOL_ESTIMATOR_C_H */
diff --git a/src/test_ssol_estimator.c b/src/test_ssol_estimator.c
@@ -0,0 +1,88 @@
+/* Copyright (C) CNRS 2016
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "ssol.h"
+#include "test_ssol_utils.h"
+#include "test_ssol_materials.h"
+
+#define HALF_X 1
+#define HALF_Y 1
+#define PLANE_NAME SQUARE
+#include "test_ssol_rect_geometry.h"
+
+#include "ssol_solver_c.h"
+
+#include <rsys/logger.h>
+#include <rsys/double33.h>
+
+#include <star/s3d.h>
+#include <star/ssp.h>
+
+/*******************************************************************************
+* Test main program
+******************************************************************************/
+int
+main(int argc, char** argv)
+{
+ struct logger logger;
+ struct mem_allocator allocator;
+ struct ssol_device* dev;
+ struct ssp_rng* rng;
+ struct ssol_estimator* estimator;
+ struct ssol_estimator_status status;
+
+ (void) argc, (void) argv;
+
+ mem_init_proxy_allocator(&allocator, &mem_default_allocator);
+
+ CHECK(logger_init(&allocator, &logger), RES_OK);
+ logger_set_stream(&logger, LOG_OUTPUT, log_stream, NULL);
+ logger_set_stream(&logger, LOG_ERROR, log_stream, NULL);
+ logger_set_stream(&logger, LOG_WARNING, log_stream, NULL);
+
+ CHECK(ssol_device_create
+ (&logger, &allocator, SSOL_NTHREADS_DEFAULT, 0, &dev), RES_OK);
+
+ CHECK(ssp_rng_create(&allocator, &ssp_rng_threefry, &rng), RES_OK);
+
+ CHECK(ssol_estimator_create(NULL, &estimator), RES_BAD_ARG);
+ CHECK(ssol_estimator_create(dev, NULL), RES_BAD_ARG);
+ CHECK(ssol_estimator_create(dev, &estimator), RES_OK);
+
+ CHECK(ssol_estimator_ref_get(NULL), RES_BAD_ARG);
+ CHECK(ssol_estimator_ref_get(estimator), RES_OK);
+ CHECK(ssol_estimator_ref_put(NULL), RES_BAD_ARG);
+ CHECK(ssol_estimator_ref_put(estimator), RES_OK);
+
+ CHECK(ssol_estimator_get_status(NULL, STATUS_MISSING, &status), RES_BAD_ARG);
+ CHECK(ssol_estimator_get_status(estimator, STATUS_TYPES_COUNT__, &status), RES_BAD_ARG);
+ CHECK(ssol_estimator_get_status(estimator, STATUS_MISSING, NULL), RES_BAD_ARG);
+ CHECK(ssol_estimator_get_status(estimator, STATUS_MISSING, &status), RES_OK);
+
+ CHECK(ssol_estimator_ref_put(estimator), RES_OK);
+
+ /* free data */
+
+ CHECK(ssol_device_ref_put(dev), RES_OK);
+ CHECK(ssp_rng_ref_put(rng), RES_OK);
+
+ logger_release(&logger);
+
+ check_memory_allocator(&allocator);
+ mem_shutdown_proxy_allocator(&allocator);
+ CHECK(mem_allocated_size(), 0);
+
+ return 0;
+}