class
Chem::Spatial::Grid
- Chem::Spatial::Grid
- Reference
- Object
Overview
Generate .read and #write methods on readable/writable types
Included Modules
Defined in:
chem/register_format.crchem/spatial/grid.cr
Constructors
- .[](ni : Int, nj : Int, nk : Int) : self
- .atom_distance(structure : Structure, dim : Dimensions, bounds : Parallelepiped | Nil = nil) : self
-
.atom_distance_like(other : self | Info, structure : Structure) : self
Returns a grid filled with the distances to the nearest atom.
-
.build(info : Info, source_file : String | Path | Nil = nil, & : Pointer(Float64), Int32 -> ) : self
Creates a new
Gridwith info and yields a buffer to be filled. - .build(dim : Dimensions, bounds : Parallelepiped, source_file : String | Path | Nil = nil, & : Pointer(Float64), Int32 -> ) : self
-
.empty_like(other : self | Info) : self
Returns a zero-filled grid with the same bounds and points as other.
-
.fill_like(other : self | Info, value : Number) : self
Returns a grid with the same bounds and points as other filled with value.
- .from_chgcar(io : IO) : self
- .from_chgcar(path : Path | String) : self
- .from_cube(io : IO) : self
- .from_cube(path : Path | String) : self
- .from_dx(io : IO) : self
- .from_dx(path : Path | String) : self
-
.from_io(io : IO, format : IO::ByteFormat) : self
Reads a grid from io in the given format.
- .from_locpot(io : IO) : self
- .from_locpot(path : Path | String) : self
- .new(dim : Dimensions, bounds : Parallelepiped, source_file : String | Path | Nil = nil)
- .new(dim : Dimensions, bounds : Parallelepiped, initial_value : Float64, source_file : String | Path | Nil = nil)
- .new(dim : Dimensions, bounds : Parallelepiped, source_file : String | Path | Nil = nil, &block : Location -> Number)
- .read(input : IO | Path | String, format : Chem::Cube.class) : self
- .read(input : IO | Path | String, format : Chem::DX.class) : self
- .read(input : IO | Path | String, format : Chem::VASP::Chgcar.class) : self
- .read(input : IO | Path | String, format : Chem::VASP::Locpot.class) : self
-
.read(input : IO | Path | String, format) : self
FIXME Make it compile time error.
- .read(path : IO | Path | String) : self
-
.vdw_mask(structure : Structure, dim : Dimensions, bounds : Parallelepiped | Nil = nil, delta : Float64 = 0.02) : self
TODO add more tests FIXME: check delta calculation (grid.resolution.min / 2 shouldn't be enough?)
-
.vdw_mask_like(other : self | Info, structure : Structure, delta : Float64 = 0.02) : self
Returns a grid mask with the points at the vdW spheres set to 1.
Instance Method Summary
- #*(rhs : Number) : self
- #*(rhs : self) : self
- #+(rhs : Number) : self
- #+(rhs : self) : self
- #-(rhs : Number) : self
- #-(rhs : self) : self
- #/(rhs : Number) : self
- #/(rhs : self) : self
-
#==(rhs : self) : Bool
Returns
trueif this reference is the same as other. - #[](*args, **options) : Float64
- #[]=(i : Int, j : Int, k : Int, value : Float64) : Float64
- #[]=(i : Int, value : Float64) : Float64
- #[]=(loc : Location, value : Float64) : Float64
- #[]?(i : Int, j : Int, k : Int) : Float64 | Nil
- #[]?(loc : Location) : Float64 | Nil
-
#[]?(vec : Vec3) : Float64 | Nil
TODO add interpolation (check ARBInterp)
- #bounds : Parallelepiped
- #dim : Dimensions
-
#dup : self
Returns a shallow copy of this object.
-
#each_axial_slice(axis : Int, reuse : Bool | Array(Float64) = false, & : Array(Float64) -> ) : Nil
Iterates over slices along axis.
- #each_loc(vec : Vec3, cutoff : Number, & : Location, Float64 -> ) : Nil
- #each_loc(& : Location -> ) : Nil
- #each_pos(& : Vec3 -> ) : Nil
- #each_with_loc(& : Float64, Location -> ) : Nil
- #each_with_pos(& : Float64, Vec3 -> ) : Nil
- #includes?(*args, **options)
- #includes?(*args, **options, &)
- #index(loc : Location) : Int32 | Nil
- #index(vec : Vec3) : Int32 | Nil
- #index!(*args, **options) : Int32
- #loc_at(*args, **options) : Location
- #loc_at?(i : Int) : Location | Nil
- #loc_at?(vec : Vec3) : Location | Nil
-
#map(& : Float64 -> Float64) : self
Returns an
Arraywith the results of running the block against each element of the collection. - #map!(& : Float64 -> Float64) : self
-
#map_with_index(& : Float64, Int32 -> Number) : self
Like
#map, but the block gets passed both the element and its index. - #map_with_index!(& : Float64, Int32 -> Number) : self
- #map_with_loc(& : Float64, Location -> Number) : self
- #map_with_loc!(& : Float64, Location -> Number) : self
- #map_with_pos(& : Float64, Vec3 -> Number) : self
- #map_with_pos!(& : Float64, Vec3 -> Number) : self
-
#mask(& : Float64 -> Bool) : self
Returns a grid mask.
-
#mask(value : Number, delta : Number) : self
Returns a grid mask.
-
#mask(pattern) : self
Returns a grid mask.
-
#mask!(& : Float64 -> Bool) : self
Masks a grid by the passed block.
-
#mask!(value : Number, delta : Number) : self
Masks a grid by value+/-delta.
-
#mask!(pattern) : self
Masks a grid by pattern.
-
#mask_by_index(& : Int32 -> Bool) : self
Returns a grid mask.
-
#mask_by_index!(& : Int32 -> Bool) : self
Masks a grid by index.
-
#mask_by_loc(& : Location -> Bool) : self
Returns a grid mask.
-
#mask_by_loc!(& : Location -> Bool) : self
Masks a grid by location.
-
#mask_by_pos(& : Vec3 -> Bool) : self
Returns a grid mask.
-
#mask_by_pos!(& : Vec3 -> Bool) : self
Masks a grid by coordinates.
-
#mean(axis : Int) : Array(Float64)
Returns the arithmetic mean along axis.
-
#mean : Float64
Returns the arithmetic mean of the grid elements.
-
#mean_with_pos(axis : Int) : Array(Tuple(Float64, Float64))
Returns the arithmetic mean along axis with its coordinates.
- #ni : Int32
- #nj : Int32
- #nk : Int32
- #origin(*args, **options)
- #origin(*args, **options, &)
- #pos_at(*args, **options) : Vec3
- #pos_at?(i : Int, j : Int, k : Int) : Vec3 | Nil
- #pos_at?(i : Int) : Vec3 | Nil
- #pos_at?(loc : Location) : Vec3 | Nil
- #resolution : FloatTriple
-
#size : Int32
Returns the number of elements in this container.
- #source_file : Path | Nil
- #step(di : Int, dj : Int, dk : Int) : self
- #step(n : Int) : self
-
#to_a : Array(Float64)
Returns an
Arraywith all the elements in the collection. - #to_chgcar(io : IO, struc : Structure) : Nil
- #to_chgcar(path : Path | String, struc : Structure) : Nil
- #to_chgcar(struc : Structure) : String
- #to_cube(io : IO, atoms : AtomView | Structure) : Nil
- #to_cube(path : Path | String, atoms : AtomView | Structure) : Nil
- #to_cube(atoms : AtomView | Structure) : String
- #to_dx(io : IO) : Nil
- #to_dx(path : Path | String) : Nil
- #to_dx : String
-
#to_io(io : IO, format : IO::ByteFormat = :system_endian) : Nil
Writes the binary representation of the grid to io in the given format.
- #to_locpot(io : IO, struc : Structure) : Nil
- #to_locpot(path : Path | String, struc : Structure) : Nil
- #to_locpot(struc : Structure) : String
- #to_unsafe : Pointer(Float64)
-
#unsafe_fetch(index : Int) : Float64
Returns the element at the given index, without doing any bounds check.
- #unsafe_fetch(loc : Location) : Float64
- #volume(*args, **options)
- #volume(*args, **options, &)
- #write(output : IO | Path | String, format : Chem::Cube.class) : Nil
- #write(output : IO | Path | String, format : Chem::DX.class) : Nil
- #write(output : IO | Path | String, format : Chem::VASP::Chgcar.class) : Nil
- #write(output : IO | Path | String, format : Chem::VASP::Locpot.class) : Nil
-
#write(output : IO | Path | String, format) : Nil
FIXME Make it compile time error.
- #write(path : IO | Path | String) : Nil
Instance methods inherited from module Indexable(Float64)
[](idxs : Tuple) : Tuple[](idxs : Enumerable(Int)) : Array(T)
[](*idxs : Int) : Tuple [], sentence(io : IO, separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ", & : T, IO -> ) : Nil
sentence(io : IO, separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ") : Nil
sentence(separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ", & : T -> ) : String
sentence(separator : String = ", ", *, pair_separator : String = " and ", tail_separator : String = ", and ") : String sentence, to_dcd(io : IO, title : String | Nil = nil) : Nil
to_dcd(title : String | Nil = nil) : String
to_dcd(path : Path | String, title : String | Nil = nil) : Nil to_dcd
Instance methods inherited from module Enumerable(Float64)
===(atom : Chem::Atom) : Bool===(chain : Chem::Chain) : Bool
===(residue : Chem::Residue) : Bool ===, average(weights : Indexable(Number))
average(weights : Indexable(Number), & : T -> _) average, find(pattern, if_none default = nil) find, find!(pattern) find!, mean
mean(& : T -> _) mean, to_dcd(io : IO, title : String | Nil = nil) : Nil
to_dcd(title : String | Nil = nil) : String
to_dcd(path : Path | String, title : String | Nil = nil) : Nil to_dcd, to_mol2(io : IO) : Nil
to_mol2(path : Path | String) : Nil
to_mol2 : String to_mol2, to_pdb(io : IO, conect conect_options : PDB::ConectOptions = PDB::ConectOptions.flags(Het, Disulfide), renumber : Bool = true, ter_on_fragment : Bool = false) : Nil
to_pdb(conect conect_options : PDB::ConectOptions = PDB::ConectOptions.flags(Het, Disulfide), renumber : Bool = true, ter_on_fragment : Bool = false) : String
to_pdb(path : Path | String, conect conect_options : PDB::ConectOptions = PDB::ConectOptions.flags(Het, Disulfide), renumber : Bool = true, ter_on_fragment : Bool = false) : Nil to_pdb, to_sdf(io : IO, variant : Mol::Variant = :v2000) : Nil
to_sdf(path : Path | String, variant : Mol::Variant = :v2000) : Nil
to_sdf(variant : Mol::Variant = :v2000) : String to_sdf, to_xyz(io : IO, extended : Bool = false, fields : Array(String) = [] of String) : Nil
to_xyz(extended : Bool = false, fields : Array(String) = [] of String) : String
to_xyz(path : Path | String, extended : Bool = false, fields : Array(String) = [] of String) : Nil to_xyz
Constructor Detail
Returns a grid filled with the distances to the nearest atom. It will have the same bounds and points as other.
structure = Structure.read "path/to/file"
info = Grid::Info.new Parallelepiped[1.5, 2.135, 6.12], {10, 10, 10}
grid = Grid.atom_distance structure, info.dim, info.bounds
Grid.atom_distance_like(info, structure) == grid # => true
Creates a new Grid with info and yields a buffer to be filled.
This method is unsafe, but it is usually used to initialize the buffer in a linear fashion, e.g., reading values from a file.
Grid.build(Grid.info("/path/to/file")) do |buffer, size|
size.times do |i|
buffer[i] = read_value
end
end
Returns a zero-filled grid with the same bounds and points as other.
grid = Grid.from_dx "/path/to/grid"
other = Grid.empty_like grid
other.bounds == grid.bounds # => true
other.dim == grid.dim # => true
other.to_a.minmax # => {0.0, 0.0}
Returns a grid with the same bounds and points as other filled with value.
grid = Grid.from_dx "/path/to/grid"
other = Grid.fill_like grid, 2345.123
other.bounds == grid.bounds # => true
other.dim == grid.dim # => true
other.to_a.minmax # => {2345.123, 2345.123}
Reads a grid from io in the given format. See also:
IO#read_bytes. Raises IO::EOFError if there is missing data.
FIXME Make it compile time error. Should raise if format is not registered or format is incompatible with open_type. Better create an override with the other formats, and leave this as a fallback.
TODO add more tests
FIXME check delta calculation (grid.resolution.min / 2 shouldn't be enough?)
Returns a grid mask with the points at the vdW spheres set to 1. It will have the same bounds and points as other.
structure = Structure.read "path/to/file"
info = Grid::Info.new Parallelepiped[5.213, 6.823, 10.352], {20, 25, 40}
grid = Grid.vdw_mask structure, info.dim, info.bounds
Grid.vdw_mask_like(info, structure) == grid # => true
Instance Method Detail
Returns true if this reference is the same as other. Invokes same?.
Returns a shallow copy of this object.
This allocates a new object and copies the contents of
self into it.
Iterates over slices along axis. Axis is specified as an integer: 0-2 refer to the direction of the first, second or third basis vector, respectively.
grid = Grid.new({2, 3, 2}, Parallelepiped[1, 1, 1]) { |i, j, k| i * 6 + j * 2 + k }
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
slices = [] of Array(Float64)
grid.each_axial_slice(axis: 1) { |slice| slices << slice }
slices # => [[0, 1, 6, 7], [2, 3, 8, 9], [4, 5, 10, 11]]
When using read-only slices, one can specify the reuse option to prevent many memory allocations:
- If reuse is an
Array, this array will be reused. - If reuse is true, the method will create a new array and reuse it.
- If reuse is false (default), a new array is created and yielded on each iteration.
Returns an Array with the results of running the block against each element of the collection.
[1, 2, 3].map { |i| i * 10 } # => [10, 20, 30]
Like #map, but the block gets passed both the element and its index.
["Alice", "Bob"].map_with_index { |name, i| "User ##{i}: #{name}" }
# => ["User #0: Alice", "User #1: Bob"]
Accepts an optional offset parameter, which tells it to start counting from there.
Returns a grid mask. Elements for which the passed block returns true are set to
1, otherwise 0.
Grid masks are very useful to deal with multiple grids, and when points are to be selected based on one grid only.
grid = Grid.new({2, 2, 2}, Parallelepiped[10, 10, 10]) { |i, j, k| i + j + k }
grid.to_a # => [0, 1, 1, 2, 1, 2, 2, 3]
grid.mask(&.>(1)).to_a # => [0, 0, 0, 1, 0, 1, 1, 1]
grid.to_a # => [0, 1, 1, 2, 1, 2, 2, 3]
Returns a grid mask. Elements for which (value - ele).abs <= delta returns
true are set to 1, otherwise 0.
Grid masks are very useful to deal with multiple grids, and when points are to be selected based on one grid only.
grid = Grid.new({2, 2, 3}, Parallelepiped[1, 1, 1]) { |i, j, k| (i + 1) * (j + 1) * (k + 1) / 5 }
grid.to_a # => [0.2, 0.4, 0.6, 0.4, 0.8, 1.2, 0.4, 0.8, 1.2, 0.8, 1.6, 2.4]
grid.mask(1, 0.5).to_a # => [0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0]
grid.to_a # => [0.2, 0.4, 0.6, 0.4, 0.8, 1.2, 0.4, 0.8, 1.2, 0.8, 1.6, 2.4]
Returns a grid mask. Elements for which pattern === element returns true are
set to 1, otherwise 0.
Grid masks are very useful to deal with multiple grids, and when points are to be selected based on one grid only.
grid = Grid.new({2, 2, 3}, Parallelepiped[1, 1, 1]) { |i, j, k| (i + 1) * (j + 1) * (k + 1) }
grid.to_a # => [1, 2, 3, 2, 4, 6, 2, 4, 6, 4, 8, 12]
grid.mask(2..4.5).to_a # => [0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0]
grid.to_a # => [1, 2, 3, 2, 4, 6, 2, 4, 6, 4, 8, 12]
Masks a grid by the passed block. Elements for which the passed block returns
false are set to 0.
Optimized version of creating a mask and applying it to the same grid, but avoids
creating intermediate grids. This is equivalent to grid = grid * grid.mask { ... }.
grid = Grid.new({2, 2, 2}, Parallelepiped[10, 10, 10]) { |i, j, k| i + j + k }
grid.to_a # => [0, 1, 1, 2, 1, 2, 2, 3]
grid.mask! &.>(1)
grid.to_a # => [0, 0, 0, 2, 0, 2, 2, 3]
Masks a grid by value+/-delta. Elements for which (value - ele).abs > delta
returns true are set to 0.
Optimized version of creating a mask and applying it to the same grid, but avoids
creating intermediate grids. This is equivalent to grid = grid * grid.mask(value, delta)
grid = Grid.new({2, 2, 3}, Parallelepiped[1, 1, 1]) { |i, j, k| (i + j + k) / 5 }
grid.to_a # => [0.0, 0.2, 0.4, 0.2, 0.4, 0.6, 0.2, 0.4, 0.6, 0.4, 0.6, 0.8]
grid.mask! 0.5, 0.1
grid.to_a # => [0.0, 0.0, 0.4, 0.0, 0.4, 0.6, 0.0, 0.4, 0.6, 0.4, 0.6, 0.0]
Masks a grid by pattern. Elements for which pattern === element returns
false are set to 0.
Optimized version of creating a mask and applying it to the same grid, but avoids
creating intermediate grids. This is equivalent to grid = grid * grid.mask(pattern)
grid = Grid.new({2, 2, 3}, Parallelepiped[1, 1, 1]) { |i, j, k| (i + 1) * (j + 1) * (k + 1) }
grid.to_a # => [1, 2, 3, 2, 4, 6, 2, 4, 6, 4, 8, 12]
grid.mask! 2..4.5
grid.to_a # => [0, 2, 3, 2, 4, 0, 2, 4, 0, 4, 0, 0]
Returns a grid mask. Indexes for which the passed block returns true are set to
1, otherwise 0.
Grid masks are very useful to deal with multiple grids, and when points are to be selected based on one grid only.
grid = Grid.new({2, 2, 2}, Parallelepiped[10, 10, 10]) { |i, j, k| i * 4 + j * 2 + k }
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7]
grid.mask_by_index(&.>(4)).to_a # => [0, 0, 0, 0, 0, 1, 1, 1]
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7]
Masks a grid by index. Indexes for which the passed block returns false are set
to 0.
Optimized version of creating a mask and applying it to the same grid, but avoids
creating intermediate grids. This is equivalent to grid = grid * grid.mask_by_index { ... }
grid = Grid.new({2, 2, 2}, Parallelepiped[1, 1, 1]) { |i, j, k| i * 4 + j * 2 + k }
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7]
grid.mask_by_index! &.<(3)
grid.to_a # => [0, 1, 2, 0, 0, 0, 0, 0]
Returns a grid mask. Locations for which the passed block returns true are set
to 1, otherwise 0.
Grid masks are very useful to deal with multiple grids, and when points are to be selected based on one grid only.
grid = Grid.new({2, 2, 2}, Parallelepiped[10, 10, 10]) { |i, j, k| i * 4 + j * 2 + k }
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7]
grid.mask_by_loc { |i, j, k| k == 1 }.to_a # => [0, 1, 0, 1, 0, 1, 0, 1]
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7]
Masks a grid by location. Locations for which the passed block returns false are
set to 0.
Optimized version of creating a mask and applying it to the same grid, but avoids
creating intermediate grids. This is equivalent to grid = grid * grid.mask_by_loc { ... }
grid = Grid.new({2, 2, 2}, Parallelepiped[1, 1, 1]) { |i, j, k| i * 4 + j * 2 + k }
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7]
grid.mask_by_loc! { |(i, j, k)| i == 1 }
grid.to_a # => [0, 0, 0, 0, 4, 5, 6, 7]
Returns a grid mask. Coordinates for which the passed block returns true are set
to 1, otherwise 0.
Grid masks are very useful to deal with multiple grids, and when points are to be selected based on one grid only.
grid = Grid.new({2, 2, 2}, Parallelepiped[10, 10, 10]) { |i, j, k| i * 4 + j * 2 + k }
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7]
grid.mask_by_pos(&.x.==(0)).to_a # => [1, 1, 1, 1, 0, 0, 0, 0]
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7]
Masks a grid by coordinates. Coordinates for which the passed block returns
false are set to 0.
Optimized version of creating a mask and applying it to the same grid, but avoids
creating intermediate grids. This is equivalent to grid = grid * grid.mask_by_pos { ... }
grid = Grid.new({2, 2, 2}, Parallelepiped[5, 5, 5]) { |i, j, k| i * 4 + j * 2 + k }
grid.to_a # => [0, 1, 2, 3, 4, 5, 6, 7]
grid.mask_by_pos! { |vec| vec.y == 5 }
grid.to_a # => [0, 0, 2, 3, 0, 0, 6, 7]
Returns the arithmetic mean along axis. Axis is specified as an integer: 0-2 refer to the direction of the first, second or third basis vector, respectively.
Raises IndexError is axis is out of bounds.
grid = Grid.new({2, 3, 4}, Parallelepiped[1, 1, 1]) { |i, j, k| i * 12 + j * 4 + k }
grid.mean(axis: 0) # => [5.5, 17.5]
grid.mean(axis: 1) # => [7.5, 11.5, 15.5]
grid.mean(axis: 2) # => [10, 11, 12, 13]
grid.mean(axis: 3) # raises IndexError
Returns the arithmetic mean of the grid elements.
grid = Grid.new({2, 3, 4}, Parallelepiped[1, 1, 1]) { |i, j, k| i * 12 + j * 4 + k }
grid.mean # => 11.5
Returns the arithmetic mean along axis with its coordinates. Axis is specified as an integer: 0-2 refer to the direction of the first, second or third basis vector, respectively.
Raises IndexError is axis is out of bounds.
grid = Grid.new({2, 3, 5}, Parallelepiped[1, 1, 1]) { |i, j, k| i * 12 + j * 4 + k }
grid.mean(axis: 1) # => [{9.5, 0.0}, {14.5, 0.5}, {19.5, 1.0}]
Returns the number of elements in this container.
Returns an Array with all the elements in the collection.
(1..5).to_a # => [1, 2, 3, 4, 5]
Writes the binary representation of the grid to io in the given
format. See also IO#write_bytes. Raises ArgumentError is the
encoding is not UTF-8.
Returns the element at the given index, without doing any bounds check.
Indexable makes sure to invoke this method with index in 0...size,
so converting negative indices to positive ones is not needed here.
Clients never invoke this method directly. Instead, they access
elements with #[](index) and #[]?(index).
This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.
FIXME Make it compile time error. Should raise if format is not registered or format is incompatible with open_type. Better create an override with the other formats, and leave this as a fallback.