crystalcell 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +1 -0
- data/VERSION +1 -1
- data/crystalcell.gemspec +81 -0
- data/lib/crystalcell/atom.rb +118 -118
- data/lib/crystalcell/cell.rb +584 -584
- data/lib/crystalcell/element.rb +135 -135
- data/lib/crystalcell/latticeaxes.rb +194 -188
- data/lib/crystalcell/periodiccell.rb +244 -244
- data/test/test_atom.rb +198 -198
- data/test/test_cell.rb +1450 -1478
- data/test/test_element.rb +642 -642
- data/test/test_latticeaxes.rb +283 -278
- data/test/test_periodiccell.rb +948 -948
- metadata +4 -3
data/CHANGES
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/crystalcell.gemspec
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "crystalcell"
|
8
|
+
s.version = "0.0.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["ippei94da"]
|
12
|
+
s.date = "2015-02-05"
|
13
|
+
s.description = "This gem provides Cell, LatticeAxes, Atom classes, and so on.\n And this provides simple treatment of a periodic boundary condition.\n "
|
14
|
+
s.email = "ippei94da@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"CHANGES",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"crystalcell.gemspec",
|
28
|
+
"lib/crystalcell.rb",
|
29
|
+
"lib/crystalcell/atom.rb",
|
30
|
+
"lib/crystalcell/cell.rb",
|
31
|
+
"lib/crystalcell/element.rb",
|
32
|
+
"lib/crystalcell/latticeaxes.rb",
|
33
|
+
"lib/crystalcell/periodiccell.rb",
|
34
|
+
"test/cif/ZrO2-25C.cif",
|
35
|
+
"test/helper.rb",
|
36
|
+
"test/test_atom.rb",
|
37
|
+
"test/test_cell.rb",
|
38
|
+
"test/test_element.rb",
|
39
|
+
"test/test_latticeaxes.rb",
|
40
|
+
"test/test_periodiccell.rb"
|
41
|
+
]
|
42
|
+
s.homepage = "http://github.com/ippei94da/crystalcell"
|
43
|
+
s.licenses = ["MIT"]
|
44
|
+
s.require_paths = ["lib"]
|
45
|
+
s.rubygems_version = "1.8.23"
|
46
|
+
s.summary = "Classes around a cell in crystallography"
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<rdoc>, ["~> 4.1.1"])
|
53
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.7.2"])
|
54
|
+
s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
|
55
|
+
s.add_development_dependency(%q<simplecov>, [">= 0.9.0"])
|
56
|
+
s.add_development_dependency(%q<malge>, [">= 0.0.8"])
|
57
|
+
s.add_development_dependency(%q<mageo>, [">= 0.0.3"])
|
58
|
+
s.add_development_dependency(%q<maset>, [">= 0.0.0"])
|
59
|
+
s.add_development_dependency(%q<builtinextension>, [">= 0.1.2"])
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<rdoc>, ["~> 4.1.1"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.7.2"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
64
|
+
s.add_dependency(%q<simplecov>, [">= 0.9.0"])
|
65
|
+
s.add_dependency(%q<malge>, [">= 0.0.8"])
|
66
|
+
s.add_dependency(%q<mageo>, [">= 0.0.3"])
|
67
|
+
s.add_dependency(%q<maset>, [">= 0.0.0"])
|
68
|
+
s.add_dependency(%q<builtinextension>, [">= 0.1.2"])
|
69
|
+
end
|
70
|
+
else
|
71
|
+
s.add_dependency(%q<rdoc>, ["~> 4.1.1"])
|
72
|
+
s.add_dependency(%q<bundler>, ["~> 1.7.2"])
|
73
|
+
s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
|
74
|
+
s.add_dependency(%q<simplecov>, [">= 0.9.0"])
|
75
|
+
s.add_dependency(%q<malge>, [">= 0.0.8"])
|
76
|
+
s.add_dependency(%q<mageo>, [">= 0.0.3"])
|
77
|
+
s.add_dependency(%q<maset>, [">= 0.0.0"])
|
78
|
+
s.add_dependency(%q<builtinextension>, [">= 0.1.2"])
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
data/lib/crystalcell/atom.rb
CHANGED
@@ -6,125 +6,125 @@ raise "Atom class need ruby version later than 1.9." if RUBY_VERSION.to_f < 1.9
|
|
6
6
|
# This class is not assumeed in a periodic condition.
|
7
7
|
class CrystalCell::Atom
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
9
|
+
include Mageo
|
10
|
+
|
11
|
+
class TypeError < Exception ; end
|
12
|
+
|
13
|
+
# Do not change :position to attr_accessor.
|
14
|
+
# This must be Vector3DInternal instance.
|
15
|
+
attr_reader :movable_flags
|
16
|
+
attr_accessor :element, :name, :position
|
17
|
+
|
18
|
+
# Arguments:
|
19
|
+
# elements:
|
20
|
+
# Identifier of a element.
|
21
|
+
# This may be String like "Li", Integer like 0, or other class instances.
|
22
|
+
# Why doesn't this class have concrete name of elements?
|
23
|
+
# The reason is below:
|
24
|
+
# - This class is assumed to be used in Cell class instances.
|
25
|
+
# When we treat symmetry of the cell, the elements of atoms are not necessary information.
|
26
|
+
# This specification enable highly abstract level of treatment of cells.
|
27
|
+
# - POSCAR file of VASP (before ver.4 series) don't have element information.
|
28
|
+
# This specification enable to use POSCAR directly without POTCAR or element indication.
|
29
|
+
# position:
|
30
|
+
# Coordinates of an atom.
|
31
|
+
# This argument 'position' is mainly assumed as a internal coordinate.
|
32
|
+
# name:
|
33
|
+
# Identifier of an atom, e.g., "Li1" or "O23".
|
34
|
+
# movable_flags:
|
35
|
+
# Movable flags of an atom in each of three directions as a Array of three items.
|
36
|
+
def initialize(element, position, name = nil, movable_flags = nil)
|
37
|
+
#p position
|
38
|
+
#p position.methods.sort
|
39
|
+
raise TypeError, "Position doesn't have []: (#{position.inspect})" unless position.methods.include?(:[])
|
40
|
+
raise TypeError, "Number of items in position is not 3: (#{position})" if position.size != 3
|
41
|
+
raise TypeError, "Coordinate is not a Float: (#{position})" if position[0].class != Float
|
42
|
+
raise TypeError, "Coordinate is not a Float: (#{position})" if position[1].class != Float
|
43
|
+
raise TypeError, "Coordinate is not a Float: (#{position})" if position[2].class != Float
|
44
|
+
|
45
|
+
@element = element
|
46
|
+
set_position(position)
|
47
|
+
@name = name
|
48
|
+
@movable_flags = movable_flags
|
49
|
+
end
|
50
|
+
|
51
|
+
## Class methods
|
52
|
+
|
53
|
+
# Equivalence checking (class method version).
|
54
|
+
# Return true when all the condition below are satisfied:
|
55
|
+
# - same element identifier.
|
56
|
+
# - difference of coordinates is within tolerance.
|
57
|
+
# Note that distance cannot be used since this class doesn't have a lattice axes.
|
58
|
+
# Imagine the shape of tolerant zone is a hexahedron.
|
59
|
+
def self.equal_in_delta?(atom0, atom1, tol = 0.0)
|
60
|
+
return false if atom0.element != atom1.element
|
61
|
+
3.times do |i|
|
62
|
+
return false if ((atom0.position[i] - atom1.position[i]).abs > tol)
|
63
|
+
end
|
64
|
+
return true
|
65
|
+
end
|
66
|
+
|
67
|
+
## Instance methods
|
68
|
+
|
69
|
+
# Restricted equivalence checking.
|
70
|
+
# Return true when all the condition below are satisfied:
|
71
|
+
# - same element identifier.
|
72
|
+
# - difference of coordinates is the same.
|
73
|
+
# This method should be used after well-consideration.
|
74
|
+
# Arbitrary coordinates are not generally the same like Float instances.
|
75
|
+
# It is a good idea to use in test scripts.
|
76
|
+
def ==(other)
|
77
|
+
#pp self
|
78
|
+
#pp other
|
79
|
+
self.class.equal_in_delta?(self, other, 0.0)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Equivalence checking (instance method version) .
|
83
|
+
# Return true when all the condition below are satisfied:
|
84
|
+
# - same element identifier.
|
85
|
+
# - difference of coordinates is within tolerance.
|
86
|
+
def equal_in_delta?(other, tol = 0.0)
|
87
|
+
self.class.equal_in_delta?(self, other, tol)
|
88
|
+
end
|
89
|
+
|
90
|
+
#Return Vector3DInternal instance consist of coordinates between 0 and 1.
|
91
|
+
def internal_coordinates
|
92
|
+
result = @position.map{ |coord| coord - coord.floor }
|
93
|
+
return Vector3DInternal[ *result ]
|
94
|
+
end
|
95
|
+
|
96
|
+
# Return Vector3DInternal instance consist of integers,
|
97
|
+
# by which self.internal_coordinates translate to self.position.
|
98
|
+
def translation_symmetry_operation
|
99
|
+
result = @position.map{ |coord| coord.floor }
|
100
|
+
return Vector3DInternal[ *result ]
|
101
|
+
end
|
102
|
+
|
103
|
+
# Overwrite position.
|
104
|
+
def set_position(position)
|
105
|
+
#pp position
|
106
|
+
#pp position.class
|
107
|
+
raise TypeError, "#{position.class}" if position.class == Vector3D
|
108
|
+
@position = position.to_v3di
|
109
|
+
end
|
110
|
+
|
111
|
+
# Translate atom position (destructive).
|
112
|
+
# Argument 'vec' should be given as internal vector,
|
113
|
+
# which is an Array or Vector3DInternal instance but Vector3D instance.
|
114
|
+
def translate!(vec)
|
115
|
+
raise TypeError if vec.class == Vector3D
|
116
|
+
raise TypeError if vec.size != 3
|
117
|
+
self.set_position(@position + Vector3DInternal[ *vec ])
|
118
|
+
end
|
119
|
+
|
120
|
+
# Translate atom position (nondestructive).
|
121
|
+
# Argument 'vec' should be given as internal vector,
|
122
|
+
# which is an Array or Vector3DInternal instance but Vector3D instance.
|
123
|
+
def translate(vec)
|
124
|
+
tmp = Marshal.load(Marshal.dump(self))
|
125
|
+
tmp.translate!(vec)
|
126
|
+
return tmp
|
63
127
|
end
|
64
|
-
return true
|
65
|
-
end
|
66
|
-
|
67
|
-
## Instance methods
|
68
|
-
|
69
|
-
# Restricted equivalence checking.
|
70
|
-
# Return true when all the condition below are satisfied:
|
71
|
-
# - same element identifier.
|
72
|
-
# - difference of coordinates is the same.
|
73
|
-
# This method should be used after well-consideration.
|
74
|
-
# Arbitrary coordinates are not generally the same like Float instances.
|
75
|
-
# It is a good idea to use in test scripts.
|
76
|
-
def ==(other)
|
77
|
-
#pp self
|
78
|
-
#pp other
|
79
|
-
self.class.equal_in_delta?(self, other, 0.0)
|
80
|
-
end
|
81
|
-
|
82
|
-
# Equivalence checking (instance method version) .
|
83
|
-
# Return true when all the condition below are satisfied:
|
84
|
-
# - same element identifier.
|
85
|
-
# - difference of coordinates is within tolerance.
|
86
|
-
def equal_in_delta?(other, tol = 0.0)
|
87
|
-
self.class.equal_in_delta?(self, other, tol)
|
88
|
-
end
|
89
|
-
|
90
|
-
#Return Vector3DInternal instance consist of coordinates between 0 and 1.
|
91
|
-
def internal_coordinates
|
92
|
-
result = @position.map{ |coord| coord - coord.floor }
|
93
|
-
return Vector3DInternal[ *result ]
|
94
|
-
end
|
95
|
-
|
96
|
-
# Return Vector3DInternal instance consist of integers,
|
97
|
-
# by which self.internal_coordinates translate to self.position.
|
98
|
-
def translation_symmetry_operation
|
99
|
-
result = @position.map{ |coord| coord.floor }
|
100
|
-
return Vector3DInternal[ *result ]
|
101
|
-
end
|
102
|
-
|
103
|
-
# Overwrite position.
|
104
|
-
def set_position(position)
|
105
|
-
#pp position
|
106
|
-
#pp position.class
|
107
|
-
raise TypeError, "#{position.class}" if position.class == Vector3D
|
108
|
-
@position = position.to_v3di
|
109
|
-
end
|
110
|
-
|
111
|
-
# Translate atom position (destructive).
|
112
|
-
# Argument 'vec' should be given as internal vector,
|
113
|
-
# which is an Array or Vector3DInternal instance but Vector3D instance.
|
114
|
-
def translate!(vec)
|
115
|
-
raise TypeError if vec.class == Vector3D
|
116
|
-
raise TypeError if vec.size != 3
|
117
|
-
self.set_position(@position + Vector3DInternal[ *vec ])
|
118
|
-
end
|
119
|
-
|
120
|
-
# Translate atom position (nondestructive).
|
121
|
-
# Argument 'vec' should be given as internal vector,
|
122
|
-
# which is an Array or Vector3DInternal instance but Vector3D instance.
|
123
|
-
def translate(vec)
|
124
|
-
tmp = Marshal.load(Marshal.dump(self))
|
125
|
-
tmp.translate!(vec)
|
126
|
-
return tmp
|
127
|
-
end
|
128
128
|
|
129
129
|
end
|
130
130
|
|
data/lib/crystalcell/cell.rb
CHANGED
@@ -21,610 +21,610 @@ require "maset/mapping.rb"
|
|
21
21
|
#Written by Ippei Kishida [2010-12-19].
|
22
22
|
#
|
23
23
|
##Cell
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
24
|
+
# セル内の原子は、内部的には配列として保持するが、
|
25
|
+
# この順序は制御できないものとする。
|
26
|
+
# たとえば Li, Ge, O の順序にソートされているなどと思ってはいけない。
|
27
|
+
# 順序に依存するプログラムを作ってはいけない。
|
28
28
|
#
|
29
29
|
##Note:
|
30
30
|
#Cell クラスは元素情報をネイティブには持たない
|
31
|
-
#
|
32
|
-
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
#
|
31
|
+
# ボツ案:
|
32
|
+
# たとえば構成元素の情報を持ち、
|
33
|
+
# さらに Atom クラスインスタンスも持つとする。
|
34
|
+
# 原子の追加の仕方によっては、
|
35
|
+
# Atoms クラスの元素情報と矛盾するという状況は十分に考えられる。
|
36
36
|
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
37
|
+
# 構成元素として Li があっても、
|
38
|
+
# Li 原子リストが空リストだったらその元素はあると判定されるべきか、
|
39
|
+
# 疑問が生じる。
|
40
40
|
class CrystalCell::Cell
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
42
|
+
begin
|
43
|
+
require "getspg.so"
|
44
|
+
include Getspg
|
45
|
+
rescue LoadError
|
46
|
+
#Do nothing.
|
47
|
+
#To use basic functions even in environments without spglib.
|
48
|
+
end
|
49
|
+
|
50
|
+
include Mageo
|
51
|
+
|
52
|
+
class NoAtomError < Exception; end
|
53
|
+
class AxesMismatchError < Exception; end
|
54
|
+
class AxesRangeError < Exception; end
|
55
|
+
class SameAxesError < Exception; end
|
56
|
+
class TypeError < Exception; end
|
57
|
+
class ArgumentError < Exception; end #その他的エラー
|
58
|
+
class NoSpglibError < Exception; end
|
59
|
+
|
60
|
+
attr_reader :element_names, :atoms, :axes
|
61
|
+
attr_accessor :comment
|
62
|
+
|
63
|
+
#Argument 'axes' must have :to_a method and expressed in 3x3 Array.
|
64
|
+
def initialize(axes, atoms = [])
|
65
|
+
#raise CellTypeError unless axes.is_a?(Axes)
|
66
|
+
if axes.class == CrystalCell::LatticeAxes
|
67
|
+
@axes = axes
|
68
|
+
else
|
69
|
+
@axes = CrystalCell::LatticeAxes.new( axes.to_a )
|
70
|
+
end
|
71
|
+
|
72
|
+
atoms.each do |atom|
|
73
|
+
#pp atom
|
74
|
+
unless atom.is_a?(CrystalCell::Atom)
|
75
|
+
raise CellTypeError,
|
76
|
+
"#{atom} is not a kind of CrystalCell::Atom."
|
77
|
+
end
|
78
|
+
end
|
79
|
+
@atoms = atoms
|
80
|
+
end
|
81
|
+
|
82
|
+
#セルに原子を追加する。
|
83
|
+
def add_atom(atom)
|
84
|
+
#raise "Cell::add_atom, 2nd argument must be Array." if pos.class != Array
|
85
|
+
raise CellTypeError unless atom.is_a?(CrystalCell::Atom)
|
86
|
+
@atoms << atom
|
87
|
+
end
|
88
|
+
|
89
|
+
#Delete an atom from a cell.
|
90
|
+
#i は Cell クラスが保持している原子の番号。
|
91
|
+
#Cell クラスは原子を配列として保持しており、
|
92
|
+
#その番号を指定すると考えると分かり易かろう。
|
93
|
+
def delete_atom( i )
|
94
|
+
#raise "CrystalCell::Atom ID[#{i}] not exist" if @atoms[i] == nil
|
95
|
+
@atoms.delete_at( i )
|
96
|
+
end
|
97
|
+
|
98
|
+
#全ての原子の元素情報のリストを返す。
|
99
|
+
#unique なものを抽出したりはしない。
|
100
|
+
#unique なものが必要なら返り値に .uniq をつければ良い。
|
101
|
+
#e.g., #=> ['Li', 'N', 'Li']
|
102
|
+
#e.g., #=> [0, 1, 2, 1]
|
103
|
+
def elements
|
104
|
+
@atoms.collect{ |i| i.element }
|
105
|
+
end
|
106
|
+
|
107
|
+
#全ての原子の位置情報のリストを返す。
|
108
|
+
def positions
|
109
|
+
@atoms.collect{ |i| i.position }
|
110
|
+
end
|
111
|
+
|
112
|
+
#元素情報が elem の原子の index を配列にまとめて返す。
|
113
|
+
#index は原子の永続的な id ではない。
|
114
|
+
#Array#select は index ではなく要素そのものを配列にして返すので、少し違う。
|
115
|
+
def select_indices( &block )
|
116
|
+
return @atoms.select_indices( &block )
|
117
|
+
end
|
118
|
+
|
119
|
+
#Set element name to each atom in self.
|
120
|
+
#Argument 'elems' is a list of new names, which has [] method. e.g.,
|
121
|
+
# 1. Array, [ 'Li', 'O' ]
|
122
|
+
# 2. Hash , { 0 => 'Li', 1 => 'O' ]
|
123
|
+
# 3. Hash , { 'Li' => 'Na' }
|
124
|
+
#1. and 2. of the above examples induce the same result.
|
125
|
+
#Case 1. can be convenient for element names of array from POTCAR.
|
126
|
+
#
|
127
|
+
#The atoms with the name which is not included the hash key do not change their names.
|
128
|
+
def set_elements( elems )
|
129
|
+
@atoms.each do |atom|
|
130
|
+
begin
|
131
|
+
new_elem = elems[ atom.element ]
|
132
|
+
rescue
|
133
|
+
next
|
134
|
+
end
|
135
|
+
next if new_elem == nil
|
136
|
+
atom.element = new_elem
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
#セルを拡張したスーパーセルを考えたとき、中に含まれる原子のリストを返す。
|
141
|
+
#引数の意味は以下の通り。
|
142
|
+
#a_min : a 軸方向のセルの方向を整数で示したときの最小値
|
143
|
+
#a_max : a 軸方向のセルの方向を整数で示したときの最大値
|
144
|
+
#b_min : b 軸方向のセルの方向を整数で示したときの最小値
|
145
|
+
#b_max : b 軸方向のセルの方向を整数で示したときの最大値
|
146
|
+
#c_min : c 軸方向のセルの方向を整数で示したときの最小値
|
147
|
+
#c_max : c 軸方向のセルの方向を整数で示したときの最大値
|
148
|
+
#-1, 1, -1, 1, -1, 1 と指定すれば 3x3x3 の 27倍体積の構造になる。
|
149
|
+
def atoms_in_supercell( a_min, a_max, b_min, b_max, c_min, c_max )
|
150
|
+
results = []
|
151
|
+
@atoms.each do |atom|
|
152
|
+
a_min.upto( a_max ) do |a|
|
153
|
+
b_min.upto( b_max ) do |b|
|
154
|
+
c_min.upto( c_max ) do |c|
|
155
|
+
results << CrystalCell::Atom.new( atom.element, (atom.position.to_v3di + [ a, b, c ].to_v3di).to_a )
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
results
|
161
|
+
end
|
162
|
+
|
163
|
+
#他のセルと格子定数が等価であれば true を、そうでなければ false を返す。
|
164
|
+
#other: 他のセル
|
165
|
+
#length_ratio: 長さ(a, b, c) の許容値を比で指定
|
166
|
+
#angle_tolerance: 角度(alpha, beta, gamma) の許容値を角度の値で指定
|
167
|
+
def equal_lattice_in_delta?( other, length_ratio, angle_tolerance )
|
168
|
+
@axes.equal_in_delta?(
|
169
|
+
CrystalCell::LatticeAxes.new( other.axes.to_a ), length_ratio, angle_tolerance
|
170
|
+
)
|
171
|
+
end
|
172
|
+
|
173
|
+
#含まれる全原子が等価比較で一対一対応が付けられれば true を返す。
|
174
|
+
#Cell に保持される順番に関係なく、等価な原子同士が一対一に対応づけられるかで
|
175
|
+
#チェックする。
|
176
|
+
def equal_atoms_in_delta?( other, position_tolerance )
|
177
|
+
return false unless Mapping::map?(@atoms, other.atoms ){ |i,j| i.equal_in_delta?( j, position_tolerance ) }
|
178
|
+
return true
|
179
|
+
end
|
180
|
+
|
181
|
+
#等価判定。
|
182
|
+
#格子定数の長さの比率の許容値、格子定数の角度の許容値、原子座標の許容値。
|
183
|
+
def equal_in_delta?( other, length_ratio, angle_tolerance, position_tolerance )
|
184
|
+
return false unless equal_lattice_in_delta?(other, length_ratio, angle_tolerance)
|
185
|
+
return false unless equal_atoms_in_delta?(other, position_tolerance)
|
186
|
+
return true
|
187
|
+
end
|
188
|
+
|
189
|
+
#等価判定。
|
190
|
+
#「==」による等価判定は実数の等価判定と同じく、基本的には使うべきではない。
|
191
|
+
#しかし、これを定義しておくとテストが楽になることが多い。
|
192
|
+
def ==( other )
|
193
|
+
#pp axes;
|
194
|
+
#pp other.axes;
|
195
|
+
|
196
|
+
return false unless self.axes == other.axes #equal_in_delta( 0.0, 0.0, 0.0 ) とすると計算誤差でうまくいかないことがある。
|
197
|
+
equal_atoms_in_delta?( other, 0.0 )
|
198
|
+
end
|
199
|
+
|
200
|
+
#2つの地点間の距離を返す。
|
201
|
+
#それぞれ、内部座標 Vector3DInternal クラスインスタンスなら絶対座標に変換される。
|
202
|
+
#絶対座標ならばそのまま計算する。
|
203
|
+
#Vector3D か Vector3DInternal 以外のクラスなら例外 Cell::TypeError を投げる。
|
204
|
+
#周期性を考慮したりはしない。
|
205
|
+
#周期性を考慮した距離は PeriodicCell#nearest_distance で行うべき。
|
206
|
+
def distance( pos0, pos1 )
|
207
|
+
if ((pos0.class != Vector3DInternal) && (pos0.class != Vector3D))
|
208
|
+
raise CrystalCell::Cell::TypeError
|
157
209
|
end
|
158
|
-
|
159
|
-
|
160
|
-
results
|
161
|
-
end
|
162
|
-
|
163
|
-
#他のセルと格子定数が等価であれば true を、そうでなければ false を返す。
|
164
|
-
#other: 他のセル
|
165
|
-
#length_ratio: 長さ(a, b, c) の許容値を比で指定
|
166
|
-
#angle_tolerance: 角度(alpha, beta, gamma) の許容値を角度の値で指定
|
167
|
-
def equal_lattice_in_delta?( other, length_ratio, angle_tolerance )
|
168
|
-
@axes.equal_in_delta?(
|
169
|
-
CrystalCell::LatticeAxes.new( other.axes.to_a ), length_ratio, angle_tolerance
|
170
|
-
)
|
171
|
-
end
|
172
|
-
|
173
|
-
#含まれる全原子が等価比較で一対一対応が付けられれば true を返す。
|
174
|
-
#Cell に保持される順番に関係なく、等価な原子同士が一対一に対応づけられるかで
|
175
|
-
#チェックする。
|
176
|
-
def equal_atoms_in_delta?( other, position_tolerance )
|
177
|
-
return false unless Mapping::map?(@atoms, other.atoms ){ |i,j| i.equal_in_delta?( j, position_tolerance ) }
|
178
|
-
return true
|
179
|
-
end
|
180
|
-
|
181
|
-
#等価判定。
|
182
|
-
#格子定数の長さの比率の許容値、格子定数の角度の許容値、原子座標の許容値。
|
183
|
-
def equal_in_delta?( other, length_ratio, angle_tolerance, position_tolerance )
|
184
|
-
return false unless equal_lattice_in_delta?(other, length_ratio, angle_tolerance)
|
185
|
-
return false unless equal_atoms_in_delta?(other, position_tolerance)
|
186
|
-
return true
|
187
|
-
end
|
188
|
-
|
189
|
-
#等価判定。
|
190
|
-
#「==」による等価判定は実数の等価判定と同じく、基本的には使うべきではない。
|
191
|
-
#しかし、これを定義しておくとテストが楽になることが多い。
|
192
|
-
def ==( other )
|
193
|
-
#pp axes;
|
194
|
-
#pp other.axes;
|
195
|
-
|
196
|
-
return false unless self.axes == other.axes #equal_in_delta( 0.0, 0.0, 0.0 ) とすると計算誤差でうまくいかないことがある。
|
197
|
-
equal_atoms_in_delta?( other, 0.0 )
|
198
|
-
end
|
199
|
-
|
200
|
-
#2つの地点間の距離を返す。
|
201
|
-
#それぞれ、内部座標 Vector3DInternal クラスインスタンスなら絶対座標に変換される。
|
202
|
-
#絶対座標ならばそのまま計算する。
|
203
|
-
#Vector3D か Vector3DInternal 以外のクラスなら例外 Cell::TypeError を投げる。
|
204
|
-
#周期性を考慮したりはしない。
|
205
|
-
#周期性を考慮した距離は PeriodicCell#nearest_distance で行うべき。
|
206
|
-
def distance( pos0, pos1 )
|
207
|
-
if ((pos0.class != Vector3DInternal) && (pos0.class != Vector3D))
|
208
|
-
raise CrystalCell::Cell::TypeError
|
209
|
-
end
|
210
|
-
if ((pos1.class != Vector3DInternal) && (pos1.class != Vector3D))
|
211
|
-
raise CrystalCell::Cell::TypeError
|
212
|
-
end
|
213
|
-
|
214
|
-
v0 = pos0.to_v3d(@axes) if pos0.class == Vector3DInternal
|
215
|
-
v1 = pos1.to_v3d(@axes) if pos1.class == Vector3DInternal
|
216
|
-
|
217
|
-
(v0 - v1).r
|
218
|
-
end
|
219
|
-
|
220
|
-
#Dump string in POSCAR format.
|
221
|
-
#Argument <io> can be a file handle or nil.
|
222
|
-
#POSCAR を作るには、元素の順番を指定する必要があるので
|
223
|
-
#それを element_order で指定している。
|
224
|
-
#element_order の要素と == で一致する CrystalCell::Atom instance を
|
225
|
-
#それぞれ全て出力する。
|
226
|
-
#e.g.,
|
227
|
-
# cell.dump_poscar( STDOUT ) #=> output to stdout.
|
228
|
-
# cell.dump_poscar( fileIo ) #=> output to file.
|
229
|
-
# cell.dump_poscar( nil ) #=> return in String instance.
|
230
|
-
# cell.dump_poscar #=> return in String instance.
|
231
|
-
def dump_poscar( element_order, io = nil )
|
232
|
-
if (io == nil)
|
233
|
-
return create_poscar( element_order )
|
234
|
-
else
|
235
|
-
io.puts create_poscar( element_order )
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
#Cell rotation.( Destructive method)
|
240
|
-
#Argument 'matrix' is 3x3 Array of float.
|
241
|
-
#This method does not modify the position to the range between 0 and 1,
|
242
|
-
#even if it was out of range.
|
243
|
-
def rotate!( matrix )
|
244
|
-
@atoms.each { |atom|
|
245
|
-
old_pos = atom.position
|
246
|
-
new_pos = [0.0, 0.0, 0.0]
|
247
|
-
3.times do |y|
|
248
|
-
3.times do |x|
|
249
|
-
new_pos[y] += (matrix[y][x] * old_pos[x])
|
210
|
+
if ((pos1.class != Vector3DInternal) && (pos1.class != Vector3D))
|
211
|
+
raise CrystalCell::Cell::TypeError
|
250
212
|
end
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
#並進移動を行う非破壊的メソッド。
|
273
|
-
#ary は Float 3 要素の配列。
|
274
|
-
def translate( ary )
|
275
|
-
t = Marshal.load( Marshal.dump( self ) )
|
276
|
-
t.translate!( ary )
|
277
|
-
return t
|
278
|
-
end
|
279
|
-
|
280
|
-
#Return arithmetic mean of atomic positions in an internal coordinates.
|
281
|
-
#Raise 'Cell::NoAtomError' if no atoms included in self.
|
282
|
-
def center_of_atoms
|
283
|
-
raise CrystalCell::Cell::NoAtomError if @atoms.size == 0
|
284
|
-
|
285
|
-
vec = Vector3DInternal[ 0.0, 0.0, 0.0 ]
|
286
|
-
@atoms.each { |i|
|
287
|
-
3.times { |j| vec[j] += i.position[j] }
|
288
|
-
}
|
289
|
-
vec *= 1.0/ @atoms.size
|
290
|
-
end
|
291
|
-
|
292
|
-
#Calculate volume.
|
293
|
-
def calc_volume
|
294
|
-
axes = @axes.to_a.map { |i| Vector3D[*i] }
|
295
|
-
vA, vB, vC = axes[0..2]
|
296
|
-
Vector3D.scalar_triple_product( vA, vB, vC ).abs
|
297
|
-
end
|
298
|
-
|
299
|
-
#Generate a new cell with the same lattice consants,
|
300
|
-
#containing atoms of indicated elements.
|
301
|
-
#Argument 'elems' must be an array of element names.
|
302
|
-
#含まれる @atoms の順序は、保存される。元素ごとに並び換えたりしない。
|
303
|
-
#CrystalCell::Atom.element が elems の要素のどれかと完全一致しているもののみ対象となる。
|
304
|
-
#サブクラスのインスタンスで実行した場合には、
|
305
|
-
#サブクラスのインスタンスとして生成する。
|
306
|
-
def cell_of_elements( elems )
|
307
|
-
result = self.class.new( @axes )
|
308
|
-
@atoms.each do |atom|
|
309
|
-
result.add_atom(atom) if elems.include?( atom.element )
|
310
|
-
end
|
311
|
-
return result
|
312
|
-
end
|
313
|
-
|
314
|
-
#格子定数の同じ2つのセルを合わせて、全ての原子が含まれる1つのセルを返す
|
315
|
-
#非破壊的メソッド。
|
316
|
-
#2つのセルの格子定数が異なれば例外 Cell::AxesMismatchError を発生させる。
|
317
|
-
#内部的には @atoms はレシーバの @atoms のあとに引数の @atoms を追加した形になる。
|
318
|
-
#comment は空文字になる。
|
319
|
-
#原子座標の重複チェックなどは行わない。
|
320
|
-
def unite( cell )
|
321
|
-
#raise Cell::AxesMismatchError unless @axes == cell.axes
|
322
|
-
result = Marshal.load( Marshal.dump( self ) )
|
323
|
-
cell.atoms.each do |atom|
|
324
|
-
result.add_atom(atom)
|
325
|
-
end
|
326
|
-
return result
|
327
|
-
end
|
328
|
-
|
329
|
-
#任意の格子軸のベクトルを反転する破壊的メソッド。
|
330
|
-
#大まかなイメージとしては、
|
331
|
-
#格子軸の原点をセルを構成する8つの頂点のどれかに移動する操作と考えれば良い。
|
332
|
-
# ただし厳密には、格子ベクトルは LatticeAxes.new によって triangulate されるため、
|
333
|
-
# b 軸を反転させた時は a 軸も反転する。( b 軸の y成分を正にするため)
|
334
|
-
# c 軸を反転させた時は a, b 軸も反転する。( c 軸の z成分を正にするため)
|
335
|
-
#セルの形状、内部のモチーフは保存する。
|
336
|
-
#原子の絶対座標は移動せず、内部座標の表現が変わる。
|
337
|
-
#引数 axis_id は 0, 1, 2 のいずれかの値を取り、それぞれ x, y, z軸を表す。
|
338
|
-
#x, y, z軸の関係は、右手系と左手系が入れ替わる。
|
339
|
-
def inverse_axis!( axis_id )
|
340
|
-
axis_id = axis_id.to_i
|
341
|
-
raise CrystalCell::Cell::AxesRangeError if ( axis_id < 0 || 2 < axis_id )
|
342
|
-
|
343
|
-
axes = []
|
344
|
-
3.times do |i|
|
345
|
-
if ( i == axis_id )
|
346
|
-
axes << @axes[ i ] * (-1.0)
|
347
|
-
else
|
348
|
-
axes << @axes[ i ]
|
349
|
-
end
|
350
|
-
end
|
351
|
-
@axes = CrystalCell::LatticeAxes.new( axes )
|
352
|
-
|
353
|
-
atoms = []
|
354
|
-
@atoms.each do |atom|
|
355
|
-
position = []
|
356
|
-
3.times do |i|
|
357
|
-
if i == axis_id
|
358
|
-
position[i] = atom.position[i] * (-1)
|
213
|
+
|
214
|
+
v0 = pos0.to_v3d(@axes) if pos0.class == Vector3DInternal
|
215
|
+
v1 = pos1.to_v3d(@axes) if pos1.class == Vector3DInternal
|
216
|
+
|
217
|
+
(v0 - v1).r
|
218
|
+
end
|
219
|
+
|
220
|
+
#Dump string in POSCAR format.
|
221
|
+
#Argument <io> can be a file handle or nil.
|
222
|
+
#POSCAR を作るには、元素の順番を指定する必要があるので
|
223
|
+
#それを element_order で指定している。
|
224
|
+
#element_order の要素と == で一致する CrystalCell::Atom instance を
|
225
|
+
#それぞれ全て出力する。
|
226
|
+
#e.g.,
|
227
|
+
# cell.dump_poscar( STDOUT ) #=> output to stdout.
|
228
|
+
# cell.dump_poscar( fileIo ) #=> output to file.
|
229
|
+
# cell.dump_poscar( nil ) #=> return in String instance.
|
230
|
+
# cell.dump_poscar #=> return in String instance.
|
231
|
+
def dump_poscar( element_order, io = nil )
|
232
|
+
if (io == nil)
|
233
|
+
return create_poscar( element_order )
|
359
234
|
else
|
360
|
-
|
235
|
+
io.puts create_poscar( element_order )
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
#Cell rotation.( Destructive method)
|
240
|
+
#Argument 'matrix' is 3x3 Array of float.
|
241
|
+
#This method does not modify the position to the range between 0 and 1,
|
242
|
+
#even if it was out of range.
|
243
|
+
def rotate!( matrix )
|
244
|
+
@atoms.each { |atom|
|
245
|
+
old_pos = atom.position
|
246
|
+
new_pos = [0.0, 0.0, 0.0]
|
247
|
+
3.times do |y|
|
248
|
+
3.times do |x|
|
249
|
+
new_pos[y] += (matrix[y][x] * old_pos[x])
|
250
|
+
end
|
251
|
+
end
|
252
|
+
atom.set_position( new_pos )
|
253
|
+
}
|
254
|
+
end
|
255
|
+
|
256
|
+
#Cell rotation.( Nondestructive method)
|
257
|
+
#Argument 'matrix' is 3x3 Array of float.
|
258
|
+
#This method does not modify the position to the range between 0 and 1,
|
259
|
+
#even if it was out of range.
|
260
|
+
def rotate( matrix )
|
261
|
+
t = Marshal.load( Marshal.dump( self ) )
|
262
|
+
t.rotate!( matrix )
|
263
|
+
return t
|
264
|
+
end
|
265
|
+
|
266
|
+
#並進移動を行う破壊的メソッド。
|
267
|
+
#ary は Float 3 要素の配列。
|
268
|
+
def translate!( ary )
|
269
|
+
@atoms.each { |atom| atom.translate!( ary ) }
|
270
|
+
end
|
271
|
+
|
272
|
+
#並進移動を行う非破壊的メソッド。
|
273
|
+
#ary は Float 3 要素の配列。
|
274
|
+
def translate( ary )
|
275
|
+
t = Marshal.load( Marshal.dump( self ) )
|
276
|
+
t.translate!( ary )
|
277
|
+
return t
|
278
|
+
end
|
279
|
+
|
280
|
+
#Return arithmetic mean of atomic positions in an internal coordinates.
|
281
|
+
#Raise 'Cell::NoAtomError' if no atoms included in self.
|
282
|
+
def center_of_atoms
|
283
|
+
raise CrystalCell::Cell::NoAtomError if @atoms.size == 0
|
284
|
+
|
285
|
+
vec = Vector3DInternal[ 0.0, 0.0, 0.0 ]
|
286
|
+
@atoms.each { |i|
|
287
|
+
3.times { |j| vec[j] += i.position[j] }
|
288
|
+
}
|
289
|
+
vec *= 1.0/ @atoms.size
|
290
|
+
end
|
291
|
+
|
292
|
+
#Calculate volume.
|
293
|
+
def calc_volume
|
294
|
+
axes = @axes.to_a.map { |i| Vector3D[*i] }
|
295
|
+
vA, vB, vC = axes[0..2]
|
296
|
+
Vector3D.scalar_triple_product( vA, vB, vC ).abs
|
297
|
+
end
|
298
|
+
|
299
|
+
#Generate a new cell with the same lattice consants,
|
300
|
+
#containing atoms of indicated elements.
|
301
|
+
#Argument 'elems' must be an array of element names.
|
302
|
+
#含まれる @atoms の順序は、保存される。元素ごとに並び換えたりしない。
|
303
|
+
#CrystalCell::Atom.element が elems の要素のどれかと完全一致しているもののみ対象となる。
|
304
|
+
#サブクラスのインスタンスで実行した場合には、
|
305
|
+
#サブクラスのインスタンスとして生成する。
|
306
|
+
def cell_of_elements( elems )
|
307
|
+
result = self.class.new( @axes )
|
308
|
+
@atoms.each do |atom|
|
309
|
+
result.add_atom(atom) if elems.include?( atom.element )
|
361
310
|
end
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
# axes << @axes[ i ]
|
377
|
-
# end
|
378
|
-
#end
|
379
|
-
#@axes = CrystalCell::LatticeAxes.new( axes )
|
380
|
-
|
381
|
-
#atoms = []
|
382
|
-
#@atoms.each do |atom|
|
383
|
-
# position = []
|
384
|
-
# 3.times do |i|
|
385
|
-
# if i == axis_id
|
386
|
-
# position[i] = atom.position[i] * (-1)
|
387
|
-
# else
|
388
|
-
# position[i] = atom.position[i]
|
389
|
-
# end
|
390
|
-
# end
|
391
|
-
# atom.position
|
392
|
-
# atoms << CrystalCell::Atom.new( atom.element, position, atom.name, atom.movable_flags )
|
393
|
-
#end
|
394
|
-
#@atoms = atoms
|
395
|
-
#end
|
396
|
-
|
397
|
-
#inverse_axis! の非破壊版。
|
398
|
-
def inverse_axis( axis_id )
|
399
|
-
result = Marshal.load( Marshal.dump( self ) )
|
400
|
-
result.inverse_axis!( axis_id )
|
401
|
-
return result
|
402
|
-
end
|
403
|
-
|
404
|
-
#2つの格子ベクトルを交換する破壊的メソッド。
|
405
|
-
#Argument 'axis_ids' must have 2 items of integer.
|
406
|
-
#0, 1, and 2 mean x, y, and z axes, respectively.
|
407
|
-
#この範囲の整数でなければ例外 Cell::AxesRangeError.
|
408
|
-
#axis_ids に含まれる 2つの数字が同じならば
|
409
|
-
#例外 Cell::SameAxesError.
|
410
|
-
def exchange_axes!( axis_ids )
|
411
|
-
raise ArgumentError if axis_ids.size != 2
|
412
|
-
axis_ids.each{ |i| raise AxesRangeError if ( i < 0 || 2 < i ) }
|
413
|
-
raise CrystalCell::Cell::SameAxesError if ( axis_ids[0] == axis_ids[1] )
|
414
|
-
|
415
|
-
#格子定数を交換。
|
416
|
-
axes = @axes.axes
|
417
|
-
axes[ axis_ids[0]], axes[ axis_ids[1]] = axes[ axis_ids[1]], axes[ axis_ids[0]]
|
418
|
-
@axes = CrystalCell::LatticeAxes.new( axes )
|
419
|
-
|
420
|
-
#内部座標を交換。
|
421
|
-
new_atoms = []
|
422
|
-
@atoms.each do |atom|
|
423
|
-
new_pos = atom.position
|
424
|
-
new_pos[ axis_ids[0]], new_pos[ axis_ids[1]] =
|
425
|
-
new_pos[ axis_ids[1]], new_pos[ axis_ids[0]]
|
426
|
-
new_atoms << CrystalCell::Atom.new( atom.element, new_pos, atom.name, atom.movable_flags )
|
427
|
-
end
|
428
|
-
end
|
429
|
-
|
430
|
-
#exchange_axes! の非破壊版。
|
431
|
-
def exchange_axes( axis_ids )
|
432
|
-
result = Marshal.load( Marshal.dump( self ) )
|
433
|
-
result.exchange_axes!( axis_ids )
|
434
|
-
return result
|
435
|
-
end
|
436
|
-
|
437
|
-
#鏡像となるセルに変換する破壊的メソッド。
|
438
|
-
def reflect!
|
439
|
-
axes = @axes.to_a
|
440
|
-
axes[0][0] *= -1
|
441
|
-
@axes = CrystalCell::LatticeAxes.new( axes )
|
442
|
-
end
|
443
|
-
|
444
|
-
#鏡像となるセルに変換する非破壊的メソッド。
|
445
|
-
def reflect
|
446
|
-
result = Marshal.load( Marshal.dump( self ) )
|
447
|
-
result.reflect!
|
448
|
-
return result
|
449
|
-
end
|
450
|
-
|
451
|
-
#rotation と translation からなる操作(e.g., 対称操作)
|
452
|
-
#を加えたセルを返す。
|
453
|
-
def operate(rotation, translation)
|
454
|
-
rotation = Matrix[*rotation]
|
455
|
-
translation = translation.to_v3d
|
456
|
-
new_atoms = atoms.map do |atom|
|
457
|
-
position = atom.position.to_v3d([
|
458
|
-
[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0],
|
459
|
-
])
|
460
|
-
new_pos = (rotation * position + translation).to_a.to_v3di
|
461
|
-
CrystalCell::Atom.new(atom.element, new_pos, atom.name)
|
462
|
-
end
|
463
|
-
CrystalCell::Cell.new(@axes, new_atoms)
|
464
|
-
end
|
465
|
-
|
466
|
-
#Return information of axes symmetry.
|
467
|
-
#E.g.,
|
468
|
-
# [true , true , true ] when a = b = c, like cubic
|
469
|
-
# [true , false, false] when a = b != c, like hexagonal, trigonal, tetragonal
|
470
|
-
# [false, true , false] (same as above)
|
471
|
-
# [false, false, true ] (same as above)
|
472
|
-
# [false, false, false] when a != b != c, like triclinic, monoclinic, orthorhombic
|
473
|
-
def axis_independencies(symprec, angle_tolerance)
|
474
|
-
rotations = symmetry_operations(symprec, angle_tolerance).map {|oper| oper[:rotation]}
|
475
|
-
|
476
|
-
results = [true, true, true]
|
477
|
-
rotations.each do |rot|
|
478
|
-
3.times do |i|
|
479
|
-
3.times do |j|
|
480
|
-
next if rot[i][j] == 0
|
481
|
-
next if i == j
|
482
|
-
results[i] = false
|
483
|
-
results[j] = false
|
311
|
+
return result
|
312
|
+
end
|
313
|
+
|
314
|
+
#格子定数の同じ2つのセルを合わせて、全ての原子が含まれる1つのセルを返す
|
315
|
+
#非破壊的メソッド。
|
316
|
+
#2つのセルの格子定数が異なれば例外 Cell::AxesMismatchError を発生させる。
|
317
|
+
#内部的には @atoms はレシーバの @atoms のあとに引数の @atoms を追加した形になる。
|
318
|
+
#comment は空文字になる。
|
319
|
+
#原子座標の重複チェックなどは行わない。
|
320
|
+
def unite( cell )
|
321
|
+
#raise Cell::AxesMismatchError unless @axes == cell.axes
|
322
|
+
result = Marshal.load( Marshal.dump( self ) )
|
323
|
+
cell.atoms.each do |atom|
|
324
|
+
result.add_atom(atom)
|
484
325
|
end
|
485
|
-
|
326
|
+
return result
|
486
327
|
end
|
487
|
-
return results
|
488
|
-
end
|
489
328
|
|
490
|
-
|
329
|
+
#任意の格子軸のベクトルを反転する破壊的メソッド。
|
330
|
+
#大まかなイメージとしては、
|
331
|
+
#格子軸の原点をセルを構成する8つの頂点のどれかに移動する操作と考えれば良い。
|
332
|
+
# ただし厳密には、格子ベクトルは LatticeAxes.new によって triangulate されるため、
|
333
|
+
# b 軸を反転させた時は a 軸も反転する。( b 軸の y成分を正にするため)
|
334
|
+
# c 軸を反転させた時は a, b 軸も反転する。( c 軸の z成分を正にするため)
|
335
|
+
#セルの形状、内部のモチーフは保存する。
|
336
|
+
#原子の絶対座標は移動せず、内部座標の表現が変わる。
|
337
|
+
#引数 axis_id は 0, 1, 2 のいずれかの値を取り、それぞれ x, y, z軸を表す。
|
338
|
+
#x, y, z軸の関係は、右手系と左手系が入れ替わる。
|
339
|
+
def inverse_axis!( axis_id )
|
340
|
+
axis_id = axis_id.to_i
|
341
|
+
raise CrystalCell::Cell::AxesRangeError if ( axis_id < 0 || 2 < axis_id )
|
342
|
+
|
343
|
+
axes = []
|
344
|
+
3.times do |i|
|
345
|
+
if ( i == axis_id )
|
346
|
+
axes << @axes[ i ] * (-1.0)
|
347
|
+
else
|
348
|
+
axes << @axes[ i ]
|
349
|
+
end
|
350
|
+
end
|
351
|
+
@axes = CrystalCell::LatticeAxes.new( axes )
|
352
|
+
|
353
|
+
atoms = []
|
354
|
+
@atoms.each do |atom|
|
355
|
+
position = []
|
356
|
+
3.times do |i|
|
357
|
+
if i == axis_id
|
358
|
+
position[i] = atom.position[i] * (-1)
|
359
|
+
else
|
360
|
+
position[i] = atom.position[i]
|
361
|
+
end
|
362
|
+
end
|
363
|
+
atom.position = Vector3DInternal[*position]
|
364
|
+
end
|
365
|
+
end
|
491
366
|
|
492
|
-
|
493
|
-
|
494
|
-
#
|
495
|
-
|
496
|
-
#
|
497
|
-
#
|
498
|
-
#
|
367
|
+
#def inverse_axis!( axis_id )
|
368
|
+
#axis_id = axis_id.to_i
|
369
|
+
#raise Cell::AxesRangeError if ( axis_id < 0 || 2 < axis_id )
|
370
|
+
|
371
|
+
#axes = []
|
372
|
+
#3.times do |i|
|
373
|
+
# if ( i == axis_id )
|
374
|
+
# axes << @axes[ i ] * (-1.0)
|
375
|
+
# else
|
376
|
+
# axes << @axes[ i ]
|
377
|
+
# end
|
499
378
|
#end
|
379
|
+
#@axes = CrystalCell::LatticeAxes.new( axes )
|
380
|
+
|
381
|
+
#atoms = []
|
382
|
+
#@atoms.each do |atom|
|
383
|
+
# position = []
|
384
|
+
# 3.times do |i|
|
385
|
+
# if i == axis_id
|
386
|
+
# position[i] = atom.position[i] * (-1)
|
387
|
+
# else
|
388
|
+
# position[i] = atom.position[i]
|
389
|
+
# end
|
390
|
+
# end
|
391
|
+
# atom.position
|
392
|
+
# atoms << CrystalCell::Atom.new( atom.element, position, atom.name, atom.movable_flags )
|
393
|
+
#end
|
394
|
+
#@atoms = atoms
|
395
|
+
#end
|
396
|
+
|
397
|
+
#inverse_axis! の非破壊版。
|
398
|
+
def inverse_axis( axis_id )
|
399
|
+
result = Marshal.load( Marshal.dump( self ) )
|
400
|
+
result.inverse_axis!( axis_id )
|
401
|
+
return result
|
402
|
+
end
|
403
|
+
|
404
|
+
#2つの格子ベクトルを交換する破壊的メソッド。
|
405
|
+
#Argument 'axis_ids' must have 2 items of integer.
|
406
|
+
#0, 1, and 2 mean x, y, and z axes, respectively.
|
407
|
+
#この範囲の整数でなければ例外 Cell::AxesRangeError.
|
408
|
+
#axis_ids に含まれる 2つの数字が同じならば
|
409
|
+
#例外 Cell::SameAxesError.
|
410
|
+
def exchange_axes!( axis_ids )
|
411
|
+
raise ArgumentError if axis_ids.size != 2
|
412
|
+
axis_ids.each{ |i| raise AxesRangeError if ( i < 0 || 2 < i ) }
|
413
|
+
raise CrystalCell::Cell::SameAxesError if ( axis_ids[0] == axis_ids[1] )
|
414
|
+
|
415
|
+
#格子定数を交換。
|
416
|
+
axes = @axes.axes
|
417
|
+
axes[ axis_ids[0]], axes[ axis_ids[1]] = axes[ axis_ids[1]], axes[ axis_ids[0]]
|
418
|
+
@axes = CrystalCell::LatticeAxes.new( axes )
|
419
|
+
|
420
|
+
#内部座標を交換。
|
421
|
+
new_atoms = []
|
422
|
+
@atoms.each do |atom|
|
423
|
+
new_pos = atom.position
|
424
|
+
new_pos[ axis_ids[0]], new_pos[ axis_ids[1]] =
|
425
|
+
new_pos[ axis_ids[1]], new_pos[ axis_ids[0]]
|
426
|
+
new_atoms << CrystalCell::Atom.new( atom.element, new_pos, atom.name, atom.movable_flags )
|
427
|
+
end
|
428
|
+
end
|
429
|
+
|
430
|
+
#exchange_axes! の非破壊版。
|
431
|
+
def exchange_axes( axis_ids )
|
432
|
+
result = Marshal.load( Marshal.dump( self ) )
|
433
|
+
result.exchange_axes!( axis_ids )
|
434
|
+
return result
|
435
|
+
end
|
436
|
+
|
437
|
+
#鏡像となるセルに変換する破壊的メソッド。
|
438
|
+
def reflect!
|
439
|
+
axes = @axes.to_a
|
440
|
+
axes[0][0] *= -1
|
441
|
+
@axes = CrystalCell::LatticeAxes.new( axes )
|
442
|
+
end
|
443
|
+
|
444
|
+
#鏡像となるセルに変換する非破壊的メソッド。
|
445
|
+
def reflect
|
446
|
+
result = Marshal.load( Marshal.dump( self ) )
|
447
|
+
result.reflect!
|
448
|
+
return result
|
449
|
+
end
|
500
450
|
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
poss = positions.map {|pos| pos.to_a}
|
514
|
-
|
515
|
-
table = {}
|
516
|
-
types = elements.map do |elem|
|
517
|
-
table[elem] = ((table.size) +1) unless table.keys.include? elem
|
518
|
-
table[elem]
|
519
|
-
end
|
520
|
-
|
521
|
-
#pp axes_t, poss, types, symprec, angle_tolerance
|
522
|
-
|
523
|
-
spgnum, spg, hallnum, hall_symbol, t_mat, o_shift,
|
524
|
-
rotations, translations, wyckoffs =
|
525
|
-
#include Getspg
|
526
|
-
#pp Getspg.methods.sort
|
527
|
-
#Getspg::get_dataset(axes_t, poss, types, symprec, angle_tolerance)
|
528
|
-
get_dataset(axes_t, poss, types, symprec, angle_tolerance)
|
529
|
-
|
530
|
-
results = []
|
531
|
-
rotations.size.times do |index|
|
532
|
-
results << {
|
533
|
-
:rotation => rotations[index],
|
534
|
-
:translation => translations[index]
|
535
|
-
}
|
536
|
-
end
|
537
|
-
return results
|
538
|
-
end
|
539
|
-
|
540
|
-
#POSCAR の内容の文字列を生成。
|
541
|
-
#文字列の配列ではなく、改行文字を含む1つの文字列である点に注意。
|
542
|
-
#
|
543
|
-
#VASP の挙動として、Selective dynamics 指定ありの時に
|
544
|
-
#原子に T or F 指定していなかったり 3要素に足りなかったりすれば、
|
545
|
-
#error となって実行されない。
|
546
|
-
#なので dump_poscar では Selective dynamics 指定が必要な時には
|
547
|
-
#全ての原子に T/F を記述する。
|
548
|
-
#POSCAR から生成された Cell の場合は Selective dynamics がついていれば
|
549
|
-
#全ての原子に 3つの T/F が付いていることを前提としても良いだろう。
|
550
|
-
#原子を追加するとかで、一部の原子の movable_flags が nil になっているときは、
|
551
|
-
#デフォルト値として [ true, true, true ] を入れることにする。
|
552
|
-
#nil ならば false を連想すると思うが、敢えて true で埋めている理由は、
|
553
|
-
#Selective dynamics をつけていない状態で VASP は全方向に緩和する、
|
554
|
-
#すなわち T T T と指定したことと同じになっているから。
|
555
|
-
#換言すればこのデフォルト値の設定は VASP に合わせた仕様ということになる。
|
556
|
-
#VASP に由来する仕様が Cell クラスに持ち込まれていることになるが、
|
557
|
-
#VASP へのインターフェイスである POSCAR ファイルへの書き出しに限定されるので
|
558
|
-
#他への影響はほとんどなく、気にしなくて良いだろう。
|
559
|
-
def create_poscar( element_order )
|
560
|
-
#element_order と elements が一対一対応していなければ raise
|
561
|
-
raise "Cell::create_poscar, element_order mismatches to elements." if (! Mapping::map?( elements.uniq, element_order ){ |i, j| i == j } )
|
562
|
-
|
563
|
-
results = []
|
564
|
-
results << @comment
|
565
|
-
results << "1.0" #scale
|
566
|
-
3.times do |i|
|
567
|
-
results << sprintf( "%20.14f %20.14f %20.14f", @axes[i][0], @axes[i][1], @axes[i][2]
|
568
|
-
)
|
569
|
-
end
|
570
|
-
|
571
|
-
##collect information
|
572
|
-
elem_list = Hash.new
|
573
|
-
element_order.each do |elem|
|
574
|
-
elem_list[ elem ] = @atoms.select{ |atom| atom.element == elem }
|
575
|
-
end
|
576
|
-
|
577
|
-
##element symbols
|
578
|
-
results << " " + element_order.join(" ")
|
579
|
-
|
580
|
-
##numbers of element atoms
|
581
|
-
tmp = ''
|
582
|
-
element_order.each do |elem|
|
583
|
-
tmp += " #{elem_list[elem].size.to_s}"
|
584
|
-
end
|
585
|
-
results << tmp
|
586
|
-
|
587
|
-
##Selective dynamics
|
588
|
-
##どれか1つでも getMovableFlag が真であれば Selective dynamics をオンにする
|
589
|
-
selective_dynamics = false
|
590
|
-
@atoms.each do |atom|
|
591
|
-
if atom.movable_flags
|
592
|
-
selective_dynamics = true
|
593
|
-
results << "Selective dynamics"
|
594
|
-
break
|
595
|
-
end
|
596
|
-
end
|
597
|
-
|
598
|
-
element_order.each do |elem|
|
599
|
-
elem_list[ elem ].each do |atom|
|
600
|
-
if atom.movable_flags
|
601
|
-
selective_dynamics = true
|
602
|
-
break
|
451
|
+
#rotation と translation からなる操作(e.g., 対称操作)
|
452
|
+
#を加えたセルを返す。
|
453
|
+
def operate(rotation, translation)
|
454
|
+
rotation = Matrix[*rotation]
|
455
|
+
translation = translation.to_v3d
|
456
|
+
new_atoms = atoms.map do |atom|
|
457
|
+
position = atom.position.to_v3d([
|
458
|
+
[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0],
|
459
|
+
])
|
460
|
+
new_pos = (rotation * position + translation).to_a.to_v3di
|
461
|
+
CrystalCell::Atom.new(atom.element, new_pos, atom.name)
|
603
462
|
end
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
463
|
+
CrystalCell::Cell.new(@axes, new_atoms)
|
464
|
+
end
|
465
|
+
|
466
|
+
#Return information of axes symmetry.
|
467
|
+
#E.g.,
|
468
|
+
# [true , true , true ] when a = b = c, like cubic
|
469
|
+
# [true , false, false] when a = b != c, like hexagonal, trigonal, tetragonal
|
470
|
+
# [false, true , false] (same as above)
|
471
|
+
# [false, false, true ] (same as above)
|
472
|
+
# [false, false, false] when a != b != c, like triclinic, monoclinic, orthorhombic
|
473
|
+
def axis_independencies(symprec, angle_tolerance)
|
474
|
+
rotations = symmetry_operations(symprec, angle_tolerance).map {|oper| oper[:rotation]}
|
475
|
+
|
476
|
+
results = [true, true, true]
|
477
|
+
rotations.each do |rot|
|
478
|
+
3.times do |i|
|
479
|
+
3.times do |j|
|
480
|
+
next if rot[i][j] == 0
|
481
|
+
next if i == j
|
482
|
+
results[i] = false
|
483
|
+
results[j] = false
|
484
|
+
end
|
620
485
|
end
|
621
|
-
end
|
622
486
|
end
|
623
|
-
results
|
624
|
-
|
487
|
+
return results
|
488
|
+
end
|
489
|
+
|
490
|
+
private
|
491
|
+
|
492
|
+
#Return rotations and translation of symmetry operations.
|
493
|
+
def symmetry_operations(symprec, angle_tolerance)
|
494
|
+
#begin
|
495
|
+
# require "getspg.so"
|
496
|
+
#rescue LoadError
|
497
|
+
# raise LoadError,
|
498
|
+
# "LoadError: 'spglib' seems not to be installed into the system."
|
499
|
+
#end
|
500
|
+
|
501
|
+
unless defined? Getspg
|
502
|
+
raise NoSpglibError, "symmetry_operations() is called without spglib."
|
503
|
+
end
|
504
|
+
|
505
|
+
#pp lattice # => [[2.0, 0.0, 0.0], [1.2246063538223773e-16, 2.0, 0.0], [1.2246063538223773e-16, 1.2246063538223773e-16, 2.0]]
|
506
|
+
##vasp の lattice 行と比べて転置しているのに注意。
|
507
|
+
#pp position #=>[[0.0, 0.0, 0.0], [0.5, 0.0, 0.0], [0.5, 0.5, 0.0], [0.5, 0.5, 0.5]]
|
508
|
+
#pp types #=>[1, 2, 3, 3]
|
509
|
+
#pp symprec #=>1.0e-05
|
510
|
+
#pp angle_tolerance #=>-1.0
|
511
|
+
axes_t = @axes.to_a.transpose
|
512
|
+
|
513
|
+
poss = positions.map {|pos| pos.to_a}
|
514
|
+
|
515
|
+
table = {}
|
516
|
+
types = elements.map do |elem|
|
517
|
+
table[elem] = ((table.size) +1) unless table.keys.include? elem
|
518
|
+
table[elem]
|
519
|
+
end
|
520
|
+
|
521
|
+
#pp axes_t, poss, types, symprec, angle_tolerance
|
522
|
+
|
523
|
+
spgnum, spg, hallnum, hall_symbol, t_mat, o_shift,
|
524
|
+
rotations, translations, wyckoffs =
|
525
|
+
#include Getspg
|
526
|
+
#pp Getspg.methods.sort
|
527
|
+
#Getspg::get_dataset(axes_t, poss, types, symprec, angle_tolerance)
|
528
|
+
get_dataset(axes_t, poss, types, symprec, angle_tolerance)
|
529
|
+
|
530
|
+
results = []
|
531
|
+
rotations.size.times do |index|
|
532
|
+
results << {
|
533
|
+
:rotation => rotations[index],
|
534
|
+
:translation => translations[index]
|
535
|
+
}
|
536
|
+
end
|
537
|
+
return results
|
625
538
|
end
|
626
539
|
|
627
|
-
|
628
|
-
|
540
|
+
#POSCAR の内容の文字列を生成。
|
541
|
+
#文字列の配列ではなく、改行文字を含む1つの文字列である点に注意。
|
542
|
+
#
|
543
|
+
#VASP の挙動として、Selective dynamics 指定ありの時に
|
544
|
+
#原子に T or F 指定していなかったり 3要素に足りなかったりすれば、
|
545
|
+
#error となって実行されない。
|
546
|
+
#なので dump_poscar では Selective dynamics 指定が必要な時には
|
547
|
+
#全ての原子に T/F を記述する。
|
548
|
+
#POSCAR から生成された Cell の場合は Selective dynamics がついていれば
|
549
|
+
#全ての原子に 3つの T/F が付いていることを前提としても良いだろう。
|
550
|
+
#原子を追加するとかで、一部の原子の movable_flags が nil になっているときは、
|
551
|
+
#デフォルト値として [ true, true, true ] を入れることにする。
|
552
|
+
#nil ならば false を連想すると思うが、敢えて true で埋めている理由は、
|
553
|
+
#Selective dynamics をつけていない状態で VASP は全方向に緩和する、
|
554
|
+
#すなわち T T T と指定したことと同じになっているから。
|
555
|
+
#換言すればこのデフォルト値の設定は VASP に合わせた仕様ということになる。
|
556
|
+
#VASP に由来する仕様が Cell クラスに持ち込まれていることになるが、
|
557
|
+
#VASP へのインターフェイスである POSCAR ファイルへの書き出しに限定されるので
|
558
|
+
#他への影響はほとんどなく、気にしなくて良いだろう。
|
559
|
+
def create_poscar( element_order )
|
560
|
+
#element_order と elements が一対一対応していなければ raise
|
561
|
+
raise "Cell::create_poscar, element_order mismatches to elements." if (! Mapping::map?( elements.uniq, element_order ){ |i, j| i == j } )
|
562
|
+
|
563
|
+
results = []
|
564
|
+
results << @comment
|
565
|
+
results << "1.0" #scale
|
566
|
+
3.times do |i|
|
567
|
+
results << sprintf( "%20.14f %20.14f %20.14f", @axes[i][0], @axes[i][1], @axes[i][2]
|
568
|
+
)
|
569
|
+
end
|
570
|
+
|
571
|
+
##collect information
|
572
|
+
elem_list = Hash.new
|
573
|
+
element_order.each do |elem|
|
574
|
+
elem_list[ elem ] = @atoms.select{ |atom| atom.element == elem }
|
575
|
+
end
|
576
|
+
|
577
|
+
##element symbols
|
578
|
+
results << " " + element_order.join(" ")
|
579
|
+
|
580
|
+
##numbers of element atoms
|
581
|
+
tmp = ''
|
582
|
+
element_order.each do |elem|
|
583
|
+
tmp += " #{elem_list[elem].size.to_s}"
|
584
|
+
end
|
585
|
+
results << tmp
|
586
|
+
|
587
|
+
##Selective dynamics
|
588
|
+
##どれか1つでも getMovableFlag が真であれば Selective dynamics をオンにする
|
589
|
+
selective_dynamics = false
|
590
|
+
@atoms.each do |atom|
|
591
|
+
if atom.movable_flags
|
592
|
+
selective_dynamics = true
|
593
|
+
results << "Selective dynamics"
|
594
|
+
break
|
595
|
+
end
|
596
|
+
end
|
597
|
+
|
598
|
+
element_order.each do |elem|
|
599
|
+
elem_list[ elem ].each do |atom|
|
600
|
+
if atom.movable_flags
|
601
|
+
selective_dynamics = true
|
602
|
+
break
|
603
|
+
end
|
604
|
+
end
|
605
|
+
break if selective_dynamics
|
606
|
+
end
|
607
|
+
|
608
|
+
results << "Direct" #now, Direct only
|
609
|
+
|
610
|
+
##positions of atoms
|
611
|
+
element_order.each do |elem|
|
612
|
+
elem_list[ elem ].each do |atom|
|
613
|
+
tmp = sprintf( "%20.14f %20.14f %20.14f", * atom.position )
|
614
|
+
if selective_dynamics
|
615
|
+
if atom.movable_flags == nil
|
616
|
+
tmp += " T T T"
|
617
|
+
else
|
618
|
+
atom.movable_flags.each do |mov|
|
619
|
+
( mov == true ) ? tmp += " T" : tmp += " F"
|
620
|
+
end
|
621
|
+
end
|
622
|
+
end
|
623
|
+
results << tmp
|
624
|
+
end
|
625
|
+
end
|
626
|
+
|
627
|
+
results.join("\n")
|
628
|
+
end
|
629
629
|
|
630
630
|
end
|