예제 #1
0
 def test_fpl_argument_to_reaction(self):
     fpl = espressopp.FixedPairListLambda(self.system.storage)
     espressopp.integrator.Reaction(0,
                                    0,
                                    0,
                                    0,
                                    0,
                                    0,
                                    0,
                                    0,
                                    rate=0.1,
                                    fpl=fpl,
                                    cutoff=0.0)
     fpl = espressopp.FixedPairList(self.system.storage)
     espressopp.integrator.Reaction(0,
                                    0,
                                    0,
                                    0,
                                    0,
                                    0,
                                    0,
                                    0,
                                    rate=0.1,
                                    fpl=fpl,
                                    cutoff=0.0)
예제 #2
0
 def test_harmonic_energy(self):
     fpl = espressopp.FixedPairListLambda(self.system.storage)
     fpl.addBonds([(1, 2)])
     harmonic = espressopp.interaction.Harmonic(K=30, r0=0.97)
     interaction = espressopp.interaction.FixedPairListLambdaHarmonic(
         self.system, fpl, harmonic)
     self.assertAlmostEqual(interaction.computeEnergy(), 30.0)
예제 #3
0
 def test_harmonic_energy_lambda(self):
     fpl = espressopp.FixedPairListLambda(self.system.storage)
     fpl.addBonds([(1, 2), (2, 3)])
     harmonic = espressopp.interaction.Harmonic(K=30, r0=0.97)
     interaction = espressopp.interaction.FixedPairListLambdaHarmonic(
         self.system, fpl, harmonic)
     self.assertAlmostEqual(interaction.computeEnergy(), 60.0)
     fpl.setAllLambda(0.5)
     self.assertAlmostEqual(interaction.computeEnergy(), 30.0)
     fpl.setLambda(2, 3, 0.0)  # Set lambda 0.0 to only bond 2-3
     self.assertAlmostEqual(interaction.computeEnergy(), 15.0)
 def test_harmonic_energy(self):
     fpl = espressopp.FixedPairListLambda(self.system.storage)
     fpl.addBonds([(1, 2)])
     harmonic = espressopp.interaction.Harmonic(K=30, r0=0.97)
     interaction = espressopp.interaction.FixedPairListLambdaHarmonic(
         self.system, fpl, harmonic)
     self.assertAlmostEqual(interaction.computeEnergy(), 30.0)
     self.fixed_dynamic_resolution.register_pair_list(fpl, -0.5)
     self.integrator.run(1)
     self.assertAlmostEqual(interaction.computeEnergy(), 15.0)
     self.fixed_dynamic_resolution.update_pair_list(
         0, 0.5)  # change rate of fpl, index 0
     self.integrator.run(1)
     self.assertAlmostEqual(interaction.computeEnergy(), 30.0)
예제 #5
0
    def setup_reactions(self):
        """Setup reactions.

        Returns:
            The espressopp.integrator.ChemicalReaction extension and the list
            of fixed pair lists with new bonds.

        """
        self.ar_interval = int(self.cfg['general']['interval'])
        ar = espressopp.integrator.ChemicalReaction(
            self.system,
            self.vl,
            self.system.storage,
            self.tm,
            self.ar_interval)
        ar.nearest_mode = self.cfg['general']['nearest']
        if self.cfg['general']['pair_distances_filename']:
            ar.pair_distances_filename = self.cfg['general']['pair_distances_filename']
        if self.cfg['general']['max_per_interval'] > 0:
            ar.max_per_interval = self.cfg['general']['max_per_interval']

        fpls = []
        reactions = []
        extensions_to_integrator = []

        fpl_def = collections.namedtuple('FPLDef', ['fpl', 'type_list'])

        self.reaction_index = {}
        reaction_idx = 0

        for group_name, reaction_group in self.cfg['reactions'].items():
            print('Setting reaction group {}'.format(group_name))

            type2fpl = {}

            # Setting the interaction for the pairs created by this reaction group.
            if self.args.t_hybrid_bond > 0:
                fpl = espressopp.FixedPairListLambda(self.system.storage, 0.0)
                interaction_class = eval('espressopp.interaction.FixedPairListLambda{}'.format(
                    reaction_group['potential']))
            else:
                fpl = espressopp.FixedPairList(self.system.storage)
                interaction_class = eval('espressopp.interaction.FixedPairList{}'.format(
                    reaction_group['potential']))

            pot_class = eval('espressopp.interaction.{}'.format(reaction_group['potential']))
            # Convert if it's possible, values for float
            pot_options = {}
            for k, v in reaction_group['potential_options'].items():
                try:
                    pot_options[k] = float(v)
                except ValueError:
                    pot_options[k] = v
            print('Setting potential for bond with class {}, options {}'.format(
                reaction_group['potential'], reaction_group['potential_options']))
            potential = pot_class(**pot_options)

            interaction = interaction_class(self.system, fpl, potential)
            fpl.interaction = interaction
            self.system.addInteraction(interaction, 'chem_fpl_{}'.format(group_name))

            # Setting the post process extensions.
            group_extensions = self._prepare_group_postprocess(reaction_group['extensions'])
            extensions_to_integrator = []
            extensions_to_reactions = collections.defaultdict(list)
            for k, v in group_extensions.items():
                for x in v:
                    if x.ext is not None:
                        if x.ext_type == EXT_INTEGRATOR:
                            extensions_to_integrator.append(x.ext)
                        elif x.ext_type == EXT_POSTPROCESS:
                            extensions_to_reactions[k].append(x)
                        else:
                            raise RuntimeError('Wrong ext_type={}'.format(x.ext_type))

            # Process the reactions.
            reaction_type_list = []
            print('Setting chemical reactions in group')
            for chem_reaction in reaction_group['reaction_list']:
                # Pass connectivity map from group level to reaction level
                chem_reaction['connectivity_map'] = reaction_group['connectivity_map']
                # Dissociation reaction will be done in second run.
                if chem_reaction['reaction_type'] == REACTION_DISSOCATION:
                    continue
                r, reaction_types = self._setup_reaction(chem_reaction, fpl)
                if r is not None:
                    reaction_type_list.extend(reaction_types)
                    for ext_name, extensions in extensions_to_reactions.items():
                        if ext_name in chem_reaction['exclude_extensions']:
                            print('Skip extension: {} ({})'.format(ext_name, chem_reaction['equation']))
                        else:
                            for extension in extensions:
                                print('Add extension {} to {} ({}): {}'.format(
                                    ext_name, chem_reaction['equation'], extension.pp_type, extension.ext))
                                if extension.pp_type:
                                    r.add_postprocess(extension.ext, extension.pp_type)
                                else:
                                    r.add_postprocess(extension.ext)
                    ar.add_reaction(r)
                    self.reaction_index[reaction_idx] = chem_reaction['equation']
                    reactions.append(r)
                    reaction_idx += 1
                    for t1, t2 in reaction_types:
                        type2fpl[(t1, t2)] = fpl
                        type2fpl[(t2, t1)] = fpl

            # Now process dissociation reactions.
            # Pitfall, It can only sees fixed pair lists in given group
            for chem_reaction in reaction_group['reaction_list']:
                if chem_reaction['reaction_type'] != REACTION_DISSOCATION:  # only dissociation
                    continue
                r, reaction_types = self._setup_reaction_dissocation(chem_reaction, type2fpl)
                if r is not None:
                    #reaction_type_list.extend(reaction_types)
                    self.separate_fpls.add(tuple(reaction_types[0]))
                    for ext_name, extensions in extensions_to_reactions.items():
                        if ext_name in chem_reaction['exclude_extensions']:
                            print('Skip extension: {} ({})'.format(ext_name, chem_reaction['equation']))
                        else:
                            for extension in extensions:
                                print('Add extension {} to {} ({}): {}'.format(
                                    ext_name, chem_reaction['equation'], extension.pp_type, extension.ext))
                                if extension.pp_type:
                                    r.add_postprocess(extension.ext, extension.pp_type)
                                else:
                                    r.add_postprocess(extension.ext)
                    ar.add_reaction(r)
                    self.reaction_index[reaction_idx] = chem_reaction['equation']
                    reactions.append(r)
                    reaction_idx += 1

            fpls.append(fpl_def(fpl, set(reaction_type_list)))

        return ar, fpls, reactions, extensions_to_integrator
예제 #6
0
 def test_set_lambda_single(self):
     fpl = espressopp.FixedPairListLambda(self.system.storage)
     fpl.addBonds([(1, 2), (2, 3)])
     self.assertEqual(fpl.getLambda(1, 2), [1.0])
     fpl.setLambda(1, 2, 0.0)
     self.assertEqual(fpl.getLambda(1, 2), [0.0])