示例#1
0
    def test_descriptor_two_sides(self):
        jfile = 'water_se_a_type.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 2
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have(jdata['model']['descriptor'], 'rcut')
        sel = j_must_have(jdata['model']['descriptor'], 'sel')
        ntypes = len(sel)

        data = DataSystem(systems,
                          set_pfx,
                          batch_size,
                          test_size,
                          rcut,
                          run_opt=None)

        test_data = data.get_test()
        numb_test = 1

        # set parameters
        jdata['model']['descriptor']['neuron'] = [5, 5, 5]
        jdata['model']['descriptor']['axis_neuron'] = 2
        typeebd_param = {
            'neuron': [5, 5, 5],
            'resnet_dt': False,
            'seed': 1,
        }

        # init models
        typeebd = TypeEmbedNet(neuron=typeebd_param['neuron'],
                               resnet_dt=typeebd_param['resnet_dt'],
                               seed=typeebd_param['seed'],
                               uniform_seed=True)

        jdata['model']['descriptor'].pop('type', None)
        descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed=True)

        # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']])
        input_data = {
            'coord': [test_data['coord']],
            'box': [test_data['box']],
            'type': [test_data['type']],
            'natoms_vec': [test_data['natoms_vec']],
            'default_mesh': [test_data['default_mesh']]
        }
        descrpt.bias_atom_e = data.compute_energy_shift()

        t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c')
        t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None],
                                  name='t_energy')
        t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='t_force')
        t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                  name='t_virial')
        t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                     name='t_atom_ener')
        t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='i_coord')
        t_type = tf.placeholder(tf.int32, [None], name='i_type')
        t_natoms = tf.placeholder(tf.int32, [ntypes + 2], name='i_natoms')
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9],
                               name='i_box')
        t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh')
        is_training = tf.placeholder(tf.bool)
        t_fparam = None

        type_embedding = typeebd.build(ntypes,
                                       suffix="_se_a_type_des_ebd_2sdies")

        dout \
            = descrpt.build(
                t_coord,
                t_type,
                t_natoms,
                t_box,
                t_mesh,
                {'type_embedding' : type_embedding},
                reuse = False,
                suffix = "_se_a_type_des_2sides"
            )

        feed_dict_test = {
            t_prop_c: test_data['prop_c'],
            t_energy: test_data['energy'][:numb_test],
            t_force: np.reshape(test_data['force'][:numb_test, :], [-1]),
            t_virial: np.reshape(test_data['virial'][:numb_test, :], [-1]),
            t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :],
                                    [-1]),
            t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]),
            t_box: test_data['box'][:numb_test, :],
            t_type: np.reshape(test_data['type'][:numb_test, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            is_training: False
        }

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [model_dout] = sess.run([dout], feed_dict=feed_dict_test)
        model_dout = model_dout.reshape([-1])

        ref_dout = [
            0.0005722682145569174, -0.00020202686217742682,
            -0.00020202686217742682, 7.13250554992363e-05,
            -0.0014770058171250015, 0.000521468690207748,
            -0.001143865186937176, 0.0004038453384193948,
            0.0005617335409639567, -0.00019831394075147532,
            0.00048086740718842236, -0.0001693584775806112,
            -0.0001693584775806112, 5.966987137476082e-05,
            -0.0012342029581315136, 0.00043492340851472783,
            -0.0009566016612537016, 0.00033706767041080107,
            0.00047065988464132244, -0.0001657950398095401,
            0.0003647849239740657, -0.00013744939018250384,
            -0.00013744939018250384, 5.1825826955234744e-05,
            -0.00096004206555711, 0.00036185565262332876,
            -0.0007267433909643961, 0.0002738914365542745,
            0.00038019365906978136, -0.00014322754331896057,
            0.0004675256930823109, -0.00017634410399626168,
            -0.00017634410399626168, 6.652672908755666e-05,
            -0.0012328062885292486, 0.00046500213384094614,
            -0.0009328887521346069, 0.0003518668613172834,
            0.0004877847509912577, -0.00018396318824508986,
            0.0005154794374703516, -0.00019422534512034776,
            -0.00019422534512034776, 7.318151797939947e-05,
            -0.0013576642997136488, 0.0005115548790018505,
            -0.0010275333676074971, 0.00038716440070070385,
            0.0005376426714609369, -0.00020257810468163985,
            0.0004482204892297628, -0.00016887749501640607,
            -0.00016887749501640607, 6.364643102775375e-05,
            -0.001181345877677835, 0.0004452029242063362,
            -0.0008941636427724908, 0.0003369586197174627,
            0.0004677878512312651, -0.00017625260641095753
        ]

        places = 10
        np.testing.assert_almost_equal(model_dout, ref_dout, places)
示例#2
0
    def test_model(self):
        jfile = 'water_se_a_type.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have (jdata['model']['descriptor'], 'rcut')
        
        data = DataSystem(systems, set_pfx, batch_size, test_size, rcut, run_opt = None)
        
        test_data = data.get_test ()
        numb_test = 1

        jdata['model']['descriptor'].pop('type', None)        
        descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed = True)
        jdata['model']['fitting_net']['descrpt'] = descrpt
        fitting = EnerFitting(**jdata['model']['fitting_net'], uniform_seed = True)
        typeebd_param = jdata['model']['type_embedding']
        typeebd = TypeEmbedNet(
            neuron = typeebd_param['neuron'],
            resnet_dt = typeebd_param['resnet_dt'],
            seed = typeebd_param['seed'], 
            uniform_seed = True)
        model = EnerModel(descrpt, fitting, typeebd)

        # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']])
        input_data = {'coord' : [test_data['coord']], 
                      'box': [test_data['box']], 
                      'type': [test_data['type']],
                      'natoms_vec' : [test_data['natoms_vec']],
                      'default_mesh' : [test_data['default_mesh']]
        }
        model._compute_input_stat(input_data)
        model.descrpt.bias_atom_e = data.compute_energy_shift()

        t_prop_c           = tf.placeholder(tf.float32, [5],    name='t_prop_c')
        t_energy           = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None], name='t_energy')
        t_force            = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_force')
        t_virial           = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_virial')
        t_atom_ener        = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_atom_ener')
        t_coord            = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='i_coord')
        t_type             = tf.placeholder(tf.int32,   [None], name='i_type')
        t_natoms           = tf.placeholder(tf.int32,   [model.ntypes+2], name='i_natoms')
        t_box              = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name='i_box')
        t_mesh             = tf.placeholder(tf.int32,   [None], name='i_mesh')
        is_training        = tf.placeholder(tf.bool)
        t_fparam = None
        inputs_dict = {}

        model_pred \
            = model.build (t_coord, 
                           t_type, 
                           t_natoms, 
                           t_box, 
                           t_mesh,
                           inputs_dict,
                           suffix = "se_a_type", 
                           reuse = False)
        energy = model_pred['energy']
        force  = model_pred['force']
        virial = model_pred['virial']
        atom_ener =  model_pred['atom_ener']

        feed_dict_test = {t_prop_c:        test_data['prop_c'],
                          t_energy:        test_data['energy']              [:numb_test],
                          t_force:         np.reshape(test_data['force']    [:numb_test, :], [-1]),
                          t_virial:        np.reshape(test_data['virial']   [:numb_test, :], [-1]),
                          t_atom_ener:     np.reshape(test_data['atom_ener'][:numb_test, :], [-1]),
                          t_coord:         np.reshape(test_data['coord']    [:numb_test, :], [-1]),
                          t_box:           test_data['box']                 [:numb_test, :],
                          t_type:          np.reshape(test_data['type']     [:numb_test, :], [-1]),
                          t_natoms:        test_data['natoms_vec'],
                          t_mesh:          test_data['default_mesh'],
                          is_training:     False}

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [e, f, v] = sess.run([energy, force, virial], 
                             feed_dict = feed_dict_test)
        # print(sess.run(model.type_embedding))
        # np.savetxt('tmp.out', sess.run(descrpt.dout, feed_dict = feed_dict_test), fmt='%.10e')
        # # print(sess.run(model.atype_embed, feed_dict = feed_dict_test))
        # print(sess.run(fitting.inputs, feed_dict = feed_dict_test))
        # print(sess.run(fitting.outs, feed_dict = feed_dict_test))
        # print(sess.run(fitting.atype_embed, feed_dict = feed_dict_test))

        e = e.reshape([-1])
        f = f.reshape([-1])
        v = v.reshape([-1])
        np.savetxt('e.out', e.reshape([1, -1]), delimiter=',')
        np.savetxt('f.out', f.reshape([1, -1]), delimiter=',')
        np.savetxt('v.out', v.reshape([1, -1]), delimiter=',')

        refe = [6.049065170680415804e+01]
        reff = [1.021832439441947293e-01,1.122650466359011306e-01,3.927874278714531091e-03,1.407089812207832635e-01,1.312473824343091400e-01,-1.228371057389851181e-02,-1.109672154547165501e-01,6.582735820731049070e-02,1.251568633647655391e-03,7.933758749748777428e-02,-1.831777072317984367e-01,-6.173090134630876760e-03,-2.703597126460742794e-01,4.817856571062521104e-02,1.491963457594796399e-02,5.909711543832503466e-02,-1.743406457563475287e-01,-1.642276779780762769e-03]
        refv = [-6.932736357193732823e-01,1.453756052949563837e-01,2.138263139115256783e-02,1.453756052949564392e-01,-3.880901656480436612e-01,-7.782259726407755700e-03,2.138263139115256437e-02,-7.782259726407749628e-03,-1.225285973678705374e-03]

        refe = np.reshape(refe, [-1])
        reff = np.reshape(reff, [-1])
        refv = np.reshape(refv, [-1])

        places = 10
        np.testing.assert_almost_equal(e, refe, places)
        np.testing.assert_almost_equal(f, reff, places)
        np.testing.assert_almost_equal(v, refv, places)
示例#3
0
    def test_descriptor_one_side(self):
        jfile = 'water_se_a_type.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have(jdata['model']['descriptor'], 'rcut')
        sel = j_must_have(jdata['model']['descriptor'], 'sel')
        ntypes = len(sel)

        data = DataSystem(systems,
                          set_pfx,
                          batch_size,
                          test_size,
                          rcut,
                          run_opt=None)

        test_data = data.get_test()
        numb_test = 1

        # set parameters
        jdata['model']['descriptor']['neuron'] = [5, 5, 5]
        jdata['model']['descriptor']['axis_neuron'] = 2
        jdata['model']['descriptor']['type_one_side'] = True
        typeebd_param = {
            'neuron': [5, 5, 5],
            'resnet_dt': False,
            'seed': 1,
        }

        # init models
        typeebd = TypeEmbedNet(neuron=typeebd_param['neuron'],
                               resnet_dt=typeebd_param['resnet_dt'],
                               seed=typeebd_param['seed'],
                               uniform_seed=True)

        jdata['model']['descriptor'].pop('type', None)
        descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed=True)

        # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']])
        input_data = {
            'coord': [test_data['coord']],
            'box': [test_data['box']],
            'type': [test_data['type']],
            'natoms_vec': [test_data['natoms_vec']],
            'default_mesh': [test_data['default_mesh']]
        }
        descrpt.bias_atom_e = data.compute_energy_shift()

        t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c')
        t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None],
                                  name='t_energy')
        t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='t_force')
        t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                  name='t_virial')
        t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                     name='t_atom_ener')
        t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='i_coord')
        t_type = tf.placeholder(tf.int32, [None], name='i_type')
        t_natoms = tf.placeholder(tf.int32, [ntypes + 2], name='i_natoms')
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9],
                               name='i_box')
        t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh')
        is_training = tf.placeholder(tf.bool)
        t_fparam = None

        type_embedding = typeebd.build(ntypes,
                                       suffix="_se_a_type_des_ebd_1side")

        dout \
            = descrpt.build(
                t_coord,
                t_type,
                t_natoms,
                t_box,
                t_mesh,
                {'type_embedding' : type_embedding},
                reuse = False,
                suffix = "_se_a_type_des_1side"
            )

        feed_dict_test = {
            t_prop_c: test_data['prop_c'],
            t_energy: test_data['energy'][:numb_test],
            t_force: np.reshape(test_data['force'][:numb_test, :], [-1]),
            t_virial: np.reshape(test_data['virial'][:numb_test, :], [-1]),
            t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :],
                                    [-1]),
            t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]),
            t_box: test_data['box'][:numb_test, :],
            t_type: np.reshape(test_data['type'][:numb_test, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            is_training: False
        }

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [model_dout] = sess.run([dout], feed_dict=feed_dict_test)
        model_dout = model_dout.reshape([-1])

        ref_dout = [
            0.0009704469114440277, 0.0007136310372560243,
            0.0007136310372560243, 0.000524968274824758,
            -0.0019790100690810016, -0.0014556100390424947,
            -0.001318691223889266, -0.0009698525512440269,
            0.001937780602605409, 0.0014251755182315322, 0.0008158935519461114,
            0.0005943870925895051, 0.0005943870925895051,
            0.0004340263490412088, -0.0016539827195947239,
            -0.0012066241021841376, -0.0011042186455562336,
            -0.0008051343572505189, 0.0016229491738044255,
            0.0011833923257801077, 0.0006020440527161554,
            0.00047526899287409847, 0.00047526899287409847,
            0.00037538142786805136, -0.0012811397377036637,
            -0.0010116898098710776, -0.0008465095301785942,
            -0.0006683577463042215, 0.0012459039620461505,
            0.0009836962283627838, 0.00077088529431722, 0.0006105807630364827,
            0.0006105807630364827, 0.00048361458700877996,
            -0.0016444700616024337, -0.001302510079662288,
            -0.0010856603485807576, -0.0008598975276238373,
            0.00159730642327918, 0.001265146946434076, 0.0008495806081447204,
            0.000671787466824433, 0.000671787466824433, 0.0005312928157964384,
            -0.0018105890543181475, -0.001431844407277983,
            -0.0011956722392735362, -0.000945544277375045,
            0.0017590147511761475, 0.0013910348287283414,
            0.0007393644735054756, 0.0005850536182149991,
            0.0005850536182149991, 0.0004631887654949332,
            -0.0015760302086346792, -0.0012475134925387294,
            -0.001041074331192672, -0.0008239586048523492,
            0.0015319673563669856, 0.0012124704278707746
        ]

        places = 10
        np.testing.assert_almost_equal(model_dout, ref_dout, places)
示例#4
0
    def test_model(self):
        jfile = 'polar_se_a.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have (jdata['model']['descriptor'], 'rcut')
        
        data = DataSystem(systems, set_pfx, batch_size, test_size, rcut, run_opt = None)
        
        test_data = data.get_test ()
        numb_test = 1
        
        jdata['model']['descriptor'].pop('type', None)
        descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed = True)
        jdata['model']['fitting_net'].pop('type', None)
        jdata['model']['fitting_net'].pop('fit_diag', None)
        jdata['model']['fitting_net']['descrpt'] = descrpt
        fitting = DipoleFittingSeA(**jdata['model']['fitting_net'], uniform_seed = True)
        model = DipoleModel(descrpt, fitting)

        # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']])
        input_data = {'coord' : [test_data['coord']], 
                      'box': [test_data['box']], 
                      'type': [test_data['type']],
                      'natoms_vec' : [test_data['natoms_vec']],
                      'default_mesh' : [test_data['default_mesh']],
                      'fparam': [test_data['fparam']],
        }
        model._compute_input_stat(input_data)

        t_prop_c           = tf.placeholder(tf.float32, [5],    name='t_prop_c')
        t_energy           = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None], name='t_energy')
        t_force            = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_force')
        t_virial           = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_virial')
        t_atom_ener        = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_atom_ener')
        t_coord            = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='i_coord')
        t_type             = tf.placeholder(tf.int32,   [None], name='i_type')
        t_natoms           = tf.placeholder(tf.int32,   [model.ntypes+2], name='i_natoms')
        t_box              = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name='i_box')
        t_mesh             = tf.placeholder(tf.int32,   [None], name='i_mesh')
        is_training        = tf.placeholder(tf.bool)
        t_fparam = None

        model_pred \
            = model.build (t_coord, 
                           t_type, 
                           t_natoms, 
                           t_box, 
                           t_mesh,
                           t_fparam,
                           suffix = "dipole_se_a", 
                           reuse = False)
        dipole = model_pred['dipole']
        gdipole = model_pred['global_dipole']
        force = model_pred['force']
        virial = model_pred['virial']
        atom_virial = model_pred['atom_virial']

        feed_dict_test = {t_prop_c:        test_data['prop_c'],
                          t_coord:         np.reshape(test_data['coord']    [:numb_test, :], [-1]),
                          t_box:           test_data['box']                 [:numb_test, :],
                          t_type:          np.reshape(test_data['type']     [:numb_test, :], [-1]),
                          t_natoms:        test_data['natoms_vec'],
                          t_mesh:          test_data['default_mesh'],
                          is_training:     False}

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [p, gp] = sess.run([dipole, gdipole], feed_dict = feed_dict_test)

        p = p.reshape([-1])
        refp = [1.616802262298876514e+01,9.809535439521079425e+00,3.572312180768947854e-01,1.336308874095981203e+00,1.057908563208963848e+01,-5.999602350098874881e-01]

        places = 10
        np.testing.assert_almost_equal(p, refp, places)

        gp = gp.reshape([-1])
        refgp = np.array(refp).reshape(-1, 3).sum(0)

        places = 9
        np.testing.assert_almost_equal(gp, refgp, places)

        # make sure only one frame is used
        feed_dict_single = {t_prop_c:        test_data['prop_c'],
                            t_coord:         np.reshape(test_data['coord']    [:1, :], [-1]),
                            t_box:           test_data['box']                 [:1, :],
                            t_type:          np.reshape(test_data['type']     [:1, :], [-1]),
                            t_natoms:        test_data['natoms_vec'],
                            t_mesh:          test_data['default_mesh'],
                            is_training:     False}

        [pf, pv, pav] = sess.run([force, virial, atom_virial], feed_dict = feed_dict_single)
        pf, pv = pf.reshape(-1), pv.reshape(-1)
        spv = pav.reshape(1, 3, -1, 9).sum(2).reshape(-1)

        base_dict = feed_dict_single.copy()
        coord0 = base_dict.pop(t_coord)
        box0 = base_dict.pop(t_box)

        fdf = - finite_difference(
                    lambda coord: sess.run(gdipole, 
                        feed_dict={**base_dict, 
                                t_coord:coord, 
                                t_box:box0}).reshape(-1),
                    test_data['coord'][:numb_test, :].reshape([-1])).reshape(-1)
        fdv = - (finite_difference(
                    lambda box: sess.run(gdipole, 
                        feed_dict={**base_dict, 
                                t_coord:strerch_box(coord0, box0, box), 
                                t_box:box}).reshape(-1),
                    test_data['box'][:numb_test, :]).reshape([-1,3,3]).transpose(0,2,1)
                @ box0.reshape(3,3)).reshape(-1)

        delta = 1e-5
        np.testing.assert_allclose(pf, fdf, delta)
        np.testing.assert_allclose(pv, fdv, delta)
        # make sure atomic virial sum to virial
        places = 10
        np.testing.assert_almost_equal(pv, spv, places)
示例#5
0
    def test_descriptor_one_side_exclude_types(self):
        """When we enable type_one_side, the descriptor should be the same
        for different types, when its environments are the same.

        Here we generates two data. The only difference is the type:
        (1) 0 1 1 1 1 1
        (2) 1 1 1 1 1 1

        When type_one_side is true, the first atom should have the same descriptor.
        Otherwise, it should be different (with random initial variables). We test
        both situation.
        """
        jfile = 'water_se_a.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        rcut = j_must_have(jdata['model']['descriptor'], 'rcut')
        sel = j_must_have(jdata['model']['descriptor'], 'sel')
        ntypes = len(sel)

        data = DataSystem(systems,
                          set_pfx,
                          batch_size,
                          test_size,
                          rcut,
                          run_opt=None)

        test_data = data.get_test()
        numb_test = 1

        # set parameters
        jdata['model']['descriptor']['neuron'] = [5, 5, 5]
        jdata['model']['descriptor']['axis_neuron'] = 2
        jdata['model']['descriptor']['type_one_side'] = True
        jdata['model']['descriptor']['exclude_types'] = [[0, 0]]

        t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c')
        t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='i_coord')
        t_type = tf.placeholder(tf.int32, [None], name='i_type')
        t_natoms = tf.placeholder(tf.int32, [ntypes + 2], name='i_natoms')
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9],
                               name='i_box')
        t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh')
        is_training = tf.placeholder(tf.bool)

        # successful
        descrpt = Descriptor(**jdata['model']['descriptor'])
        dout \
            = descrpt.build(
                t_coord,
                t_type,
                t_natoms,
                t_box,
                t_mesh,
                {},
                reuse = False,
                suffix = "_se_a_1side_exclude_types"
            )
        # failed
        descrpt_failed = Descriptor(
            **{
                **jdata['model']['descriptor'], "type_one_side": False
            })
        dout_failed \
            = descrpt_failed.build(
                t_coord,
                t_type,
                t_natoms,
                t_box,
                t_mesh,
                {},
                reuse = False,
                suffix = "_se_a_1side_exclude_types_failed"
            )

        feed_dict_test1 = {
            t_prop_c: test_data['prop_c'],
            t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]),
            t_box: test_data['box'][:numb_test, :],
            t_type: np.reshape(test_data['type'][:numb_test, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            is_training: False
        }
        feed_dict_test2 = feed_dict_test1.copy()
        # original type: 0 0 1 1 1 1
        # current: 0 1 1 1 1 1
        # current: 1 1 1 1 1 1
        new_natoms1 = test_data['natoms_vec'].copy()
        new_natoms1[2] = 1
        new_natoms1[3] = 5
        new_type1 = test_data['type'].copy()
        new_type1[:numb_test, 0] = 0
        new_type1[:numb_test, 1:6] = 1
        new_natoms2 = test_data['natoms_vec'].copy()
        new_natoms2[2] = 0
        new_natoms2[3] = 6
        new_type2 = test_data['type'].copy()
        new_type2[:numb_test] = 1
        feed_dict_test1[t_type] = np.reshape(new_type1[:numb_test, :], [-1])
        feed_dict_test1[t_natoms] = new_natoms1
        feed_dict_test2[t_type] = np.reshape(new_type2[:numb_test, :], [-1])
        feed_dict_test2[t_natoms] = new_natoms2
        print(feed_dict_test1, feed_dict_test2)

        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            [model_dout1] = sess.run([dout], feed_dict=feed_dict_test1)
            [model_dout2] = sess.run([dout], feed_dict=feed_dict_test2)
            [model_dout1_failed] = sess.run([dout_failed],
                                            feed_dict=feed_dict_test1)
            [model_dout2_failed] = sess.run([dout_failed],
                                            feed_dict=feed_dict_test2)
        model_dout1 = model_dout1.reshape([6, -1])
        model_dout2 = model_dout2.reshape([6, -1])
        model_dout1_failed = model_dout1_failed.reshape([6, -1])
        model_dout2_failed = model_dout2_failed.reshape([6, -1])

        np.testing.assert_almost_equal(model_dout1[0], model_dout2[0], 10)
        with self.assertRaises(AssertionError):
            np.testing.assert_almost_equal(model_dout1_failed[0],
                                           model_dout2_failed[0], 10)
    def test_model(self):
        jfile = 'water_se_a_fparam.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have(jdata['model']['descriptor'], 'rcut')

        data = DataSystem(systems,
                          set_pfx,
                          batch_size,
                          test_size,
                          rcut,
                          run_opt=None)

        test_data = data.get_test()
        numb_test = 1

        jdata['model']['descriptor'].pop('type', None)
        descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed=True)
        jdata['model']['fitting_net']['descrpt'] = descrpt
        fitting = EnerFitting(**jdata['model']['fitting_net'],
                              uniform_seed=True)
        # descrpt = DescrptSeA(jdata['model']['descriptor'])
        # fitting = EnerFitting(jdata['model']['fitting_net'], descrpt)
        model = EnerModel(descrpt, fitting)

        # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']])
        input_data = {
            'coord': [test_data['coord']],
            'box': [test_data['box']],
            'type': [test_data['type']],
            'natoms_vec': [test_data['natoms_vec']],
            'default_mesh': [test_data['default_mesh']],
            'fparam': [test_data['fparam']],
        }
        model._compute_input_stat(input_data)
        model.descrpt.bias_atom_e = data.compute_energy_shift()

        t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c')
        t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None],
                                  name='t_energy')
        t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='t_force')
        t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                  name='t_virial')
        t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                     name='t_atom_ener')
        t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='i_coord')
        t_type = tf.placeholder(tf.int32, [None], name='i_type')
        t_natoms = tf.placeholder(tf.int32, [model.ntypes + 2],
                                  name='i_natoms')
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9],
                               name='i_box')
        t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh')
        t_fparam = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                  name='i_fparam')
        is_training = tf.placeholder(tf.bool)
        input_dict = {}
        input_dict['fparam'] = t_fparam

        model_pred\
            = model.build (t_coord,
                           t_type,
                           t_natoms,
                           t_box,
                           t_mesh,
                           input_dict,
                           suffix = "se_a_fparam",
                           reuse = False)
        energy = model_pred['energy']
        force = model_pred['force']
        virial = model_pred['virial']
        atom_ener = model_pred['atom_ener']

        feed_dict_test = {
            t_prop_c: test_data['prop_c'],
            t_energy: test_data['energy'][:numb_test],
            t_force: np.reshape(test_data['force'][:numb_test, :], [-1]),
            t_virial: np.reshape(test_data['virial'][:numb_test, :], [-1]),
            t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :],
                                    [-1]),
            t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]),
            t_box: test_data['box'][:numb_test, :],
            t_type: np.reshape(test_data['type'][:numb_test, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            t_fparam: np.reshape(test_data['fparam'][:numb_test, :], [-1]),
            is_training: False
        }

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [e, f, v] = sess.run([energy, force, virial], feed_dict=feed_dict_test)

        e = e.reshape([-1])
        f = f.reshape([-1])
        v = v.reshape([-1])
        refe = [61.35473702079649]
        reff = [
            7.789591210641927388e-02, 9.411176646369459609e-02,
            3.785806413688173194e-03, 1.430830954178063386e-01,
            1.146964190520970150e-01, -1.320340288927138173e-02,
            -7.308720494747594776e-02, 6.508269338140809657e-02,
            5.398739145542804643e-04, 5.863268336973800898e-02,
            -1.603409523950408699e-01, -5.083084610994957619e-03,
            -2.551569799443983988e-01, 3.087934885732580501e-02,
            1.508590526622844222e-02, 4.863249399791078065e-02,
            -1.444292753594846324e-01, -1.125098094204559241e-03
        ]
        refv = [
            -6.069498397488943819e-01, 1.101778888191114192e-01,
            1.981907430646132409e-02, 1.101778888191114608e-01,
            -3.315612988100872793e-01, -5.999739184898976799e-03,
            1.981907430646132756e-02, -5.999739184898974197e-03,
            -1.198656608172396325e-03
        ]
        refe = np.reshape(refe, [-1])
        reff = np.reshape(reff, [-1])
        refv = np.reshape(refv, [-1])

        places = 10
        np.testing.assert_almost_equal(e, refe, places)
        np.testing.assert_almost_equal(f, reff, places)
        np.testing.assert_almost_equal(v, refv, places)
示例#7
0
    def test_model(self):
        jfile = 'wfc.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have(jdata['model']['descriptor'], 'rcut')

        data = DataSystem(systems,
                          set_pfx,
                          batch_size,
                          test_size,
                          rcut,
                          run_opt=None)

        test_data = data.get_test()
        numb_test = 1

        jdata['model']['descriptor'].pop('type', None)
        jdata['model']['descriptor'].pop('_comment', None)
        descrpt = DescrptLocFrame(**jdata['model']['descriptor'])
        jdata['model']['fitting_net']['uniform_seed'] = True
        fitting = WFCFitting(jdata['model']['fitting_net'], descrpt)
        model = WFCModel(descrpt, fitting)

        input_data = {
            'coord': [test_data['coord']],
            'box': [test_data['box']],
            'type': [test_data['type']],
            'natoms_vec': [test_data['natoms_vec']],
            'default_mesh': [test_data['default_mesh']],
            'fparam': [test_data['fparam']],
        }
        model._compute_input_stat(input_data)

        t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c')
        t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None],
                                  name='t_energy')
        t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='t_force')
        t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                  name='t_virial')
        t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                     name='t_atom_ener')
        t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='i_coord')
        t_type = tf.placeholder(tf.int32, [None], name='i_type')
        t_natoms = tf.placeholder(tf.int32, [model.ntypes + 2],
                                  name='i_natoms')
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9],
                               name='i_box')
        t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh')
        is_training = tf.placeholder(tf.bool)
        t_fparam = None

        model_pred \
            = model.build (t_coord,
                           t_type,
                           t_natoms,
                           t_box,
                           t_mesh,
                           t_fparam,
                           suffix = "wfc",
                           reuse = False)
        wfc = model_pred['wfc']

        feed_dict_test = {
            t_prop_c: test_data['prop_c'],
            t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]),
            t_box: test_data['box'][:numb_test, :],
            t_type: np.reshape(test_data['type'][:numb_test, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            is_training: False
        }

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [p] = sess.run([wfc], feed_dict=feed_dict_test)

        p = p.reshape([-1])
        refp = [
            -9.105016838228578990e-01, 7.196284362034099935e-01,
            -9.548516928185298014e-02, 2.764615027095288724e+00,
            2.661319598995644520e-01, 7.579512949131941846e-02,
            -2.107409067376114997e+00, -1.299080016614967414e-01,
            -5.962778584850070285e-01, 2.913899917663253514e-01,
            -1.226917174638697094e+00, 1.829523069930876655e+00,
            1.015704024959750873e+00, -1.792333611099589386e-01,
            5.032898080485321834e-01, 1.808561721292949453e-01,
            2.468863482075112081e+00, -2.566442546384765100e-01,
            -1.467453783795173994e-01, -1.822963931552128658e+00,
            5.843600156865462747e-01, -1.493875280832117403e+00,
            1.693322352814763398e-01, -1.877325443995481624e+00
        ]

        places = 6
        np.testing.assert_almost_equal(p, refp, places)
示例#8
0
    def test_fitting(self):
        jfile = 'water_se_a_type.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have(jdata['model']['descriptor'], 'rcut')
        sel = j_must_have(jdata['model']['descriptor'], 'sel')
        ntypes = len(sel)

        data = DataSystem(systems,
                          set_pfx,
                          batch_size,
                          test_size,
                          rcut,
                          run_opt=None)

        test_data = data.get_test()
        numb_test = 1

        jdata['model']['descriptor']['neuron'] = [5, 5, 5]
        jdata['model']['descriptor']['axis_neuron'] = 2

        jdata['model']['descriptor'].pop('type', None)
        descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed=True)
        jdata['model']['fitting_net']['descrpt'] = descrpt
        fitting = EnerFitting(**jdata['model']['fitting_net'],
                              uniform_seed=True)

        # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']])
        input_data = {
            'coord': [test_data['coord']],
            'box': [test_data['box']],
            'type': [test_data['type']],
            'natoms_vec': [test_data['natoms_vec']],
            'default_mesh': [test_data['default_mesh']]
        }

        t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c')
        t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None],
                                  name='t_energy')
        t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='t_force')
        t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                  name='t_virial')
        t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                     name='t_atom_ener')
        t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='i_coord')
        t_type = tf.placeholder(tf.int32, [None], name='i_type')
        t_natoms = tf.placeholder(tf.int32, [ntypes + 2], name='i_natoms')
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9],
                               name='i_box')
        t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh')
        is_training = tf.placeholder(tf.bool)
        t_fparam = None

        dout = np.array([
            0.0005722682145569174, -0.00020202686217742682,
            -0.00020202686217742682, 7.13250554992363e-05,
            -0.0014770058171250015, 0.000521468690207748,
            -0.001143865186937176, 0.0004038453384193948,
            0.0005617335409639567, -0.00019831394075147532,
            0.00048086740718842236, -0.0001693584775806112,
            -0.0001693584775806112, 5.966987137476082e-05,
            -0.0012342029581315136, 0.00043492340851472783,
            -0.0009566016612537016, 0.00033706767041080107,
            0.00047065988464132244, -0.0001657950398095401,
            0.0003647849239740657, -0.00013744939018250384,
            -0.00013744939018250384, 5.1825826955234744e-05,
            -0.00096004206555711, 0.00036185565262332876,
            -0.0007267433909643961, 0.0002738914365542745,
            0.00038019365906978136, -0.00014322754331896057,
            0.0004675256930823109, -0.00017634410399626168,
            -0.00017634410399626168, 6.652672908755666e-05,
            -0.0012328062885292486, 0.00046500213384094614,
            -0.0009328887521346069, 0.0003518668613172834,
            0.0004877847509912577, -0.00018396318824508986,
            0.0005154794374703516, -0.00019422534512034776,
            -0.00019422534512034776, 7.318151797939947e-05,
            -0.0013576642997136488, 0.0005115548790018505,
            -0.0010275333676074971, 0.00038716440070070385,
            0.0005376426714609369, -0.00020257810468163985,
            0.0004482204892297628, -0.00016887749501640607,
            -0.00016887749501640607, 6.364643102775375e-05,
            -0.001181345877677835, 0.0004452029242063362,
            -0.0008941636427724908, 0.0003369586197174627,
            0.0004677878512312651, -0.00017625260641095753
        ])
        type_embedding = np.array([
            1.4916816460764615, 0.2720153234707013, -2.4385153754181985,
            -1.8454294510880027, 2.874575701113528, 1.1225116575801295,
            0.4204818970813372, -2.3784087249787587, -1.5053748251050598,
            2.769329403073084
        ])

        dout = dout.reshape([-1, 10])
        type_embedding = type_embedding.reshape([ntypes, -1])
        atom_ener = fitting.build(
            tf.convert_to_tensor(dout),
            t_natoms, {'type_embedding': tf.convert_to_tensor(type_embedding)},
            reuse=False,
            suffix="se_a_type_fit_")

        feed_dict_test = {
            t_prop_c: test_data['prop_c'],
            t_energy: test_data['energy'][:numb_test],
            t_force: np.reshape(test_data['force'][:numb_test, :], [-1]),
            t_virial: np.reshape(test_data['virial'][:numb_test, :], [-1]),
            t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :],
                                    [-1]),
            t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]),
            t_box: test_data['box'][:numb_test, :],
            t_type: np.reshape(test_data['type'][:numb_test, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            is_training: False
        }

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [pred_atom_ener] = sess.run([atom_ener], feed_dict=feed_dict_test)

        pred_atom_ener = pred_atom_ener.reshape([-1])

        ref_atom_ener = [
            10.121733946849165, 10.121712105320634, 10.143275419743475,
            10.143299785396826, 10.143311150431957, 10.143295201182019
        ]

        places = 10

        np.testing.assert_almost_equal(pred_atom_ener, ref_atom_ener, places)
示例#9
0
    def test_model(self):
        jfile = 'polar_se_a.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have(jdata['model']['descriptor'], 'rcut')

        data = DataSystem(systems,
                          set_pfx,
                          batch_size,
                          test_size,
                          rcut,
                          run_opt=None)

        test_data = data.get_test()
        numb_test = 1

        jdata['model']['descriptor'].pop('type', None)
        jdata['model']['fitting_net'].pop('type', None)
        descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed=True)
        jdata['model']['fitting_net']['descrpt'] = descrpt
        fitting = PolarFittingSeA(**jdata['model']['fitting_net'],
                                  uniform_seed=True)
        model = PolarModel(descrpt, fitting)

        # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']])
        input_data = {
            'coord': [test_data['coord']],
            'box': [test_data['box']],
            'type': [test_data['type']],
            'natoms_vec': [test_data['natoms_vec']],
            'default_mesh': [test_data['default_mesh']],
            'fparam': [test_data['fparam']],
        }
        model._compute_input_stat(input_data)

        t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c')
        t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None],
                                  name='t_energy')
        t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='t_force')
        t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                  name='t_virial')
        t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                     name='t_atom_ener')
        t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='i_coord')
        t_type = tf.placeholder(tf.int32, [None], name='i_type')
        t_natoms = tf.placeholder(tf.int32, [model.ntypes + 2],
                                  name='i_natoms')
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9],
                               name='i_box')
        t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh')
        is_training = tf.placeholder(tf.bool)
        t_fparam = None

        model_pred \
            = model.build (t_coord,
                           t_type,
                           t_natoms,
                           t_box,
                           t_mesh,
                           t_fparam,
                           suffix = "polar_se_a",
                           reuse = False)
        polar = model_pred['polar']
        gpolar = model_pred['global_polar']
        force = model_pred['force']
        virial = model_pred['virial']
        atom_virial = model_pred['atom_virial']

        feed_dict_test = {
            t_prop_c: test_data['prop_c'],
            t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]),
            t_box: test_data['box'][:numb_test, :],
            t_type: np.reshape(test_data['type'][:numb_test, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            is_training: False
        }

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [p, gp] = sess.run([polar, gpolar], feed_dict=feed_dict_test)

        p = p.reshape([-1])
        refp = [
            3.39695248e+01, 2.16564043e+01, 8.18501479e-01, 2.16564043e+01,
            1.38211789e+01, 5.22775159e-01, 8.18501479e-01, 5.22775159e-01,
            1.97847218e-02, 8.08467431e-01, 3.42081126e+00, -2.01072261e-01,
            3.42081126e+00, 1.54924596e+01, -9.06153697e-01, -2.01072261e-01,
            -9.06153697e-01, 5.30193262e-02
        ]

        places = 6
        np.testing.assert_almost_equal(p, refp, places)

        gp = gp.reshape([-1])
        refgp = np.array(refp).reshape(-1, 9).sum(0)

        places = 5
        np.testing.assert_almost_equal(gp, refgp, places)

        # make sure only one frame is used
        feed_dict_single = {
            t_prop_c: test_data['prop_c'],
            t_coord: np.reshape(test_data['coord'][:1, :], [-1]),
            t_box: test_data['box'][:1, :],
            t_type: np.reshape(test_data['type'][:1, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            is_training: False
        }

        [pf, pv, pav] = sess.run([force, virial, atom_virial],
                                 feed_dict=feed_dict_single)
        pf, pv = pf.reshape(-1), pv.reshape(-1)
        spv = pav.reshape(1, 9, -1, 9).sum(2).reshape(-1)

        base_dict = feed_dict_single.copy()
        coord0 = base_dict.pop(t_coord)
        box0 = base_dict.pop(t_box)

        fdf = -finite_difference(
            lambda coord: sess.run(gpolar,
                                   feed_dict={
                                       **base_dict, t_coord: coord,
                                       t_box: box0
                                   }).reshape(-1),
            test_data['coord'][:numb_test, :].reshape([-1])).reshape(-1)
        fdv = -(finite_difference(
            lambda box: sess.run(
                gpolar,
                feed_dict={
                    **base_dict, t_coord: strerch_box(coord0, box0, box),
                    t_box: box
                }).reshape(-1), test_data['box'][:numb_test, :]).reshape([
                    -1, 3, 3
                ]).transpose(0, 2, 1) @ box0.reshape(3, 3)).reshape(-1)

        delta = 1e-4
        np.testing.assert_allclose(pf, fdf, delta)
        np.testing.assert_allclose(pv, fdv, delta)
        # make sure atomic virial sum to virial
        places = 10
        np.testing.assert_almost_equal(pv, spv, places)
示例#10
0
    def test_model(self):
        jfile = 'water_se_a_srtab.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have (jdata['model']['descriptor'], 'rcut')
        
        data = DataSystem(systems, set_pfx, batch_size, test_size, rcut, run_opt = None)
        
        test_data = data.get_test ()
        numb_test = 1
        
        jdata['model']['descriptor'].pop('type', None)        
        descrpt = DescrptSeA(**jdata['model']['descriptor'], uniform_seed = True)
        jdata['model']['fitting_net']['descrpt'] = descrpt
        fitting = EnerFitting(**jdata['model']['fitting_net'], uniform_seed = True)
        # descrpt = DescrptSeA(jdata['model']['descriptor'])
        # fitting = EnerFitting(jdata['model']['fitting_net'], descrpt)
        model = EnerModel(
            descrpt, 
            fitting, 
            None,
            jdata['model'].get('type_map'),
            jdata['model'].get('data_stat_nbatch'),
            jdata['model'].get('data_stat_protect'),
            jdata['model'].get('use_srtab'),
            jdata['model'].get('smin_alpha'),
            jdata['model'].get('sw_rmin'),
            jdata['model'].get('sw_rmax')
        )

        # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']])
        input_data = {'coord' : [test_data['coord']], 
                      'box': [test_data['box']], 
                      'type': [test_data['type']],
                      'natoms_vec' : [test_data['natoms_vec']],
                      'default_mesh' : [test_data['default_mesh']]
        }
        model._compute_input_stat(input_data)
        model.descrpt.bias_atom_e = data.compute_energy_shift()

        t_prop_c           = tf.placeholder(tf.float32, [5],    name='t_prop_c')
        t_energy           = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None], name='t_energy')
        t_force            = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_force')
        t_virial           = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_virial')
        t_atom_ener        = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='t_atom_ener')
        t_coord            = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None], name='i_coord')
        t_type             = tf.placeholder(tf.int32,   [None], name='i_type')
        t_natoms           = tf.placeholder(tf.int32,   [model.ntypes+2], name='i_natoms')
        t_box              = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9], name='i_box')
        t_mesh             = tf.placeholder(tf.int32,   [None], name='i_mesh')
        is_training        = tf.placeholder(tf.bool)
        t_fparam = None

        model_pred\
            = model.build (t_coord, 
                           t_type, 
                           t_natoms, 
                           t_box, 
                           t_mesh,
                           t_fparam,
                           suffix = "se_a_srtab", 
                           reuse = False)
        energy = model_pred['energy']
        force  = model_pred['force']
        virial = model_pred['virial']
        atom_ener =  model_pred['atom_ener']

        feed_dict_test = {t_prop_c:        test_data['prop_c'],
                          t_energy:        test_data['energy']              [:numb_test],
                          t_force:         np.reshape(test_data['force']    [:numb_test, :], [-1]),
                          t_virial:        np.reshape(test_data['virial']   [:numb_test, :], [-1]),
                          t_atom_ener:     np.reshape(test_data['atom_ener'][:numb_test, :], [-1]),
                          t_coord:         np.reshape(test_data['coord']    [:numb_test, :], [-1]),
                          t_box:           test_data['box']                 [:numb_test, :],
                          t_type:          np.reshape(test_data['type']     [:numb_test, :], [-1]),
                          t_natoms:        test_data['natoms_vec'],
                          t_mesh:          test_data['default_mesh'],
                          is_training:     False}

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [e, f, v] = sess.run([energy, force, virial], 
                             feed_dict = feed_dict_test)

        e = e.reshape([-1])
        f = f.reshape([-1])
        v = v.reshape([-1])

        refe = [1.141610882066236599e+02]
        reff = [-1.493121233165248043e+02,-1.831419491743885715e+02,-8.439542992300344437e+00,-1.811987095947552859e+02,-1.476380826187439084e+02,1.264271856742560018e+01,1.544377958934875323e+02,-7.816520233903435866e+00,1.287925245463442225e+00,-4.000393268449002449e+00,1.910748885843098890e+02,7.134789955349889468e+00,1.826908441979261113e+02,3.677156386479059513e+00,-1.122312112141401741e+01,-2.617413911684622008e+00,1.438445070562470391e+02,-1.402769654524568033e+00]
        refv = [3.585047655925112622e+02,-7.569252978336677984e+00,-1.068382043878426124e+01,-7.569252978336677096e+00,3.618439481685132932e+02,5.448668500896081568e+00,-1.068382043878426302e+01,5.448668500896082456e+00,1.050393462151727686e+00]
        refe = np.reshape(refe, [-1])
        reff = np.reshape(reff, [-1])
        refv = np.reshape(refv, [-1])

        places = 10
        np.testing.assert_almost_equal(e, refe, places)
        np.testing.assert_almost_equal(f, reff, places)
        np.testing.assert_almost_equal(v, refv, places)
示例#11
0
    def test_model(self):
        jfile = 'water_se_t.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have(jdata['model']['descriptor'], 'rcut')

        data = DataSystem(systems,
                          set_pfx,
                          batch_size,
                          test_size,
                          rcut,
                          run_opt=None)

        test_data = data.get_test()
        numb_test = 1

        jdata['model']['descriptor'].pop('type', None)
        descrpt = DescrptSeT(**jdata['model']['descriptor'], uniform_seed=True)
        jdata['model']['fitting_net']['descrpt'] = descrpt
        fitting = EnerFitting(**jdata['model']['fitting_net'],
                              uniform_seed=True)
        model = EnerModel(descrpt, fitting)

        input_data = {
            'coord': [test_data['coord']],
            'box': [test_data['box']],
            'type': [test_data['type']],
            'natoms_vec': [test_data['natoms_vec']],
            'default_mesh': [test_data['default_mesh']]
        }
        model._compute_input_stat(input_data)
        model.descrpt.bias_atom_e = data.compute_energy_shift()

        t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c')
        t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None],
                                  name='t_energy')
        t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='t_force')
        t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                  name='t_virial')
        t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                     name='t_atom_ener')
        t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='i_coord')
        t_type = tf.placeholder(tf.int32, [None], name='i_type')
        t_natoms = tf.placeholder(tf.int32, [model.ntypes + 2],
                                  name='i_natoms')
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9],
                               name='i_box')
        t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh')
        is_training = tf.placeholder(tf.bool)
        t_fparam = None

        model_pred\
            = model.build (t_coord,
                           t_type,
                           t_natoms,
                           t_box,
                           t_mesh,
                           t_fparam,
                           suffix = "se_t",
                           reuse = False)
        energy = model_pred['energy']
        force = model_pred['force']
        virial = model_pred['virial']
        atom_ener = model_pred['atom_ener']

        feed_dict_test = {
            t_prop_c: test_data['prop_c'],
            t_energy: test_data['energy'][:numb_test],
            t_force: np.reshape(test_data['force'][:numb_test, :], [-1]),
            t_virial: np.reshape(test_data['virial'][:numb_test, :], [-1]),
            t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :],
                                    [-1]),
            t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]),
            t_box: test_data['box'][:numb_test, :],
            t_type: np.reshape(test_data['type'][:numb_test, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            is_training: False
        }

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [e, f, v] = sess.run([energy, force, virial], feed_dict=feed_dict_test)

        e = e.reshape([-1])
        f = f.reshape([-1])
        v = v.reshape([-1])
        np.savetxt('e.out', e.reshape([1, -1]))
        np.savetxt('f.out', f.reshape([1, -1]), delimiter=',')
        np.savetxt('v.out', v.reshape([1, -1]), delimiter=',')
        refe = [4.8436558582194039e+01]
        reff = [
            5.2896335066946598e+00, 5.5778402259211131e+00,
            2.6839994229557251e-01, 5.3528786387686784e+00,
            5.2477755362164968e+00, -4.0486366542657343e-01,
            -5.1297084055340498e+00, 3.4607112287117253e-01,
            -5.1800783428369482e-02, 1.5557068351407846e-01,
            -5.9071343228741506e+00, -2.2012359669589748e-01,
            -5.9156735320857488e+00, 8.8397615509389127e-02,
            3.6701215949753935e-01, 2.4729910864238122e-01,
            -5.3529501776440211e+00, 4.1375943757728552e-02
        ]
        refv = [
            -1.3159448660141607e+01, 4.6952048725161544e-01,
            3.5482003698976106e-01, 4.6952048725161577e-01,
            -1.2178990983673918e+01, -1.6867277410496895e-01,
            3.5482003698976106e-01, -1.6867277410496900e-01,
            -3.3986741457321945e-02
        ]
        refe = np.reshape(refe, [-1])
        reff = np.reshape(reff, [-1])
        refv = np.reshape(refv, [-1])

        places = 6
        np.testing.assert_almost_equal(e, refe, places)
        np.testing.assert_almost_equal(f, reff, places)
        np.testing.assert_almost_equal(v, refv, places)
示例#12
0
    def test_model(self):
        jfile = 'water_se_r.json'
        jdata = j_loader(jfile)

        systems = j_must_have(jdata, 'systems')
        set_pfx = j_must_have(jdata, 'set_prefix')
        batch_size = j_must_have(jdata, 'batch_size')
        test_size = j_must_have(jdata, 'numb_test')
        batch_size = 1
        test_size = 1
        stop_batch = j_must_have(jdata, 'stop_batch')
        rcut = j_must_have(jdata['model']['descriptor'], 'rcut')

        data = DataSystem(systems,
                          set_pfx,
                          batch_size,
                          test_size,
                          rcut,
                          run_opt=None)

        test_data = data.get_test()
        numb_test = 1

        jdata['model']['descriptor'].pop('type', None)
        descrpt = DescrptSeR(**jdata['model']['descriptor'], uniform_seed=True)
        jdata['model']['fitting_net']['descrpt'] = descrpt
        fitting = EnerFitting(**jdata['model']['fitting_net'],
                              uniform_seed=True)
        # fitting = EnerFitting(jdata['model']['fitting_net'], descrpt)
        model = EnerModel(descrpt, fitting)

        # model._compute_dstats([test_data['coord']], [test_data['box']], [test_data['type']], [test_data['natoms_vec']], [test_data['default_mesh']])
        input_data = {
            'coord': [test_data['coord']],
            'box': [test_data['box']],
            'type': [test_data['type']],
            'natoms_vec': [test_data['natoms_vec']],
            'default_mesh': [test_data['default_mesh']]
        }
        model._compute_input_stat(input_data)
        model.descrpt.bias_atom_e = data.compute_energy_shift()

        t_prop_c = tf.placeholder(tf.float32, [5], name='t_prop_c')
        t_energy = tf.placeholder(GLOBAL_ENER_FLOAT_PRECISION, [None],
                                  name='t_energy')
        t_force = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='t_force')
        t_virial = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                  name='t_virial')
        t_atom_ener = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                     name='t_atom_ener')
        t_coord = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None],
                                 name='i_coord')
        t_type = tf.placeholder(tf.int32, [None], name='i_type')
        t_natoms = tf.placeholder(tf.int32, [model.ntypes + 2],
                                  name='i_natoms')
        t_box = tf.placeholder(GLOBAL_TF_FLOAT_PRECISION, [None, 9],
                               name='i_box')
        t_mesh = tf.placeholder(tf.int32, [None], name='i_mesh')
        is_training = tf.placeholder(tf.bool)
        t_fparam = None

        model_pred\
            = model.build (t_coord,
                           t_type,
                           t_natoms,
                           t_box,
                           t_mesh,
                           t_fparam,
                           suffix = "se_r",
                           reuse = False)
        energy = model_pred['energy']
        force = model_pred['force']
        virial = model_pred['virial']
        atom_ener = model_pred['atom_ener']

        feed_dict_test = {
            t_prop_c: test_data['prop_c'],
            t_energy: test_data['energy'][:numb_test],
            t_force: np.reshape(test_data['force'][:numb_test, :], [-1]),
            t_virial: np.reshape(test_data['virial'][:numb_test, :], [-1]),
            t_atom_ener: np.reshape(test_data['atom_ener'][:numb_test, :],
                                    [-1]),
            t_coord: np.reshape(test_data['coord'][:numb_test, :], [-1]),
            t_box: test_data['box'][:numb_test, :],
            t_type: np.reshape(test_data['type'][:numb_test, :], [-1]),
            t_natoms: test_data['natoms_vec'],
            t_mesh: test_data['default_mesh'],
            is_training: False
        }

        sess = self.test_session().__enter__()
        sess.run(tf.global_variables_initializer())
        [e, f, v] = sess.run([energy, force, virial], feed_dict=feed_dict_test)

        e = e.reshape([-1])
        f = f.reshape([-1])
        v = v.reshape([-1])
        refe = [6.152085988309423925e+01]
        reff = [
            -1.714443151616400110e-04, -1.315836609370952051e-04,
            -5.584120460897444674e-06, -7.197863450669731334e-05,
            -1.384609799994930676e-04, 8.856091902774708468e-06,
            1.120578238869146797e-04, -7.428703645877488470e-05,
            9.370560731488587317e-07, -1.048347129617610465e-04,
            1.977876923815685781e-04, 7.522050342771599598e-06,
            2.361772659657814205e-04, -5.774651813388292487e-05,
            -1.233143271630744828e-05, 2.257277740226381951e-08,
            2.042905031476775584e-04, 6.003548585097267914e-07
        ]
        refv = [
            1.035180911513190792e-03, -1.118982949050497126e-04,
            -2.383287813436022850e-05, -1.118982949050497126e-04,
            4.362023915782403281e-04, 8.119543218224559240e-06,
            -2.383287813436022850e-05, 8.119543218224559240e-06,
            1.201142938802945237e-06
        ]
        refe = np.reshape(refe, [-1])
        reff = np.reshape(reff, [-1])
        refv = np.reshape(refv, [-1])

        places = 6
        for ii in range(e.size):
            self.assertAlmostEqual(e[ii], refe[ii], places=places)
        for ii in range(f.size):
            self.assertAlmostEqual(f[ii], reff[ii], places=places)
        for ii in range(v.size):
            self.assertAlmostEqual(v[ii], refv[ii], places=places)