Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
miniff
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Quantum Tinkerer
miniff
Commits
5b9b1a73
Commit
5b9b1a73
authored
3 years ago
by
Artem Pulkin
Browse files
Options
Downloads
Patches
Plain Diff
kernel: move packing/unpacking into class methods
parent
d22f394a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Pipeline
#79998
passed
3 years ago
Stage: configure
Stage: test
Stage: docs
Stage: package
Stage: deploy
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
miniff/kernel.py
+51
-72
51 additions, 72 deletions
miniff/kernel.py
with
51 additions
and
72 deletions
miniff/kernel.py
+
51
−
72
View file @
5b9b1a73
...
...
@@ -9,7 +9,8 @@ from scipy.sparse import coo_matrix, csr_matrix
from
scipy.spatial
import
cKDTree
from
itertools
import
product
from
collections
import
namedtuple
,
OrderedDict
from
collections
import
OrderedDict
from
typing
import
NamedTuple
try
:
from
functools
import
cached_property
except
ImportError
:
...
...
@@ -712,78 +713,56 @@ class SnapshotHistory(list):
self
.
append
(
cell
)
sf_parameters
=
namedtuple
(
"
sf_parameters
"
,
(
"
cartesian
"
,
"
vectors
"
))
class
sf_parameters
(
NamedTuple
):
cartesian
:
np
.
ndarray
vectors
:
np
.
ndarray
def
pack_coordinates
(
c
=
None
,
v
=
None
,
vc
=
Non
e
,
v
v
=
Non
e
):
"""
A convention to pack coordinates
.
def
copy
(
self
,
c
=
Tru
e
,
v
=
Tru
e
):
"""
Makes a copy
.
Parameters
----------
c : np.ndarray
Atomic coordinates array.
v : np.ndarray
Cell vectors array.
vc : np.ndarray
Atomic velocities array.
vv : np.ndarray
(Fake) Cell vector velocities.
Parameters
----------
c : bool
Copy coordinates, if available.
v : bool
Copy vectors, if available.
Returns
-------
result : np.ndarray
A 2D array with packed coordinates.
"""
if
vc
is
not
None
:
assert
c
is
not
None
if
vv
is
not
None
:
assert
v
is
not
None
args
=
(
c
,
v
,
vc
,
vv
)
assert
sum
(
i
is
not
None
for
i
in
args
)
!=
3
args
=
tuple
(
i
for
i
in
args
if
i
is
not
None
)
return
np
.
concatenate
(
args
,
axis
=
0
)
Returns
-------
result : sf_parameters
The resulting parameters.
"""
return
sf_parameters
(
self
.
cartesian
if
c
else
None
,
self
.
vectors
if
v
else
None
)
def
pack
(
self
):
return
np
.
concatenate
(
tuple
(
i
for
i
in
self
if
i
is
not
None
))
def
unpack_coordinates
(
params
,
c
=
False
,
v
=
False
,
vc
=
False
,
vv
=
False
,
nv
=
3
):
"""
A convention to unpack coordinates.
@staticmethod
def
unpack
(
data
,
c
=
False
,
v
=
False
,
nv
=
3
):
"""
Unpacks the data into parameters.
Parameters
----------
params : np.ndarray
The packed coordinates.
c : bool
Indicates the presence of atomic coordinates.
v : bool
Indicates the presence of vectors.
vc : bool
Indicates the presence of atomic velocities.
vv : bool
Indicates the presence of (fake) cell vector velocities.
nv : int
Number of vectors.
Parameters
----------
data : np.ndarray
The packed parameters.
c : bool
Indicates the presence of atomic coordinates.
v : bool
Indicates the presence of vectors.
nv : int
Number of vectors.
Returns
-------
result : tuple
Coordinates and velocities as separate arrays.
"""
assert
(
c
,
vc
)
!=
(
False
,
True
)
assert
(
v
,
vv
)
!=
(
False
,
True
)
args
=
(
c
,
v
,
vc
,
vv
)
assert
sum
(
args
)
!=
3
if
vc
or
vv
:
params_c
,
params_v
=
params
[:
len
(
params
)
//
2
],
params
[
len
(
params
)
//
2
:]
else
:
params_c
=
params
params_v
=
None
return
(
params_c
[:
-
nv
]
if
c
and
v
else
params_c
if
c
else
None
,
params_c
[
-
nv
:]
if
v
else
None
,
params_v
[:
-
nv
]
if
vc
and
vv
else
params_v
if
vc
else
None
,
params_v
[
-
nv
:]
if
vv
else
None
,
)
Returns
-------
result : sf_parameters
The resulting parameters.
"""
return
sf_parameters
(
data
[:
-
nv
]
if
c
and
v
else
data
if
c
else
None
,
data
[
-
nv
:]
if
v
else
None
,
)
class
ScalarFunctionWrapper
:
...
...
@@ -834,18 +813,18 @@ class ScalarFunctionWrapper:
def
p2c
(
self
,
parameters
:
np
.
ndarray
)
->
sf_parameters
:
"""
Unpacks parameters.
"""
return
sf_parameters
(
*
unpack
_coordinates
(
return
sf_parameters
.
unpack
(
parameters
.
reshape
(
-
1
,
3
),
c
=
self
.
include
[
"
cartesian
"
],
v
=
self
.
include
[
"
vectors
"
],
)
[:
2
])
)
def
c2p
(
self
,
coordinates
:
sf_parameters
)
->
np
.
ndarray
:
"""
Packs parameters.
"""
return
pack_
coordinates
(
c
=
coordinates
.
cartesian
if
self
.
include
[
"
cartesian
"
]
else
None
,
v
=
coordinates
.
vectors
if
self
.
include
[
"
vectors
"
]
else
None
,
).
reshape
(
-
1
)
return
coordinates
.
copy
(
c
=
self
.
include
[
"
cartesian
"
],
v
=
self
.
include
[
"
vectors
"
],
).
pack
(
).
reshape
(
-
1
)
def
start_recording
(
self
):
"""
Starts recording of coordinates passed.
"""
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment