initial upload

This commit is contained in:
2024-09-10 20:25:18 +08:00
parent b8de03ee4f
commit f1cc876972
377 changed files with 2721267 additions and 34 deletions

27
slbsi/CMakeLists.txt Normal file
View File

@@ -0,0 +1,27 @@
set(TOOL_NAME slbsi)
set(BIN_DIR bin)
set(INSTALL_DIR sbin)
find_package(GCTL_OPTIMIZATION REQUIRED)
include_directories(${GCTL_OPTIMIZATION_INC_DIR})
#find_package(LibLCG REQUIRED)
#include_directories(${LibLCG_INC_DIR})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
if(WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
endif()
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/${BIN_DIR})
aux_source_directory(. TOOL_SRC)
add_executable(${TOOL_NAME} ${TOOL_SRC})
set_target_properties(${TOOL_NAME} PROPERTIES INSTALL_RPATH /usr/local/lib)
set_target_properties(${TOOL_NAME} PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON)
target_link_libraries(${TOOL_NAME} PUBLIC ${GCTL_OPTIMIZATION_LIB})
install(TARGETS ${TOOL_NAME} RUNTIME DESTINATION ${INSTALL_DIR})

85
slbsi/slbsi.cpp Normal file
View File

@@ -0,0 +1,85 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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.
******************************************************/
#include "slbsi.h"
int main(int argc, char *argv[]) try
{
gctl::flags_parser fp;
fp.set_proname("lbsi");
fp.set_proinfo("Limited memory Bi-harmonic Spline Interpolation of 2D arbitrary located data on a spherical surface. \
This program is a toolkit of the GCTL package. The GCTL comes with ABSOLUTE NO WARRANTY. \
Please see instructions or contact the author for more information.");
fp.add_opt('i', "in-table", required_argument, NULL, "Input table name of the input locations and data.", "<file>", true);
fp.add_opt('t', "target-table", required_argument, NULL, "Input table name of the target locations or parameters of output grid locations.", "<file>|<xmin>/<dx>/<xmax>/<ymin>/<dx>/<ymax>", true);
fp.add_opt('o', "out-table", required_argument, NULL, "Output table name of the interpolated data.", "<file>", true);
fp.add_opt('k', "kernel-size", required_argument, NULL, "Kernel size for solving the linear system of a target location (default is 20), which equals to the number of closest input locations that are used to evaluate the interpolated value.", "<size>", false);
fp.add_opt('e', "epsilon", required_argument, NULL, "Threshold for solving the linear system (default is 1e-10).", "<value>", false);
fp.add_opt('b', "box-dimension", required_argument, NULL, "2D boxes' dimensions used for pre-processing the input data (default is 10).", "<size>", false);
fp.add_opt('c', "column", required_argument, NULL, "Column index of x, y and data. The defaults are 0,1,2. Add extra columns to process multiple data sets.", "<col-x>,<col-y>,<col-data>,...", false);
fp.add_opt('h', "head-record", required_argument, NULL, "Input file has [0] Header record(s).", "<size>", false);
fp.add_opt('a', "annotate", required_argument, NULL, "The starting symbol of an annotate line. The default is '#'.", "<sym>", false);
fp.add_opt('d', "delimeter", required_argument, NULL, "Delimeter between different columns. The default is space.", "<sym>", false);
fp.configure(argc, argv);
if (argc == 1)
{
fp.show_help_page();
return 0;
}
std::string in_name, out_name, tar_name, kernel_str, eps_str, box_str, col_str, head_str, ant_str, del_str;
fp.get_argv({'i', 't', 'o', 'k', 'e', 'b', 'c', 'h', 'a', 'd'}, {&in_name, &tar_name, &out_name, &kernel_str, &eps_str, &box_str, &col_str, &head_str, &ant_str, &del_str});
// 查看是否通过强制参数检查
if (!fp.pass_mandatory()) return 0;
int kernel_size, box_size;
double epsilon;
if (kernel_str == "NULL") kernel_size = 20;
else gctl::str2type(kernel_str, kernel_size);
if (eps_str == "NULL") epsilon = 1e-10;
else gctl::str2type(eps_str, epsilon);
if (box_str == "NULL") box_size = 10;
else gctl::str2type(box_str, box_size);
if (col_str == "NULL") col_str = "0,1,2";
gctl::text_descriptor desc;
if (head_str != "NULL") gctl::str2type(head_str, desc.head_num_);
if (ant_str != "NULL") gctl::str2type(ant_str, desc.att_sym_);
if (del_str != "NULL") gctl::str2type(del_str, desc.delimiter_);
LBSI si;
si.Routine(in_name, tar_name, out_name, col_str, desc, kernel_size, box_size, epsilon);
return 0;
}
catch (std::exception &e)
{
GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}

431
slbsi/slbsi.h Normal file
View File

@@ -0,0 +1,431 @@
/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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);
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);
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_all(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_all(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;
gctl::read_text2vector2d(filename, table_data, desc);
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)
{
// try to use the para as a file name
if (access(para.c_str(), F_OK) != -1)
{
std::vector<point2dc> tmp_vec;
read_text2vector(para, 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;
}