Exemplo n.º 1
0
def test_nspinspec():
    v, J = spin8()
    # # print(v)
    # # print(J)
    h = hamiltonian_sparse(v, J)
    # # print(h)
    nspins = len(v)
    return simsignals(h, nspins)
    # spectrum = normalize_spectrum(spectrum, nspins)
    # return hamiltonian_sparse(v, J)
    assert 1 == 1
Exemplo n.º 2
0
def test_cleanup():
    # make a mess
    v, J = spin8()
    hamiltonian(v, J)
    # then clean it up
    files_cleaned = cleanup()
    print('FILES CLEANED:')
    print(files_cleaned)
    path = os.getcwd()
    spin8_operators = ['Lproduct8.npy', 'Lz8.npy']
    operators = [os.path.join(path, operator) for operator in spin8_operators]
    assert files_cleaned == operators
    for operator in operators:
        assert not os.path.isfile(operator), 'File left behind! ' + operator
Exemplo n.º 3
0
def test_smoke():
    nmrplot(nspinspec(hamiltonian, *spin8()))
    nmrplot(nspinspec(hamiltonian_sparse, *spin8()))
Exemplo n.º 4
0
"""Testing the performance for the entire spectrum calculation."""
import numpy as np
from scipy.sparse import csc_matrix, csr_matrix, lil_matrix
import sparse

from nmrtools.nmrmath import is_allowed, normalize_spectrum, transition_matrix
from nmrtools.nmrplot import nmrplot
from speedtest.compare_hamiltonians import hamiltonian, hamiltonian_sparse
from speedtest.speedutils import timefn

from simulation_data import spin8, spin3, spin11

H8 = hamiltonian(*spin8())
H8_SPARSE = hamiltonian_sparse(*spin8())
print('Import type: ', type(H8_SPARSE))

# Below are older functions that were written before completion of
# test_simsignals.py


def nspinspec(h_func, freqs, couplings, normalize=True):
    """A version of nmrtools.nmrmath.nspinspec that accepts different H functions."""
    """
    Calculates second-order spectral data (freqency and intensity of signals)
    for *n* spin-half nuclei.

    Parameters
    ---------
    freqs : [float...]
        a list of *n* nuclei frequencies in Hz
    couplings : array-like
Exemplo n.º 5
0
    Lx, Ly, Lz = kuprov_so(nspins)
    H = np.zeros((2**nspins, 2**nspins), dtype=np.complex128)
    for n in range(nspins):
        H += v[n] * Lz[n]

    for n in range(nspins):
        for k in range(nspins):
            if n != k:
                H += 0.5 * J[n, k] * (Lx[n] @ Lx[k] + Ly[n] @ Ly[k] +
                                      Lz[n] @ Lz[k])
    return H


if __name__ == '__main__':
    from simulation_data import spin8
    from tests.prepare import standard_8
    v, J = spin8()
    hamiltonians = [
        f(v, J)
        for f in [kuprov_H_csr, kuprov_H_lil, kuprov_H_dense, kuprov_cached]
    ]
    for i, h in enumerate(hamiltonians):
        try:
            assert np.array_equal(h, standard_8), 'fail at ' + str(i)
            print('Passed: ', str(i), str(i + 1))
        except AssertionError:
            print('failure at ', str(i))
            print(hamiltonians[i])
    print(standard_8)
    print(hamiltonians[-1])
Exemplo n.º 6
0
import numpy as np
import scipy
from scipy.sparse import csc_matrix, csr_matrix, lil_matrix
import sparse

from nmrtools.nmrmath import is_allowed, normalize_spectrum, transition_matrix
from nmrtools.nmrplot import nmrplot
from speedtest.compare_hamiltonians import hamiltonian, hamiltonian_sparse
from speedtest.speedutils import timefn
from tests.test_spectraspeed import simsignals

from simulation_data import spin8, spin3, spin11, rioux

H3_MATRIX = hamiltonian(*spin3())
H3_NDARRAY = hamiltonian_sparse(*spin3())
H8_MATRIX = hamiltonian(*spin8())
H8_NDARRAY = hamiltonian_sparse(*spin8())
H11_MATRIX = hamiltonian(*spin11())
H11_NDARRAY = hamiltonian_sparse(*spin11())
H_RIOUX = hamiltonian(*rioux())
H_RIOUX_SPARSE = hamiltonian_sparse(*rioux())


@timefn
def numpy_eigh(h):
    return np.linalg.eigh(h)


@timefn
def scipy_eigh(h):
    return scipy.linalg.eigh(h)
Exemplo n.º 7
0
    Lcol = np.vstack((Lx, Ly, Lz)).real
    Lrow = Lcol.T  # As opposed to sparse version of code, this works!
    Lproduct = np.dot(Lrow, Lcol)

    # Hamiltonian operator
    H = np.zeros((2**nspins, 2**nspins))

    # Add Zeeman interactions:
    for n in range(nspins):
        H = H + freqlist[n] * Lz[0, n]

    # Scalar couplings

    # Testing with MATLAB discovered J must be /2.
    # Believe it is related to the fact that in the SpinDynamics.org simulation
    # freqs are *2pi, but Js by pi only.
    scalars = 0.5 * couplings
    scalars = np.multiply(scalars, Lproduct)
    for n in range(nspins):
        for k in range(nspins):
            H += scalars[n, k].real

    return H


standard_3 = hamiltonian_original(*spin3())
standard_8 = hamiltonian_original(*spin8())
standard_11 = hamiltonian(*spin11())
# standard_fox = hamiltonian(*fox())