|
| 1 | +/* *************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (C) 2013-2016 University of Dundee |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * This file is part of SAMoS (Soft Active Matter on Surfaces) program. |
| 7 | + * |
| 8 | + * SAMoS is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * SAMoS is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + * ****************************************************************************/ |
| 22 | + |
| 23 | +/*! |
| 24 | + * \file external_piv_aligner.hpp |
| 25 | + * \author Rastko Sknepnek, [email protected] |
| 26 | + * \date 31-Jan-2020 |
| 27 | + * \brief Declaration of ExternalPIVAlign class |
| 28 | + */ |
| 29 | + |
| 30 | +#ifndef __EXTERNAL_PIV_ALIGN_HPP__ |
| 31 | +#define __EXTERNAL_PIV_ALIGN_HPP__ |
| 32 | + |
| 33 | +#include <cmath> |
| 34 | +#include <vector> |
| 35 | +#include <string> |
| 36 | +#include <sstream> |
| 37 | + |
| 38 | +#include <boost/filesystem.hpp> |
| 39 | + |
| 40 | +#include "external_aligner.hpp" |
| 41 | + |
| 42 | +using std::istringstream; |
| 43 | +using std::make_pair; |
| 44 | +using std::sqrt; |
| 45 | +using std::string; |
| 46 | +using std::vector; |
| 47 | +using namespace boost::filesystem; |
| 48 | + |
| 49 | +//! Structure that handles parameters for the polar alignment |
| 50 | +struct PIVData |
| 51 | +{ |
| 52 | + vector<vector<double>> piv_data; |
| 53 | +}; |
| 54 | + |
| 55 | +struct PIVAlignParameters |
| 56 | +{ |
| 57 | + double gamma; // Alignment strength |
| 58 | +} |
| 59 | + |
| 60 | +/*! ExternalPIVAlign implements the alignment to the velocity field obtained from |
| 61 | + * experiments in embryos. Given a local vector vector \f$ \vec v_i \f$. Torque on particle \f$ i \f$ is then |
| 62 | + * \f$ \vec \tau_i = \gamma \vec v_i \times \vec n_i \f$, where \f$ \gamma \f$ is a constant. We note that |
| 63 | + * valued of \f$ \vec v_i \f$ are interpolated between two consecutive time steps (current and the one ahead). |
| 64 | + * All cells are binned into boxes (given by the PIV resolution). Each cell is aligned to the vector \f$ \vec v_i \f$ |
| 65 | + * of the box it falls into. |
| 66 | + * \note: One could do a more sophisticaed spatial and temporal interpolation. |
| 67 | + */ |
| 68 | +class ExternalPIVAlign : public ExternalAlign |
| 69 | +{ |
| 70 | +public: |
| 71 | + |
| 72 | + //! Constructor |
| 73 | + //! \param sys Pointer to the System object |
| 74 | + //! \param msg Pointer to the internal state messenger |
| 75 | + //! \param param Contains information about all parameters |
| 76 | + ExternalPIVAlign(SystemPtr sys, MessengerPtr msg, pairs_type& param) : ExternalAlign(sys,msg,param) |
| 77 | + { |
| 78 | + int ntypes = m_system->get_ntypes(); |
| 79 | + if (param.find("xfile") == param.end()) |
| 80 | + { |
| 81 | + m_msg->msg(Messenger::ERROR,"External PIV aligner. No x component of file external PIV given."); |
| 82 | + throw runtime_error("No x component file provided for the external PIV alignment."); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + path p(param["xfile"]); |
| 87 | + if (exists(p)) |
| 88 | + { |
| 89 | + this->__read_csv(param["xfile"], m_x); |
| 90 | + } |
| 91 | + else |
| 92 | + throw runtime_error("File " + param["xfile"] + " does not exist."); |
| 93 | + } |
| 94 | + if (param.find("hx") == param.end()) |
| 95 | + { |
| 96 | + m_msg->msg(Messenger::WARNING,"External PIV aligner. No x component of external PIV given. Assuming 0."); |
| 97 | + m_hx = 0.0; |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + m_msg->msg(Messenger::INFO,"External PIV aligner. x component of the external PIV set to "+param["hx"]+"."); |
| 102 | + m_hx = lexical_cast<double>(param["hx"]); |
| 103 | + } |
| 104 | + m_msg->write_config("aligner.external.PIV.hx",lexical_cast<string>(m_hx)); |
| 105 | + if (param.find("hy") == param.end()) |
| 106 | + { |
| 107 | + m_msg->msg(Messenger::WARNING,"External PIV aligner. No y component of external PIV given. Assuming 0."); |
| 108 | + m_hy = 0.0; |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + m_msg->msg(Messenger::INFO,"External PIV aligner. y component of the external PIV set to "+param["hy"]+"."); |
| 113 | + m_hy = lexical_cast<double>(param["hy"]); |
| 114 | + } |
| 115 | + m_msg->write_config("aligner.external.PIV.hy",lexical_cast<string>(m_hy)); |
| 116 | + if (param.find("hz") == param.end()) |
| 117 | + { |
| 118 | + m_msg->msg(Messenger::WARNING,"External PIV aligner. No z component of external PIV given. Assuming 0."); |
| 119 | + m_hz = 0.0; |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + m_msg->msg(Messenger::INFO,"External PIV aligner. z component of the external PIV set to "+param["hz"]+"."); |
| 124 | + m_hz = lexical_cast<double>(param["hz"]); |
| 125 | + } |
| 126 | + m_msg->write_config("aligner.external.PIV.hz",lexical_cast<string>(m_hz)); |
| 127 | + |
| 128 | + m_type_params = new PIVAlignParameters[ntypes]; |
| 129 | + for (int i = 0; i < ntypes; i++) |
| 130 | + { |
| 131 | + m_type_params[i].gamma = m_gamma; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + virtual ~ExternalPIVAlign() |
| 136 | + { |
| 137 | + delete [] m_type_params; |
| 138 | + } |
| 139 | + |
| 140 | + //! Set pair parameters data for self alignment |
| 141 | + void set_parameters(pairs_type& self_param) |
| 142 | + { |
| 143 | + map<string,double> param; |
| 144 | + int type; |
| 145 | + |
| 146 | + if (self_param.find("type") == self_param.end()) |
| 147 | + { |
| 148 | + m_msg->msg(Messenger::ERROR,"Type has not been defined for external PIV alignment."); |
| 149 | + throw runtime_error("Missing key for external alignment parameters."); |
| 150 | + } |
| 151 | + |
| 152 | + type = lexical_cast<int>(self_param["type"]); |
| 153 | + |
| 154 | + |
| 155 | + if (self_param.find("hx") != self_param.end()) |
| 156 | + { |
| 157 | + m_msg->msg(Messenger::INFO,"External PIV aligner. Setting x component of external filed to "+self_param["hx"]+" for particle pair of type ("+lexical_cast<string>(type)+")."); |
| 158 | + param["hx"] = lexical_cast<double>(self_param["hx"]); |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + m_msg->msg(Messenger::INFO,"External PIV aligner. Using default x component of external filed ("+lexical_cast<string>(m_hx)+") for particle pair of types ("+lexical_cast<string>(type)+")"); |
| 163 | + param["hx"] = m_hx; |
| 164 | + } |
| 165 | + m_msg->write_config("aligner.external.PIV.type."+self_param["type"]+".hx",lexical_cast<string>(param["hx"])); |
| 166 | + if (self_param.find("hy") != self_param.end()) |
| 167 | + { |
| 168 | + m_msg->msg(Messenger::INFO,"External PIV aligner. Setting y component of external filed to "+self_param["hy"]+" for particle pair of type ("+lexical_cast<string>(type)+")."); |
| 169 | + param["hy"] = lexical_cast<double>(self_param["hy"]); |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + m_msg->msg(Messenger::INFO,"External PIV aligner. Using default y component of external filed ("+lexical_cast<string>(m_hy)+") for particle pair of types ("+lexical_cast<string>(type)+")"); |
| 174 | + param["hy"] = m_hy; |
| 175 | + } |
| 176 | + m_msg->write_config("aligner.external.PIV.type."+self_param["type"]+".hy",lexical_cast<string>(param["hy"])); |
| 177 | + if (self_param.find("hz") != self_param.end()) |
| 178 | + { |
| 179 | + m_msg->msg(Messenger::INFO,"External PIV aligner. Setting z component of external filed to "+self_param["hz"]+" for particle pair of type ("+lexical_cast<string>(type)+")."); |
| 180 | + param["hz"] = lexical_cast<double>(self_param["hz"]); |
| 181 | + } |
| 182 | + else |
| 183 | + { |
| 184 | + m_msg->msg(Messenger::INFO,"External PIV aligner. Using default z component of external filed ("+lexical_cast<string>(m_hz)+") for particle pair of types ("+lexical_cast<string>(type)+")"); |
| 185 | + param["hz"] = m_hz; |
| 186 | + } |
| 187 | + m_msg->write_config("aligner.external.PIV.type."+self_param["type"]+".hz",lexical_cast<string>(param["hz"])); |
| 188 | + |
| 189 | + m_type_params[type-1].hx = param["hx"]; |
| 190 | + m_type_params[type-1].hy = param["hy"]; |
| 191 | + m_type_params[type-1].hz = param["hz"]; |
| 192 | + |
| 193 | + m_has_params = true; |
| 194 | + } |
| 195 | + |
| 196 | + |
| 197 | + //! Computes "torques" |
| 198 | + void compute(); |
| 199 | + |
| 200 | + |
| 201 | +private: |
| 202 | + |
| 203 | + PIVData m_x; // x components of the grid |
| 204 | + PIVData m_y; // y components of the grid |
| 205 | + vector<PIVData> m_u; // Holds x components of the velocity over time |
| 206 | + vector<PIVData> m_v; // Holds y components of the velocity over time |
| 207 | + int m_max_time; // Total number of time steps |
| 208 | + int m_step_gap; // Number of steps between two consecutive PIV snapshots |
| 209 | + double m_gamma; // Alignment strength |
| 210 | + |
| 211 | + void __read_csv(const string &, PIVData &); |
| 212 | +}; |
| 213 | + |
| 214 | +typedef shared_ptr<ExternalPIVAlign> ExternalPIVAlignPtr; |
| 215 | + |
| 216 | +#endif |
| 217 | + |
0 commit comments