451 lines
12 KiB
C++
451 lines
12 KiB
C++
/********************************************************
|
|
* ██████╗ ██████╗████████╗██╗
|
|
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
|
* ██║ ███╗██║ ██║ ██║
|
|
* ██║ ██║██║ ██║ ██║
|
|
* ╚██████╔╝╚██████╗ ██║ ███████╗
|
|
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
|
|
* Geophysical Computational Tools & Library (GCTL)
|
|
*
|
|
* Copyright (c) 2022 Yi Zhang (yizhang-geo@zju.edu.cn)
|
|
*
|
|
* GCTL is distributed under a dual licensing scheme. You can 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
|
|
* 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
|
|
{
|
|
point2dc loc;
|
|
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;
|
|
boxes2d<data_point> PntBoxes;
|
|
|
|
double Xmin, Xmax, Ymin, Ymax; // 对输入数据进行归一化处理
|
|
|
|
size_t MatSize, ValSize, MaxCol;
|
|
matrix<double> Kernel;
|
|
array<double> Wgts;
|
|
array<double> MidPdt;
|
|
array<double> B;
|
|
|
|
//array<double> GK;
|
|
//array<double> DK;
|
|
//array<double> ADK;
|
|
|
|
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);
|
|
//GK.resize(MatSize);
|
|
//DK.resize(MatSize);
|
|
//ADK.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);
|
|
// run the inversion process in factory mode
|
|
//lcg(_AxProduct, nullptr, Wgts.get(), B.get(), MatSize, &my_para, this, GK.get(), DK.get(), ADK.get());
|
|
lcg(Wgts, B);
|
|
|
|
double dist, sum;
|
|
for (int i = 0; i < TargNodes.size(); ++i)
|
|
{
|
|
sum = 0.0;
|
|
for (int j = 0; j < MatSize; ++j)
|
|
{
|
|
dist = sqrt((TargNodes[i].loc.x - ConsNodes[j].loc.x)*(TargNodes[i].loc.x - ConsNodes[j].loc.x)
|
|
+ (TargNodes[i].loc.y - ConsNodes[j].loc.y)*(TargNodes[i].loc.y - ConsNodes[j].loc.y));
|
|
|
|
if (dist >= GCTL_ZERO)
|
|
{
|
|
sum += dist*dist*(log(dist)-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);
|
|
//GK.resize(MatSize);
|
|
//DK.resize(MatSize);
|
|
//ADK.resize(MatSize);
|
|
|
|
lcg_para my_para = default_lcg_para();
|
|
//my_para.max_iterations = 1000;
|
|
my_para.epsilon = epsilon;
|
|
set_lcg_para(my_para);
|
|
|
|
gctl::array<double> xs(ConsNodes.size());
|
|
gctl::array<double> ys(ConsNodes.size());
|
|
for (int i = 0; i < ConsNodes.size(); ++i)
|
|
{
|
|
xs[i] = ConsNodes[i].loc.x;
|
|
ys[i] = ConsNodes[i].loc.y;
|
|
}
|
|
|
|
PntBoxes.init(xs, ys, ConsNodes, box_size, box_size);
|
|
|
|
double dist, 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);
|
|
// run the inversion process in factory mode
|
|
//lcg(_AxProduct, nullptr, Wgts.get(), B.get(), MatSize, &my_para, this, GK.get(), DK.get(), ADK.get());
|
|
lcg(Wgts, B);
|
|
|
|
sum = 0.0;
|
|
for (int j = 0; j < MatSize; ++j)
|
|
{
|
|
dist = sqrt((TargNodes[i].loc.x - LocalNodes[j]->loc.x)*(TargNodes[i].loc.x - LocalNodes[j]->loc.x)
|
|
+ (TargNodes[i].loc.y - LocalNodes[j]->loc.y)*(TargNodes[i].loc.y - LocalNodes[j]->loc.y));
|
|
|
|
if (dist >= GCTL_ZERO)
|
|
{
|
|
sum += dist*dist*(log(dist)-1.0)*Wgts[j];
|
|
}
|
|
}
|
|
TargNodes[i].vals[s] = sum;
|
|
}
|
|
}
|
|
|
|
WriteTargetNodes(out_name, desc);
|
|
return;
|
|
}
|
|
|
|
void LBSI::ReadConstrainNodes(std::string filename, gctl::text_descriptor &desc)
|
|
{
|
|
desc.file_name_ = filename;
|
|
gctl::_2d_vector table_data;
|
|
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(...)");
|
|
}
|
|
|
|
Xmin = Ymin = 1e+30;
|
|
Xmax = Ymax = -1e+30;
|
|
|
|
ValSize = col_index.size() - 2;
|
|
ConsNodes.resize(table_data.size());
|
|
for (size_t i = 0; i < table_data.size(); ++i)
|
|
{
|
|
ConsNodes[i].loc.x = table_data[i][col_index[0]];
|
|
ConsNodes[i].loc.y = table_data[i][col_index[1]];
|
|
Xmin = std::min(Xmin, ConsNodes[i].loc.x);
|
|
Xmax = std::max(Xmax, ConsNodes[i].loc.x);
|
|
Ymin = std::min(Ymin, ConsNodes[i].loc.y);
|
|
Ymax = std::max(Ymax, ConsNodes[i].loc.y);
|
|
|
|
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].loc.x = (ConsNodes[i].loc.x - Xmin)/(Xmax - Xmin);
|
|
ConsNodes[i].loc.y = (ConsNodes[i].loc.y - Ymin)/(Ymax - Ymin);
|
|
}
|
|
|
|
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].loc.x = TargNodes[i].loc.x*(Xmax - Xmin) + Xmin;
|
|
TargNodes[i].loc.y = TargNodes[i].loc.y*(Ymax - Ymin) + Ymin;
|
|
|
|
outfile << TargNodes[i].loc.x << " " << TargNodes[i].loc.y << " " << 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)
|
|
{
|
|
desc.file_name_ = para;
|
|
std::vector<point2dc> tmp_vec;
|
|
read_text2vector(desc, tmp_vec);
|
|
|
|
TargNodes.resize(tmp_vec.size());
|
|
for (size_t i = 0; i < tmp_vec.size(); ++i)
|
|
{
|
|
TargNodes[i].loc.x = tmp_vec[i].x;
|
|
TargNodes[i].loc.y = tmp_vec[i].y;
|
|
|
|
TargNodes[i].loc.x = (TargNodes[i].loc.x - Xmin)/(Xmax - Xmin);
|
|
TargNodes[i].loc.y = (TargNodes[i].loc.y - Ymin)/(Ymax - Ymin);
|
|
}
|
|
|
|
for (size_t i = 0; i < TargNodes.size(); i++)
|
|
{
|
|
TargNodes[i].vals.resize(ValSize);
|
|
}
|
|
|
|
destroy_vector(tmp_vec);
|
|
return;
|
|
}
|
|
|
|
double dx, dy, xmin, xmax, ymin, ymax;
|
|
gctl::parse_string_to_value(para, '/', true, xmin, dx, xmax, ymin, dy, ymax);
|
|
|
|
size_t M = floor((ymax - ymin)/dy) + 1;
|
|
size_t N = floor((xmax - xmin)/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].loc.x = xmin + dx*i;
|
|
TargNodes[i + j*N].loc.y = ymin + dy*j;
|
|
|
|
TargNodes[i + j*N].loc.x = (TargNodes[i + j*N].loc.x - Xmin)/(Xmax - Xmin);
|
|
TargNodes[i + j*N].loc.y = (TargNodes[i + j*N].loc.y - Ymin)/(Ymax - Ymin);
|
|
}
|
|
}
|
|
|
|
for (size_t i = 0; i < TargNodes.size(); i++)
|
|
{
|
|
TargNodes[i].vals.resize(ValSize);
|
|
}
|
|
return;
|
|
}
|
|
|
|
void LBSI::CalKernel()
|
|
{
|
|
// 计算出所有成对的格林函数值
|
|
double dist;
|
|
for (int j = 0; j < MatSize-1; ++j)
|
|
{
|
|
for (int k = j+1; k < MatSize; ++k)
|
|
{
|
|
dist = sqrt((ConsNodes[j].loc.x - ConsNodes[k].loc.x)*(ConsNodes[j].loc.x - ConsNodes[k].loc.x)
|
|
+ (ConsNodes[j].loc.y - ConsNodes[k].loc.y)*(ConsNodes[j].loc.y - ConsNodes[k].loc.y));
|
|
|
|
if (dist >= GCTL_ZERO)
|
|
{
|
|
Kernel[j][k] = Kernel[k][j] = dist*dist*(log(dist)-1.0);
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
void LBSI::CalKernel(const data_point &tar_node)
|
|
{
|
|
// 找出距离tar_node最近的一组控制点
|
|
PntBoxes.get_by_number(tar_node.loc.x, tar_node.loc.y, MatSize, LocalNodes);
|
|
|
|
// 计算出所有成对的格林函数值
|
|
double dist;
|
|
for (int j = 0; j < MatSize-1; ++j)
|
|
{
|
|
for (int k = j+1; k < MatSize; ++k)
|
|
{
|
|
dist = sqrt((LocalNodes[j]->loc.x - LocalNodes[k]->loc.x)*(LocalNodes[j]->loc.x - LocalNodes[k]->loc.x)
|
|
+ (LocalNodes[j]->loc.y - LocalNodes[k]->loc.y)*(LocalNodes[j]->loc.y - LocalNodes[k]->loc.y));
|
|
|
|
if (dist >= GCTL_ZERO)
|
|
{
|
|
Kernel[j][k] = Kernel[k][j] = dist*dist*(log(dist)-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;
|
|
} |