433 lines
11 KiB
C++
433 lines
11 KiB
C++
/********************************************************
|
|
* ██████╗ ██████╗████████╗██╗
|
|
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
|
* ██║ ███╗██║ ██║ ██║
|
|
* ██║ ██║██║ ██║ ██║
|
|
* ╚██████╔╝╚██████╗ ██║ ███████╗
|
|
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
|
|
* Geophlatsical Computational Tools & Library (GCTL)
|
|
*
|
|
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
|
*
|
|
* GCTL is degributed under a dual licensing scheme. You can redegribute
|
|
* 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
|
|
* of the License, or (at your option) any later version. You should have
|
|
* received a copy of the GNU Lesser General Public License along with this
|
|
* program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
|
* the GCTL, please consider the option to obtain a commercial license for a
|
|
* fee. These licenses are offered by the GCTL's original author. As a rule,
|
|
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
|
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
|
* to include some description of your company and the realm of its activities.
|
|
* Also add information on how to contact you by electronic and paper mail.
|
|
******************************************************/
|
|
|
|
// linear conjugate gradient solver library
|
|
//#include "lcg/solver.h"
|
|
// GCTL library
|
|
#include "gctl/core.h"
|
|
#include "gctl/geometry.h"
|
|
#include "gctl/io.h"
|
|
#include "gctl/utility.h"
|
|
#include "gctl/optimization.h"
|
|
|
|
#if defined _WINDOWS || __WIN32__
|
|
#include "io.h"
|
|
// Test for file existence
|
|
#define F_OK 0
|
|
#endif
|
|
|
|
using namespace gctl;
|
|
|
|
struct data_point
|
|
{
|
|
double lon, lat;
|
|
std::vector<double> vals;
|
|
};
|
|
|
|
class LBSI : public gctl::lcg_solver
|
|
{
|
|
public:
|
|
LBSI(){}
|
|
virtual ~LBSI(){}
|
|
virtual void LCG_Ax(const array<double> &a, array<double> &b);
|
|
virtual void LCG_Mx(const array<double> &a, array<double> &b){}
|
|
|
|
void Routine(std::string in_name, std::string tar_name,
|
|
std::string out_name, std::string col_str, gctl::text_descriptor &desc,
|
|
unsigned int kernel_size, unsigned int box_size,
|
|
double epsilon);
|
|
void ReadConstrainNodes(std::string filename, gctl::text_descriptor &desc);
|
|
void WriteTargetNodes(std::string filename, const gctl::text_descriptor &desc);
|
|
void InitTargetNodes(std::string para, gctl::text_descriptor &desc);
|
|
void CalKernel();
|
|
void CalKernel(const data_point &tar_node);
|
|
void UpdateTarVec(size_t idx, bool if_global = true);
|
|
void set_kernel_size(unsigned int k){MatSize = k;}
|
|
public:
|
|
array<data_point> ConsNodes;
|
|
array<data_point> TargNodes;
|
|
std::vector<data_point*> LocalNodes;
|
|
boxes_sph<data_point> PntBoxes;
|
|
|
|
double LONmin, LONmax, LATmin, LATmax; // 对输入数据进行归一化处理
|
|
|
|
size_t MatSize, ValSize, MaxCol;
|
|
matrix<double> Kernel;
|
|
array<double> Wgts;
|
|
array<double> MidPdt;
|
|
array<double> B;
|
|
|
|
gctl::_1i_vector col_index;
|
|
};
|
|
|
|
void LBSI::Routine(std::string in_name, std::string tar_name,
|
|
std::string out_name, std::string col_str, gctl::text_descriptor &desc,
|
|
unsigned int kernel_size, unsigned int box_size,
|
|
double epsilon)
|
|
{
|
|
gctl::parse_string_to_vector(col_str, ',', col_index);
|
|
if (col_index.size() < 3)
|
|
{
|
|
throw gctl::runtime_error("Invalid column index. From LBSI::Routine(...)");
|
|
}
|
|
|
|
MaxCol = 0;
|
|
for (size_t i = 0; i < col_index.size(); i++)
|
|
{
|
|
if (MaxCol < col_index[i]) MaxCol = col_index[i];
|
|
}
|
|
|
|
ReadConstrainNodes(in_name, desc);
|
|
InitTargetNodes(tar_name, desc);
|
|
|
|
unsigned int k_size = kernel_size;
|
|
if (k_size <= 1)
|
|
{
|
|
throw gctl::runtime_error("Invalid local size. From LBSI::Routine(...)");
|
|
}
|
|
|
|
// Throw errors only
|
|
set_lcg_message(LCG_THROW);
|
|
|
|
if (k_size >= ConsNodes.size())
|
|
{
|
|
GCTL_ShowWhatError("The local size is equal to or bigger than the input node's size. Reduced to the global algorithm.", GCTL_WARNING_ERROR, 0, 0, 0);
|
|
|
|
k_size = ConsNodes.size();
|
|
set_kernel_size(k_size);
|
|
Kernel.resize(MatSize, MatSize);
|
|
Wgts.resize(MatSize);
|
|
MidPdt.resize(MatSize);
|
|
B.resize(MatSize);
|
|
|
|
lcg_para my_para = default_lcg_para();
|
|
//my_para.max_iterations = 1000;
|
|
my_para.epsilon = epsilon;
|
|
set_lcg_para(my_para);
|
|
|
|
CalKernel();
|
|
|
|
for (size_t s = 0; s < ValSize; s++)
|
|
{
|
|
UpdateTarVec(s, true);
|
|
|
|
Wgts.assign(0.0);
|
|
lcg(Wgts, B);
|
|
|
|
double deg, sum;
|
|
for (int i = 0; i < TargNodes.size(); ++i)
|
|
{
|
|
sum = 0.0;
|
|
for (int j = 0; j < MatSize; ++j)
|
|
{
|
|
deg = PntBoxes.spherical_angle(TargNodes[i].lon, TargNodes[i].lat, ConsNodes[j].lon, ConsNodes[j].lat);
|
|
|
|
if (deg >= GCTL_ZERO)
|
|
{
|
|
sum += deg*deg*(log(deg)-1.0)*Wgts[j];
|
|
}
|
|
}
|
|
TargNodes[i].vals[s] = sum;
|
|
}
|
|
}
|
|
|
|
WriteTargetNodes(out_name, desc);
|
|
return;
|
|
}
|
|
|
|
set_kernel_size(k_size);
|
|
LocalNodes.resize(MatSize);
|
|
Kernel.resize(MatSize, MatSize);
|
|
Wgts.resize(MatSize);
|
|
MidPdt.resize(MatSize);
|
|
B.resize(MatSize);
|
|
|
|
lcg_para my_para = default_lcg_para();
|
|
my_para.epsilon = epsilon;
|
|
set_lcg_para(my_para);
|
|
|
|
gctl::array<double> lons(ConsNodes.size());
|
|
gctl::array<double> lats(ConsNodes.size());
|
|
for (int i = 0; i < ConsNodes.size(); ++i)
|
|
{
|
|
lons[i] = ConsNodes[i].lon;
|
|
lats[i] = ConsNodes[i].lat;
|
|
}
|
|
|
|
PntBoxes.init(lons, lats, ConsNodes, box_size, box_size);
|
|
|
|
double deg, sum;
|
|
progress_bar bar(TargNodes.size());
|
|
for (int i = 0; i < TargNodes.size(); ++i)
|
|
{
|
|
bar.progressed(i);
|
|
|
|
Kernel.assign_all(0.0);
|
|
CalKernel(TargNodes[i]);
|
|
|
|
for (size_t s = 0; s < ValSize; s++)
|
|
{
|
|
UpdateTarVec(s, false);
|
|
|
|
Wgts.assign(0.0);
|
|
lcg(Wgts, B);
|
|
|
|
sum = 0.0;
|
|
for (int j = 0; j < MatSize; ++j)
|
|
{
|
|
deg = PntBoxes.spherical_angle(TargNodes[i].lon, TargNodes[i].lat, LocalNodes[j]->lon, LocalNodes[j]->lat);
|
|
|
|
if (deg >= GCTL_ZERO)
|
|
{
|
|
sum += deg*deg*(log(deg)-1.0)*Wgts[j];
|
|
}
|
|
}
|
|
TargNodes[i].vals[s] = sum;
|
|
}
|
|
}
|
|
|
|
WriteTargetNodes(out_name, desc);
|
|
return;
|
|
}
|
|
|
|
void LBSI::ReadConstrainNodes(std::string filename, gctl::text_descriptor &desc)
|
|
{
|
|
gctl::_2d_vector table_data;
|
|
desc.file_name_ = filename;
|
|
gctl::read_text2vector2d(desc, table_data);
|
|
|
|
if (table_data.size() <= 1)
|
|
{
|
|
throw gctl::runtime_error("Not enough constraint points. From LBSI::ReadConstrainNodes(...)");
|
|
}
|
|
|
|
if (table_data[0].size() - 1 < MaxCol)
|
|
{
|
|
throw gctl::runtime_error("Invalid constraint point format. From LBSI::ReadConstrainNodes(...)");
|
|
}
|
|
|
|
LONmin = LATmin = 1e+30;
|
|
LONmax = LATmax = -1e+30;
|
|
|
|
ValSize = col_index.size() - 2;
|
|
ConsNodes.resize(table_data.size());
|
|
for (size_t i = 0; i < table_data.size(); ++i)
|
|
{
|
|
ConsNodes[i].lon = table_data[i][col_index[0]];
|
|
ConsNodes[i].lat = table_data[i][col_index[1]];
|
|
LONmin = std::min(LONmin, ConsNodes[i].lon);
|
|
LONmax = std::max(LONmax, ConsNodes[i].lon);
|
|
LATmin = std::min(LATmin, ConsNodes[i].lat);
|
|
LATmax = std::max(LATmax, ConsNodes[i].lat);
|
|
|
|
for (size_t j = 0; j < ValSize; j++)
|
|
{
|
|
ConsNodes[i].vals.push_back(table_data[i][col_index[2+j]]);
|
|
}
|
|
}
|
|
|
|
for (size_t i = 0; i < ConsNodes.size(); i++)
|
|
{
|
|
ConsNodes[i].lon = (ConsNodes[i].lon - LONmin)/(LONmax - LONmin);
|
|
ConsNodes[i].lat = (ConsNodes[i].lat - LATmin)/(LATmax - LATmin);
|
|
}
|
|
|
|
destroy_vector(table_data);
|
|
return;
|
|
}
|
|
|
|
void LBSI::WriteTargetNodes(std::string filename, const gctl::text_descriptor &desc)
|
|
{
|
|
std::ofstream outfile;
|
|
gctl::open_outfile(outfile, filename, ".txt");
|
|
|
|
for (size_t i = 0; i < desc.head_num_; i++)
|
|
{
|
|
outfile << desc.head_strs_[i] << "\n";
|
|
}
|
|
|
|
for (size_t i = 0; i < TargNodes.size(); i++)
|
|
{
|
|
TargNodes[i].lon = TargNodes[i].lon*(LONmax - LONmin) + LONmin;
|
|
TargNodes[i].lat = TargNodes[i].lat*(LATmax - LATmin) + LATmin;
|
|
|
|
outfile << TargNodes[i].lon << " " << TargNodes[i].lat << " " << std::setprecision(12);
|
|
for (size_t j = 0; j < ValSize; j++)
|
|
{
|
|
outfile << TargNodes[i].vals[j] << " ";
|
|
}
|
|
outfile << std::endl;
|
|
}
|
|
|
|
outfile.close();
|
|
return;
|
|
}
|
|
|
|
void LBSI::InitTargetNodes(std::string para, gctl::text_descriptor &desc)
|
|
{
|
|
// try to use the para as a file name
|
|
if (access(para.c_str(), F_OK) != -1)
|
|
{
|
|
std::vector<point2dc> tmp_vec;
|
|
desc.file_name_ = para;
|
|
read_text2vector(desc, tmp_vec);
|
|
|
|
TargNodes.resize(tmp_vec.size());
|
|
for (size_t i = 0; i < tmp_vec.size(); ++i)
|
|
{
|
|
TargNodes[i].lon = tmp_vec[i].x;
|
|
TargNodes[i].lat = tmp_vec[i].y;
|
|
|
|
TargNodes[i].lon = (TargNodes[i].lon - LONmin)/(LONmax - LONmin);
|
|
TargNodes[i].lat = (TargNodes[i].lat - LATmin)/(LATmax - LATmin);
|
|
}
|
|
|
|
for (size_t i = 0; i < TargNodes.size(); i++)
|
|
{
|
|
TargNodes[i].vals.resize(ValSize);
|
|
}
|
|
|
|
destroy_vector(tmp_vec);
|
|
return;
|
|
}
|
|
|
|
double dx, dy, lonmin, lonmax, latmin, latmax;
|
|
parse_string_to_value(para, '/', true, lonmin, dx, lonmax, latmin, dy, latmax);
|
|
|
|
size_t M = floor((latmax - latmin)/dy) + 1;
|
|
size_t N = floor((lonmax - lonmin)/dx) + 1;
|
|
|
|
TargNodes.resize(M*N);
|
|
for (size_t j = 0; j < M; j++)
|
|
{
|
|
for (size_t i = 0; i < N; i++)
|
|
{
|
|
TargNodes[i + j*N].lon = lonmin + dx*i;
|
|
TargNodes[i + j*N].lat = latmin + dy*j;
|
|
|
|
TargNodes[i + j*N].lon = (TargNodes[i + j*N].lon - LONmin)/(LONmax - LONmin);
|
|
TargNodes[i + j*N].lat = (TargNodes[i + j*N].lat - LATmin)/(LATmax - LATmin);
|
|
}
|
|
}
|
|
|
|
for (size_t i = 0; i < TargNodes.size(); i++)
|
|
{
|
|
TargNodes[i].vals.resize(ValSize);
|
|
}
|
|
return;
|
|
}
|
|
|
|
void LBSI::CalKernel()
|
|
{
|
|
// 计算出所有成对的格林函数值
|
|
double deg;
|
|
for (int j = 0; j < MatSize-1; ++j)
|
|
{
|
|
for (int k = j+1; k < MatSize; ++k)
|
|
{
|
|
deg = sqrt((ConsNodes[j].lon - ConsNodes[k].lon)*(ConsNodes[j].lon - ConsNodes[k].lon)
|
|
+ (ConsNodes[j].lat - ConsNodes[k].lat)*(ConsNodes[j].lat - ConsNodes[k].lat));
|
|
|
|
if (deg >= GCTL_ZERO)
|
|
{
|
|
Kernel[j][k] = Kernel[k][j] = deg*deg*(log(deg)-1.0);
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
void LBSI::CalKernel(const data_point &tar_node)
|
|
{
|
|
// 找出距离tar_node最近的一组控制点
|
|
PntBoxes.get_by_number(tar_node.lon, tar_node.lat, MatSize, LocalNodes);
|
|
|
|
// 计算出所有成对的格林函数值
|
|
double deg;
|
|
for (int j = 0; j < MatSize-1; ++j)
|
|
{
|
|
for (int k = j+1; k < MatSize; ++k)
|
|
{
|
|
deg = PntBoxes.spherical_angle(LocalNodes[j]->lon, LocalNodes[j]->lat, LocalNodes[k]->lon, LocalNodes[k]->lat);
|
|
|
|
if (deg >= GCTL_ZERO)
|
|
{
|
|
Kernel[j][k] = Kernel[k][j] = deg*deg*(log(deg)-1.0);
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
void LBSI::UpdateTarVec(size_t idx, bool if_global)
|
|
{
|
|
if (if_global)
|
|
{
|
|
for (int i = 0; i < MatSize; ++i)
|
|
{
|
|
B[i] = 0;
|
|
for (int j = 0; j < MatSize; ++j)
|
|
{
|
|
B[i] += Kernel[j][i] * ConsNodes[j].vals[idx];
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < MatSize; ++i)
|
|
{
|
|
B[i] = 0;
|
|
for (int j = 0; j < MatSize; ++j)
|
|
{
|
|
B[i] += Kernel[j][i] * LocalNodes[j]->vals[idx];
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
void LBSI::LCG_Ax(const array<double> &a, array<double> &b)
|
|
{
|
|
for (int i = 0; i < MatSize; ++i)
|
|
{
|
|
MidPdt[i] = 0;
|
|
for (int j = 0; j < MatSize; ++j)
|
|
{
|
|
MidPdt[i] += a[j] * Kernel[i][j];
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < MatSize; ++i)
|
|
{
|
|
b[i] = 0;
|
|
for (int j = 0; j < MatSize; ++j)
|
|
{
|
|
b[i] += MidPdt[j] * Kernel[j][i];
|
|
}
|
|
}
|
|
return;
|
|
} |