#include <deal.II/base/config.h>
#include <deal.II/base/utilities.h>
#include <deal.II/base/exceptions.h>
+#include <boost/serialization/vector.hpp>
#include <vector>
#include <algorithm>
#include <deal.II/base/index_set.h>
#include <deal.II/lac/read_write_vector.h>
#include <deal.II/lac/vector_space_vector.h>
+#include <boost/serialization/array.hpp>
+#include <boost/serialization/split_member.hpp>
#include <cstdio>
#include <iostream>
*/
virtual ~Vector();
+ /**
+ * Copies the data of the input vector @p in_vector.
+ */
+ Vector<Number> &operator= (const Vector<Number> &in_vector);
+
+ /**
+ * Copies the data of the input vector @p in_vector.
+ */
+ template <typename Number2>
+ Vector<Number> &operator= (const Vector<Number2> &in_vector);
+
+ /**
+ * Sets all elements of the vector to the scalar @p s. This operation is
+ * only allowed if @p s is equal to zero.
+ */
+ Vector<Number> &operator= (const Number s);
+
/**
* Multiply the entire vector by a fixed factor.
*/
*/
virtual Number operator* (const VectorSpaceVector<Number> &V);
+ /**
+ * Add @p a to all components. Note that @p a is a scalar not a vector.
+ */
+ virtual void add(const Number a);
+
/**
* Simple addition of a multiple of a vector, i.e. <tt>*this += a*V</tt>.
*/
const unsigned int precision=3,
const bool scientific=true,
const bool across=true) const;
+ /**
+ * Write the vector en bloc to a file. This is done in a binary mode, so the
+ * output is neither readable by humans nor (probably) by other computers
+ * using a different operating system or number format.
+ */
+ void block_write (std::ostream &out) const;
+
+ /**
+ * Read a vector en block from a file. This is done using the inverse
+ * operations to the above function, so it is reasonably fast because the
+ * bitstream is not interpreted.
+ *
+ * The vector is resized if necessary.
+ *
+ * A primitive form of error checking is performed which will recognize the
+ * bluntest attempts to interpret some data as a vector stored bitwise to a
+ * file, but not more.
+ */
+ void block_read (std::istream &in);
/**
* Returns the memory consumption of this class in bytes.
typename VectorSpaceVector<Number>::real_type l2_norm_squared_recursive(
unsigned int i,
unsigned int j);
+
+ /**
+ * Serialize the data of this object using boost. This function is necessary
+ * to use boost::archive::text_iarchive and boost::archive::text_oarchive.
+ */
+ template <typename Archive>
+ void serialize(Archive &ar, const unsigned int version);
+
+ friend class boost::serialization::access;
};
/*@}*/
+ template <typename Number>
+ inline
+ Vector<Number> &Vector<Number>::operator= (const Vector<Number> &in_vector)
+ {
+ this->reinit(in_vector.size(),true);
+ std::copy(in_vector.begin(), in_vector.end(), this->begin());
+
+ return *this;
+ }
+
+
+
+ template <typename Number>
+ template <typename Number2>
+ inline
+ Vector<Number> &Vector<Number>::operator= (const Vector<Number2> &in_vector)
+ {
+ this->reinit(in_vector.size(in_vector.size()),true);
+ std::copy(in_vector.begin(), in_vector.end(), this->begin());
+
+ return *this;
+ }
+
+
+
+ template <typename Number>
+ inline
+ Vector<Number> &Vector<Number>::operator= (const Number s)
+ {
+ Assert(s==static_cast<Number>(0), ExcMessage("Only 0 can be assigned to a vector."));
+ (void) s;
+
+ std::fill(this->begin(),this->end(),Number());
+
+ return *this;
+ }
+
+
+
+ template <typename Number>
+ inline
+ void Vector<Number>::add(const Number a)
+ {
+ AssertIsFinite(a);
+ for (unsigned int i=0; i<this->size(); ++i)
+ this->val[i] += a;
+ }
+
+
+
template <typename Number>
inline
typename Vector<Number>::size_type Vector<Number>::size() const
+ template <typename Number>
+ template <typename Archive>
+ inline
+ void Vector<Number>::serialize(Archive &ar, const unsigned int)
+ {
+ unsigned int current_size = this->size();
+ ar &static_cast<Subscriptor &>(*this);
+ ar &this->stored_elements;
+ // If necessary, resize the vector during a read operation
+ if (this->size()!=current_size)
+ this->reinit(this->size());
+ ar &boost::serialization::make_array(this->val,this->size());
+ }
+
+
+
template <typename Number>
inline
std::size_t Vector<Number>::memory_consumption() const
#define dealii__la_vector_templates_h
#include <deal.II/lac/la_vector.h>
+#include <iostream>
+#include <iomanip>
DEAL_II_NAMESPACE_OPEN
return norm;
}
+
+
+
+ template <typename Number>
+ void Vector<Number>::block_write(std::ostream &out) const
+ {
+ AssertThrow(out, ExcIO());
+
+ // Other version of the following
+ // out << size() << std::endl << '[';
+ // Reason: operator<< seems to use some resources that lead to problems in
+ // a multithreaded environment.
+ const size_type sz = this->size();
+ char buf[16];
+#ifdef DEAL_II_WITH_64BIT_INDICES
+ std::sprintf(buf, "%llu", sz);
+#else
+ std::sprintf(buf, "%u", sz);
+#endif
+ std::strcat(buf, "\n[");
+
+ out.write(buf, std::strlen(buf));
+ out.write(reinterpret_cast<const char *>(this->begin()),
+ reinterpret_cast<const char *>(this->end())
+ - reinterpret_cast<const char *>(this->begin()));
+
+ // out << ']';
+ const char outro = ']';
+ out.write(&outro, 1);
+
+ AssertThrow(out, ExcIO());
+ }
+
+
+
+ template <typename Number>
+ void Vector<Number>::block_read(std::istream &in)
+ {
+ AssertThrow(in, ExcIO());
+
+ size_type sz;
+
+ char buf[16];
+
+ in.getline(buf,16,'\n');
+ sz = std::atoi(buf);
+ this->reinit(sz,true);
+
+ char c;
+ // in >> c;
+ in.read(&c,1);
+ AssertThrow(c=='[', ExcIO());
+
+ in.read(reinterpret_cast<char *>(this->begin()),
+ reinterpret_cast<const char *>(this->end())
+ - reinterpret_cast<const char *>(this->begin()));
+
+ // in >> c;
+ in.read(&c,1);
+ AssertThrow(c=']', ExcIO());
+ }
}
DEAL_II_NAMESPACE_CLOSE
resize_val(size);
stored_elements = complete_index_set(size);
+ stored_elements.compress();
// set entries to zero if so requested
if (omit_zeroing_entries == false)
*/
virtual Number operator* (const VectorSpaceVector<Number> &V) = 0;
+ /**
+ * Add @p a to all components. Note that @p a is a scalar not a vector.
+ */
+ virtual void add(const Number a) = 0;
+
/**
* Simple addition of a multiple of a vector, i.e. <tt>*this += a*V</tt>.
*/
{
const unsigned int size = 17 + test*1101;
LinearAlgebra::Vector<number> v1 (size), v2(size), v3(size), check(size);
+ // Check that the assignment works
+ v1 = 0.;
for (unsigned int i=0; i<size; ++i)
{
- v1(i) = 0.1 + 0.005 * i;
- v2(i) = -5.2 + 0.18 * i;
- v3(i) = 3.14159 + 2.7183/(1.+i);
+ v1[i] = 0.1 + 0.005 * i;
+ v2[i] = -5.2 + 0.18 * i;
+ v3[i] = 3.14159 + 2.7183/(1.+i);
}
check = v1;
const number factor = 0.01432;
for (unsigned int i=0; i<size; ++i)
deallog << check[i] << " ";
deallog << std::endl;
+
+ const number constant = 1.;
+ v1.add(constant);
+ deallog << "Vector add constant: ";
+ for (unsigned int i=0; i<size; ++i)
+ deallog << v1[i] << " ";
+ deallog << std::endl;
}
deallog << "Add and dot should be " << prod/static_cast<number>(size)
DEAL::Add and dot should be 52.71, is 52.71
DEAL::Vector add reference: 0.03 0.03 0.04 0.05 0.06 0.06 0.07 0.08 0.09 0.09 0.10 0.11 0.12 0.12 0.13 0.14 0.15
DEAL::Vector check reference: 0.03 0.03 0.04 0.05 0.06 0.06 0.07 0.08 0.09 0.09 0.10 0.11 0.12 0.12 0.13 0.14 0.15
+DEAL::Vector add constant: 1.03 1.03 1.04 1.05 1.06 1.06 1.07 1.08 1.09 1.09 1.10 1.11 1.12 1.12 1.13 1.14 1.15
DEAL::Add and dot should be 0.30, is 0.30
DEAL::Add and dot should be 13.40, is 13.40
DEAL::Add and dot should be 26.50, is 26.50
skip += 17;
LinearAlgebra::Vector<number> vec(size);
for (unsigned int i=0; i<size; ++i)
- vec(i) = i+1;
+ vec[i] = i+1;
const number l1_norm = vec.l1_norm();
AssertThrow (std::abs(l1_norm-0.5*size*(size+1)) < acc*0.5*size*(size+1),
template <typename number>
void check_complex_norms ()
{
- const number acc = 1e1*std::numeric_limits<number>::epsilon();
+ const number acc = 1e2*std::numeric_limits<number>::epsilon();
unsigned int skip = 73;
for (unsigned int size=1; size<100000; size+=skip)
{
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2012 - 2015 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <deal.II/lac/la_vector.h>
+#include <fstream>
+#include <string>
+#include <boost/archive/text_oarchive.hpp>
+#include <boost/archive/text_iarchive.hpp>
+
+
+void test()
+{
+ std::string filename("test.txt");
+ const unsigned int size(10);
+ LinearAlgebra::Vector<double> vec(size);
+ for (unsigned int i=0; i<size; ++i)
+ vec[i] = i;
+
+ // Check block_write and block_read
+ std::ofstream file_out(filename);
+ // Write the vector in the file
+ vec.block_write(file_out);
+ file_out.close();
+ // Clear the vector
+ vec.reinit(0);
+ // Load the vector form the file
+ std::ifstream file_in(filename);
+ vec.block_read(file_in);
+ file_in.close();
+ // Check the vector
+ double eps=1e-12;
+ for (unsigned int i=0; i<size; ++i)
+ AssertThrow(std::abs(vec[i]-(double)i)<eps,
+ ExcMessage("Value in the vector has been changed by block_write or block_read"));
+
+
+ // save data to archive
+ {
+ std::ofstream file_out2(filename);
+ boost::archive::text_oarchive oa(file_out2);
+ oa<<vec;
+ // archive and stream closed when destructors are called
+ }
+
+ // Clear the vector
+ vec.reinit(0);
+ {
+ std::ifstream file_in2(filename);
+ boost::archive::text_iarchive ia(file_in2);
+ ia >> vec;
+ }
+ // Check the vector
+ for (unsigned int i=0; i<size; ++i)
+ AssertThrow(std::abs(vec[i]-(double)i)<eps,
+ ExcMessage("Value in the vector has been changed by boost archive"));
+}
+
+
+int main()
+{
+ std::ofstream logfile("output");
+ deallog << std::fixed;
+ deallog << std::setprecision(2);
+ deallog.attach(logfile);
+ deallog.depth_console(0);
+ deallog.threshold_double(1.e-10);
+
+ test();
+
+ deallog << "OK" << std::endl;
+}
+
+
--- /dev/null
+
+DEAL::OK