def add_site(doctest_namespace): doctest_namespace["np"] = np doctest_namespace["plt"] = plt doctest_namespace["SpinSystem"] = SpinSystem doctest_namespace["Simulator"] = Simulator doctest_namespace["Site"] = Site doctest_namespace["Coupling"] = Coupling doctest_namespace["SymmetricTensor"] = SymmetricTensor doctest_namespace["st"] = SymmetricTensor doctest_namespace["pprint"] = pprint doctest_namespace["Isotope"] = Isotope doctest_namespace["sp"] = sp doctest_namespace["Method2D"] = Method2D site1 = Site( isotope="13C", isotropic_chemical_shift=20, shielding_symmetric=SymmetricTensor(zeta=10, eta=0.5), ) site2 = Site( isotope="1H", isotropic_chemical_shift=-4, shielding_symmetric=SymmetricTensor(zeta=2.1, eta=0.1), ) site3 = Site( isotope="27Al", isotropic_chemical_shift=120, shielding_symmetric=SymmetricTensor(zeta=2.1, eta=0.1), quadrupolar=SymmetricTensor(Cq=5.1e6, eta=0.5), ) doctest_namespace["spin_system_1H_13C"] = SpinSystem(sites=[site1, site2]) doctest_namespace["spin_systems"] = SpinSystem(sites=[site1, site2, site3]) spin_systems = [SpinSystem(sites=[site]) for site in [site1, site2, site3]] sim = Simulator() sim.spin_systems += spin_systems doctest_namespace["sim"] = sim # Transitions t1 = Transition(initial=[0.5, 0.5], final=[0.5, -0.5]) doctest_namespace["t1"] = t1 t2 = Transition(initial=[0.5, 0.5], final=[-0.5, 0.5]) doctest_namespace["t2"] = t2 path = TransitionPathway([t1, t2]) doctest_namespace["path"] = path
def all_transitions(self) -> TransitionList: """Returns a list of all possible spin transitions in the given spin system. Example ------- >>> spin_system_1H_13C.get_isotopes() # two site (spin-1/2) spin system ['13C', '1H'] >>> spin_system_1H_13C.all_transitions() # 16 two energy level transitions [|-0.5, -0.5⟩⟨-0.5, -0.5|, |-0.5, 0.5⟩⟨-0.5, -0.5|, |0.5, -0.5⟩⟨-0.5, -0.5|, |0.5, 0.5⟩⟨-0.5, -0.5|, |-0.5, -0.5⟩⟨-0.5, 0.5|, |-0.5, 0.5⟩⟨-0.5, 0.5|, |0.5, -0.5⟩⟨-0.5, 0.5|, |0.5, 0.5⟩⟨-0.5, 0.5|, |-0.5, -0.5⟩⟨0.5, -0.5|, |-0.5, 0.5⟩⟨0.5, -0.5|, |0.5, -0.5⟩⟨0.5, -0.5|, |0.5, 0.5⟩⟨0.5, -0.5|, |-0.5, -0.5⟩⟨0.5, 0.5|, |-0.5, 0.5⟩⟨0.5, 0.5|, |0.5, -0.5⟩⟨0.5, 0.5|, |0.5, 0.5⟩⟨0.5, 0.5|] """ transitions = self._all_transitions() return TransitionList([ Transition(initial=item[0].tolist(), final=item[1].tolist()) for item in transitions ])
def get_transition_pathways(self, spin_system) -> List[TransitionPathway]: """Return a list of transition pathways from the given spin system that satisfy the query selection criterion of the method. Args: SpinSystem spin_system: A SpinSystem object. Returns: A list of :ref:`transition_pathway_api` objects. Each TransitionPathway object is an ordered collection of Transition objects. Example: >>> from mrsimulator import SpinSystem >>> from mrsimulator.method.lib import ThreeQ_VAS >>> sys = SpinSystem(sites=[{'isotope': '27Al'}, {'isotope': '29Si'}]) >>> method = ThreeQ_VAS(channels=['27Al']) >>> pprint(method.get_transition_pathways(sys)) [|1.5, -0.5⟩⟨-1.5, -0.5| ⟶ |-0.5, -0.5⟩⟨0.5, -0.5|, weight=(1+0j), |1.5, -0.5⟩⟨-1.5, -0.5| ⟶ |-0.5, 0.5⟩⟨0.5, 0.5|, weight=(1+0j), |1.5, 0.5⟩⟨-1.5, 0.5| ⟶ |-0.5, -0.5⟩⟨0.5, -0.5|, weight=(1+0j), |1.5, 0.5⟩⟨-1.5, 0.5| ⟶ |-0.5, 0.5⟩⟨0.5, 0.5|, weight=(1+0j)] """ segments, weights = self._get_transition_pathway_and_weights_np(spin_system) return [ TransitionPathway( [ Transition(initial=tr[0].tolist(), final=tr[1].tolist()) for tr in item ], weight=w, ) for item, w in zip(segments, weights) if w != 0 ]
def all_transitions(self) -> TransitionList: """Returns a list of all possible spin :ref:`transition_api` objects in the given spin system. Example ------- >>> spin_system_1H_13C.all_transitions() # 16 two energy level transitions [|-0.5, -0.5⟩⟨-0.5, -0.5|, |-0.5, 0.5⟩⟨-0.5, -0.5|, |0.5, -0.5⟩⟨-0.5, -0.5|, |0.5, 0.5⟩⟨-0.5, -0.5|, |-0.5, -0.5⟩⟨-0.5, 0.5|, |-0.5, 0.5⟩⟨-0.5, 0.5|, |0.5, -0.5⟩⟨-0.5, 0.5|, |0.5, 0.5⟩⟨-0.5, 0.5|, |-0.5, -0.5⟩⟨0.5, -0.5|, |-0.5, 0.5⟩⟨0.5, -0.5|, |0.5, -0.5⟩⟨0.5, -0.5|, |0.5, 0.5⟩⟨0.5, -0.5|, |-0.5, -0.5⟩⟨0.5, 0.5|, |-0.5, 0.5⟩⟨0.5, 0.5|, |0.5, -0.5⟩⟨0.5, 0.5|, |0.5, 0.5⟩⟨0.5, 0.5|] Returns A list of :ref:`transition_api` objects. """ transitions = self._all_transitions() return TransitionList( [ Transition(initial=item[0].tolist(), final=item[1].tolist()) for item in transitions ] )
def test_transition_1(): # set up a = {"initial": [0.5, 1.5], "final": [-0.5, 1.5]} tran = Transition(**a) assert tran.initial == [0.5, 1.5] assert tran.final == [-0.5, 1.5] # to dict with unit assert tran.json() == a assert tran.tolist() == [0.5, 1.5, -0.5, 1.5] assert tran.json() == a # p and Δm assert tran.p == -1 assert tran.delta_m == -1
def get_transition_pathways(self, spin_system) -> list: """ Return a list of transition pathways from the given spin system that satisfy the query selection criterion of the method. Args: SpinSystem spin_system: A SpinSystem object. Returns: An array of TransitionPathway objects. Each TransitionPathway object is an ordered collection of Transition objects. """ segments = self._get_transition_pathways_np(spin_system) return [ TransitionPathway([ Transition(initial=tr[0].tolist(), final=tr[1].tolist()) for tr in item ]) for item in segments ]
def test_transition_list_1(): # create a list. tran_list = TransitionList() # append to a list. a = {"initial": [0.5, 1.5], "final": [-0.5, 1.5]} tran_list.append(a) assert tran_list[0] == Transition(**a) b = Transition(**{"initial": [-0.5, -1.5], "final": [-1.5, -1.5]}) tran_list.append(b) assert tran_list[1] == b error = ( "Expecting a Transition object or an equivalent python dict object, instead " "found str." ) with pytest.raises(ValueError, match=f".*{error}.*"): tran_list.append("test") # assign an item at a list index. c = {"initial": [-1.5, -1.5], "final": [1.5, -1.5]} tran_list[1] = c assert tran_list[1] == Transition(**c) with pytest.raises(ValueError, match=f".*{error}.*"): tran_list[1] = "test" # inset an item to a list. tran_list.insert(1, b) assert tran_list[1] == b assert tran_list[2] == Transition(**c) # length of the list. assert len(tran_list) == 3 # delete an item from the list. del tran_list[1] assert len(tran_list) == 2 assert tran_list[0] == Transition(**a) assert tran_list[1] == Transition(**c) # equality test assert tran_list != "a" assert tran_list != TransitionList([a]) assert tran_list != TransitionList([a, b]) assert tran_list == TransitionList([a, c]) # appending b for further filter tests. tran_list.insert(1, b) assert tran_list == TransitionList([a, b, c]) # filter tran_filter_1 = tran_list.filter() assert tran_filter_1 == tran_list # test for P tran_filter_1 = tran_list.filter(P=[-1, 0]) assert tran_filter_1 == TransitionList([a, b]) tran_filter_1 = tran_list.filter(P=[-1, -1]) assert tran_filter_1 == TransitionList([]) # test for D tran_filter_1 = tran_list.filter(D=[0, 0]) assert tran_filter_1 == TransitionList([a, c]) # test for P and D tran_filter_1 = tran_list.filter(P=[3, 0], D=[0, 0]) assert tran_filter_1 == TransitionList([c])
def add_site(doctest_namespace): doctest_namespace["np"] = np doctest_namespace["plt"] = plt doctest_namespace["SpinSystem"] = SpinSystem doctest_namespace["Simulator"] = Simulator doctest_namespace["Site"] = Site doctest_namespace["Coupling"] = Coupling doctest_namespace["SymmetricTensor"] = SymmetricTensor doctest_namespace["st"] = SymmetricTensor doctest_namespace["pprint"] = pprint doctest_namespace["Isotope"] = Isotope doctest_namespace["sp"] = sp doctest_namespace["Method2D"] = Method2D site1 = Site( isotope="13C", isotropic_chemical_shift=20, shielding_symmetric=SymmetricTensor(zeta=10, eta=0.5), ) doctest_namespace["site1"] = site1 coupling1 = Coupling( site_index=[0, 1], isotropic_j=20, j_symmetric=SymmetricTensor(zeta=10, eta=0.5), ) doctest_namespace["coupling1"] = coupling1 site2 = Site( isotope="1H", isotropic_chemical_shift=-4, shielding_symmetric=SymmetricTensor(zeta=2.1, eta=0.1), ) doctest_namespace["site2"] = site2 site3 = Site( isotope="27Al", isotropic_chemical_shift=120, shielding_symmetric=SymmetricTensor(zeta=2.1, eta=0.1), quadrupole=SymmetricTensor(Cq=5.1e6, eta=0.5), ) doctest_namespace["site3"] = site3 spin_system_1H_13C = SpinSystem(sites=[site1, site2]) doctest_namespace["spin_system_1H_13C"] = spin_system_1H_13C spin_system_1 = SpinSystem(sites=[site1]) doctest_namespace["spin_system_1"] = spin_system_1 doctest_namespace["spin_systems"] = SpinSystem(sites=[site1, site2, site3]) spin_systems = [SpinSystem(sites=[site]) for site in [site1, site2, site3]] sim = Simulator() sim.spin_systems += spin_systems doctest_namespace["sim"] = sim # coesite O17_1 = Site( isotope="17O", isotropic_chemical_shift=29, quadrupolar=SymmetricTensor(Cq=6.05e6, eta=0.000), ) O17_2 = Site( isotope="17O", isotropic_chemical_shift=41, quadrupolar=SymmetricTensor(Cq=5.43e6, eta=0.166), ) O17_3 = Site( isotope="17O", isotropic_chemical_shift=57, quadrupolar=SymmetricTensor(Cq=5.45e6, eta=0.168), ) O17_4 = Site( isotope="17O", isotropic_chemical_shift=53, quadrupolar=SymmetricTensor(Cq=5.52e6, eta=0.169), ) O17_5 = Site( isotope="17O", isotropic_chemical_shift=58, quadrupolar=SymmetricTensor(Cq=5.16e6, eta=0.292), ) sites = [O17_1, O17_2, O17_3, O17_4, O17_5] abundance = [0.83, 1.05, 2.16, 2.05, 1.90] # abundance of each spin system spin_systems = [ SpinSystem(sites=[s], abundance=a) for s, a in zip(sites, abundance) ] method = BlochDecayCTSpectrum( channels=["17O"], rotor_frequency=14000, spectral_dimensions=[{ "count": 2048, "spectral_width": 50000 }], ) sim_coesite = Simulator() sim_coesite.spin_systems += spin_systems sim_coesite.methods += [method] doctest_namespace["sim_coesite"] = sim_coesite # Transitions t1 = Transition(initial=[0.5, 0.5], final=[0.5, -0.5]) doctest_namespace["t1"] = t1 t2 = Transition(initial=[0.5, 0.5], final=[-0.5, 0.5]) doctest_namespace["t2"] = t2 path = TransitionPathway([t1, t2]) doctest_namespace["path"] = path