Skip to content

Commit

Permalink
adding C++ wrapper around raw GSL vector
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Voigt authored and Alexander Voigt committed Aug 21, 2016
1 parent 35033f6 commit 605606e
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 0 deletions.
108 changes: 108 additions & 0 deletions src/gsl_vector.cpp
@@ -0,0 +1,108 @@
// ====================================================================
// This file is part of FlexibleSUSY.
//
// FlexibleSUSY 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.
//
// FlexibleSUSY 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 FlexibleSUSY. If not, see
// <http://www.gnu.org/licenses/>.
// ====================================================================

#include "gsl_vector.hpp"
#include <cassert>

namespace flexiblesusy {

GSL_vector::GSL_vector()
: vec(NULL)
{
}

GSL_vector::GSL_vector(std::size_t size)
{
if (size)
vec = gsl_vector_calloc(size);
else
vec = NULL;
}

GSL_vector::GSL_vector(const GSL_vector& other)
{
vec = gsl_vector_alloc(other.size());
gsl_vector_memcpy(vec, other.vec);
}

GSL_vector::GSL_vector(GSL_vector&& other)
{
vec = other.vec;
other.vec = NULL;
}

GSL_vector::~GSL_vector()
{
gsl_vector_free(vec);
}

const GSL_vector& GSL_vector::operator=(const GSL_vector& other)
{
if (this != &other) {
gsl_vector_free(vec);
vec = gsl_vector_alloc(other.size());
gsl_vector_memcpy(vec, other.vec);
}

return *this;
}

double& GSL_vector::operator[](std::size_t n)
{
assert(vec);
assert(n < vec->size && "GSL_vector::operator[]: index out of range");
return *gsl_vector_ptr(vec, n);
}

double GSL_vector::operator[](std::size_t n) const
{
assert(vec);
assert(n < vec->size && "GSL_vector::operator[]: index out of range");
return gsl_vector_get(vec, n);
}

std::size_t GSL_vector::size() const
{
if (!vec) return 0;
return vec->size;
}

const gsl_vector* GSL_vector::raw() const
{
return vec;
}

gsl_vector* GSL_vector::raw()
{
return vec;
}

std::ostream& operator<<(std::ostream& ostr, const GSL_vector& vec)
{
std::cout << "(";

for (std::size_t i = 0; i < vec.size(); i++) {
std::cout << vec[i];
if (i < vec.size() - 1)
std::cout << ", ";
}

std::cout << ")";
}

} // namespace flexiblesusy
51 changes: 51 additions & 0 deletions src/gsl_vector.hpp
@@ -0,0 +1,51 @@
// ====================================================================
// This file is part of FlexibleSUSY.
//
// FlexibleSUSY 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.
//
// FlexibleSUSY 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 FlexibleSUSY. If not, see
// <http://www.gnu.org/licenses/>.
// ====================================================================

#ifndef GSL_VECTOR_H
#define GSL_VECTOR_H

#include <gsl/gsl_vector.h>
#include <iostream>

namespace flexiblesusy {

class GSL_vector {
public:
GSL_vector();
explicit GSL_vector(std::size_t);
GSL_vector(const GSL_vector&);
GSL_vector(GSL_vector&&);
~GSL_vector();

const GSL_vector& operator=(const GSL_vector&);
double& operator[](std::size_t);
double operator[](std::size_t) const;

const gsl_vector* raw() const;
gsl_vector* raw();
std::size_t size() const;

private:
gsl_vector* vec;
};

std::ostream& operator<<(std::ostream&, const GSL_vector&);

} // namespace flexiblesusy

#endif
2 changes: 2 additions & 0 deletions src/module.mk
Expand Up @@ -17,6 +17,7 @@ LIBFLEXI_SRC := \
$(DIR)/error.cpp \
$(DIR)/gm2calc_interface.cpp \
$(DIR)/gsl_utils.cpp \
$(DIR)/gsl_vector.cpp \
$(DIR)/linalg.cpp \
$(DIR)/lowe.cpp \
$(DIR)/sfermions.cpp \
Expand Down Expand Up @@ -71,6 +72,7 @@ LIBFLEXI_HDR := \
$(DIR)/functors.hpp \
$(DIR)/gm2calc_interface.hpp \
$(DIR)/gsl_utils.hpp \
$(DIR)/gsl_vector.hpp \
$(DIR)/gut_scale_calculator.hpp \
$(DIR)/two_loop_corrections.hpp \
$(DIR)/initial_guesser.hpp \
Expand Down
4 changes: 4 additions & 0 deletions test/module.mk
Expand Up @@ -26,6 +26,7 @@ TEST_SRC := \
$(DIR)/test_ewsb_solver.cpp \
$(DIR)/test_fixed_point_iterator.cpp \
$(DIR)/test_goldstones.cpp \
$(DIR)/test_gsl_vector.cpp \
$(DIR)/test_linalg2.cpp \
$(DIR)/test_minimizer.cpp \
$(DIR)/test_namespace_collisions.cpp \
Expand Down Expand Up @@ -456,6 +457,9 @@ $(DIR)/test_fixed_point_iterator.x: $(DIR)/test_fixed_point_iterator.o $(LIBFLEX
$(DIR)/test_goldstones.x: $(DIR)/test_goldstones.o $(LIBFLEXI) $(LIBLEGACY) $(filter-out -%,$(LOOPFUNCLIBS))
$(CXX) -o $@ $(call abspathx,$^) $(filter -%,$(LOOPFUNCLIBS)) $(BOOSTTESTLIBS) $(GSLLIBS) $(FLIBS)

$(DIR)/test_gsl_vector.x: $(DIR)/test_gsl_vector.o $(LIBFLEXI) $(LIBLEGACY) $(LIBTEST) $(filter-out -%,$(LOOPFUNCLIBS))
$(CXX) -o $@ $(call abspathx,$^) $(filter -%,$(LOOPFUNCLIBS)) $(BOOSTTESTLIBS) $(GSLLIBS) $(FLIBS)

$(DIR)/test_linalg2.x: $(DIR)/test_linalg2.o
$(CXX) -o $@ $(call abspathx,$^) $(BOOSTTESTLIBS) $(LAPACKLIBS) $(BLASLIBS) $(FLIBS)

Expand Down
93 changes: 93 additions & 0 deletions test/test_gsl_vector.cpp
@@ -0,0 +1,93 @@
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE test_gsl_vector

#include <boost/test/unit_test.hpp>

#include "gsl_vector.hpp"

using namespace flexiblesusy;

BOOST_AUTO_TEST_CASE( test_init_default )
{
GSL_vector v;
BOOST_CHECK_EQUAL(v.size(), 0);
}

BOOST_AUTO_TEST_CASE( test_init_0 )
{
GSL_vector v(0);
BOOST_CHECK_EQUAL(v.size(), 0);
}

BOOST_AUTO_TEST_CASE( test_init )
{
GSL_vector v(3);
BOOST_CHECK_EQUAL(v[0], 0.);
BOOST_CHECK_EQUAL(v[1], 0.);
BOOST_CHECK_EQUAL(v[2], 0.);
}

BOOST_AUTO_TEST_CASE( test_operator )
{
GSL_vector v(3);
v[0] = 1.;
v[1] = 2.;
v[2] = 3.;

BOOST_CHECK_EQUAL(v[0], 1.);
BOOST_CHECK_EQUAL(v[1], 2.);
BOOST_CHECK_EQUAL(v[2], 3.);
}

BOOST_AUTO_TEST_CASE( test_copy )
{
GSL_vector v(3);
v[0] = 1.;
v[1] = 2.;
v[2] = 3.;

GSL_vector v2(v);

BOOST_CHECK_EQUAL(v2[0], 1.);
BOOST_CHECK_EQUAL(v2[1], 2.);
BOOST_CHECK_EQUAL(v2[2], 3.);
}

BOOST_AUTO_TEST_CASE( test_assign )
{
GSL_vector v(3);
v[0] = 1.;
v[1] = 2.;
v[2] = 3.;

GSL_vector v2;
v2 = v;

BOOST_CHECK_EQUAL(v2[0], 1.);
BOOST_CHECK_EQUAL(v2[1], 2.);
BOOST_CHECK_EQUAL(v2[2], 3.);
}

BOOST_AUTO_TEST_CASE( test_move )
{
GSL_vector v(3);
v[0] = 1.;
v[1] = 2.;
v[2] = 3.;

GSL_vector v2(std::move(v));

BOOST_CHECK_EQUAL(v2[0], 1.);
BOOST_CHECK_EQUAL(v2[1], 2.);
BOOST_CHECK_EQUAL(v2[2], 3.);
}

BOOST_AUTO_TEST_CASE( test_output )
{
GSL_vector v(3);
v[0] = 1.;
v[1] = 2.;
v[2] = 3.;

BOOST_MESSAGE(v);
}

0 comments on commit 605606e

Please sign in to comment.