Skip to content

Instantly share code, notes, and snippets.

@daknuett
Created June 5, 2024 10:46
Show Gist options
  • Save daknuett/3393ef1d3d2ebb413271f8e8d41c4577 to your computer and use it in GitHub Desktop.
Save daknuett/3393ef1d3d2ebb413271f8e8d41c4577 to your computer and use it in GitHub Desktop.
Convert GPT's lattices to numpy ndarrays
#
# Copyright(c) 2024 Daniel Knüttel
#
# 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, either version 3 of the License, or
# (at your option) any later version.
#
# 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/>.
import gpt as g
import numpy as np
def lattice2ndarray(lattice):
"""
Converts a gpt (https://github.com/lehner/gpt) lattice to a numpy ndarray
keeping the ordering of axes as one would expect.
Example::
q_top = g.qcd.gauge.topological_charge_5LI(U_smeared, field=True)
plot_scalar_field(lattice2ndarray(q_top))
"""
shape = lattice.grid.fdimensions
shape = list(reversed(shape))
if lattice[:].shape[1:] != (1,):
shape.extend(lattice[:].shape[1:])
result = lattice[:].reshape(shape)
result = np.swapaxes(result, 0, 3)
result = np.swapaxes(result, 1, 2)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment