findsg.cpp 3.54 KB
Newer Older
Tobias WEBER's avatar
Tobias WEBER committed
1
2
3
4
5
/**
 * finds a matching space group
 * @author Tobias Weber <tweber@ill.fr>
 * @date jan-2020
 * @license GPLv3, see 'LICENSE' file
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * ----------------------------------------------------------------------------
 * mag-core (part of the Takin software suite)
 * Copyright (C) 2018-2021  Tobias WEBER (Institut Laue-Langevin (ILL),
 *                          Grenoble, France).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------------
Tobias WEBER's avatar
Tobias WEBER committed
24
25
26
 */

#include "../structfact/loadcif.h"
27
#include "tlibs2/libs/math20.h"
Tobias WEBER's avatar
Tobias WEBER committed
28
29

#include <gemmi/version.hpp>
30
#include <boost/algorithm/string.hpp>
Tobias WEBER's avatar
Tobias WEBER committed
31
32
33
34
35
#include <iostream>
#include <memory>


using t_real = double;
Tobias WEBER's avatar
Tobias WEBER committed
36
using t_vec = tl2::vec<t_real, std::vector>;
Tobias WEBER's avatar
Tobias WEBER committed
37
using t_mat = tl2::mat<t_real, std::vector>;
Tobias WEBER's avatar
Tobias WEBER committed
38
39
40
41
42
43
44
45
46

constexpr t_real g_eps = 1e-6;


/**
 * entry point
 */
int main(int argc, char** argv)
{
47
	std::cout << "Input atomic positions, 'e' or ENTER to end." << std::endl;
Tobias WEBER's avatar
Tobias WEBER committed
48

49
50
	std::vector<t_vec> vecFinal;
	/*{{ // test data
Tobias WEBER's avatar
Tobias WEBER committed
51
52
53
54
		tl2::create<t_vec>({ 0.1,  0.1,  0.1}),
		tl2::create<t_vec>({ 0.4, -0.1, -0.4}),
		tl2::create<t_vec>({-0.1, -0.4,  0.4}),
		tl2::create<t_vec>({-0.4,  0.4, -0.1}),
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	}}*/;

	std::size_t atomnr = 1;
	while(1)
	{
		std::string strpos;
		std::cout << "Position " << atomnr++ << ": ";

		std::getline(std::cin, strpos);
		boost::trim(strpos);
		if(strpos == "" || strpos == "e")
			break;

		// tokenise
		std::vector<std::string> vecstr;
		boost::split(vecstr, strpos, [](char c) -> bool
		{
			return c == ' ' || c== '\t' || c == ',' || c == ';';
		}, boost::token_compress_on);
Tobias WEBER's avatar
Tobias WEBER committed
74
75


76
77
78
79
80
81
82
83
84
85
86
87
88
		// convert to real vector
		t_vec vecPos;
		for(const std::string& str : vecstr)
			vecPos.emplace_back(std::stod(str));

		// fill up possibly missing coordinates
		while(vecPos.size() < 3)
			vecPos.push_back(0);
		vecPos.resize(3);

		vecFinal.emplace_back(std::move(vecPos));
	}

Tobias WEBER's avatar
Tobias WEBER committed
89
90
91
92
93
94
	if(vecFinal.size() == 0)
	{
		std::cerr << "Insufficient number of positions given." << std::endl;
		return -1;
	}

95
96

	std::cout << "\nFull set of positions to match:\n";
Tobias WEBER's avatar
Tobias WEBER committed
97
98
99
100
101
	std::size_t ctr = 1;
	for(const auto& pos : vecFinal)
		std::cout << "\t(" << ctr++ << ") " << pos << "\n";
	std::cout << std::endl;

102
103
104
105

	std::vector<t_vec> vecInit = vecFinal;


Tobias WEBER's avatar
Tobias WEBER committed
106
107
108
109
110
111
112
113
114
	while(1)
	{
		std::cout << "\n--------------------------------------------------------------------------------\n";
		std::cout << "Base set of positions:\n";
		ctr = 1;
		for(const auto& pos : vecInit)
			std::cout << "\t(" << ctr++ << ") " << pos << "\n";
		std::cout << std::endl;

Tobias WEBER's avatar
Tobias WEBER committed
115
		auto matchingSGs = find_matching_sgs<t_vec, t_mat, t_real>(vecInit, vecFinal, g_eps);
Tobias WEBER's avatar
Tobias WEBER committed
116

117
118
119
120
121
122
123
124
125
126
127
		if(matchingSGs.size())
		{
			std::cout << "Matching space groups:\n";
			ctr = 1;
			for(const auto& sg : matchingSGs)
				std::cout << "\t(" << ctr++ << ") " << std::get<1>(sg) << "\n";
		}
		else
		{
			std::cout << "No matching space groups.\n";
		}
Tobias WEBER's avatar
Tobias WEBER committed
128
129
130
131
132
133
134
135
136
137
		std::cout << "--------------------------------------------------------------------------------\n";
		std::cout << std::endl;

		vecInit.pop_back();
		if(vecInit.size() == 0)
			break;
	}

	return 0;
}