“具有多个元素的数组的真值不明确。为什么会出现此错误?”?

2024-09-27 09:25:12 发布

您现在位置:Python中文网/ 问答频道 /正文

它就像你们中的一些人建议的那样工作,但现在我得到了一堆新的错误。 我正在尝试从csv文件中读取聚合物链的初始坐标,如下所示: -0.350000 , 0.000000 , 0.000000 \ -0.007593 , 0.075808 , 0.540372 \ -0.134408、0.115877、1.384276

在我的代码中,我必须要知道聚合物链之间的距离,我想从数据文件中得到它们的初始坐标。 以下是错误消息: enter image description here

下面是完整的代码:

#Define an object called 'monomer' which consists of a position (list with 3 entries)

class monomer:
    def __init__(self, pos):
    # assign the given position to the 'pos' attribute
        self.pos = pos
        #self.m = np.loadtxt('traj_T_4.csv')

    @classmethod
    def from_file(cls, filename):
        instances = []
        with open(filename, 'r') as file:
            for line in file:
                pos = str(map(float, line.split(',')))
                instances.append(cls(pos))
        return instances

    def LJ(self, other):
    # Define the LJ interaction between one and another monomer

        # Calculate the distance between position of one and the other monomer
        rij =self.distance(other)

        sig_by_r6 = np.power(sigma**2 / rij, 6)
        sig_by_r12 = np.power(sigma**2 / rij, 12)

        return 4*epsilon*(sig_by_r12 - sig_by_r6)

    def FENE(self, other):
        rij =self.distance(other)


        fene = (-0.5 * K * np.power(R,2) * np.log(1 - ((np.sqrt(rij) - r0) / R)**2))


        if not np.isfinite(fene): # Make sure, that NaN (or - infinity) values (because of the log) are never passed to the energy calculation, as the programme behaves uncontrollably then
            return float("inf")   # Rather return infinity, since this will always fail the energy comparison
        else:
            return fene

    def distance(self, other):

        return np.linalg.norm(list(np.array(self.pos) - np.array(other.pos)))

    def shift(self, vector):
    # Move a monomer by 'vector'
         self.pos += vector

# Define an object called 'polymer', which is a chain (list) of monomer objects
class polymer:
    def __init__(self, base, N):

        self.chain = []
        # append each monomer to the polymer's chain
        # base (list of 2) represents the initial x and y value of all monomers
        for i in range(N):
            self.chain.append( monomer([base[0], base[1], i*0.7]) )

    def FENE_Energy(self):
        # Define polymer FENE energy as sum of FENE interactions between bonded monomers
        fene  = 0.0
        for i in range(N-1):
            fene += self.chain[i].FENE(self.chain[i+1])
        return fene

    def LJ_Energy(self):
        LJ = 0.0

        for i in range(N):
            for j in range(N):
                if np.absolute(i-j) > 1 and self.chain[i].distance(self.chain[j]) < rcutoff: # LJ only for non-bonded monomers, i.e. distance is greater than 1
                    LJ += self.chain[i].LJ(self.chain[j])

        return LJ/2.

    def Energy(self):
        return self.LJ_Energy() + self.FENE_Energy()

    def end_to_end_dist(self):
        """
        Figure it out! It is not correct
        """
        R = 0.0
        for i in range(N):
            R = np.linalg.norm(self.chain[i].pos[0] - self.chain[i].pos[1])
        return R

    def com(self):
        cm = [0.0, 0.0, 0.0]

        for i in range(N):
            cm[0] += self.chain[i].pos[0]
            cm[1] += self.chain[i].pos[1]
            cm[2] += self.chain[i].pos[2]
        cm[0] /= N
        cm[1] /= N
        cm[2] /= N
        return cm

    def rg(self):
        r_gyration = 0.0
        cm = self.com()
        rgx = 0.0
        rgy = 0.0
        rgz = 0.0
        for i in range(N):
            rgx += (cm[0] - self.chain[i].pos[0])**2
            rgy += (cm[1] - self.chain[i].pos[1])**2
            rgz += (cm[2] - self.chain[i].pos[2])**2
        rgx /= N**2
        rgy /= N**2
        rgz /= N**2
        r_gyration += np.sqrt(rgx + rgy + rgz)
        return r_gyration

    def print_chain(self):
        for i in range(N):
            print(self.chain[i].pos)
            #print("{0:.6f}".format(float(str(self.chain[i].pos[0]))) + " , " + "{0:.6f}".format(float(str(self.chain[i].pos[1]))) + " , " +  "{0:.6f}".format(float(str(self.chain[i].pos[2]))) + "\n")

    def traj(self):
        for i in range(shifts):
            print(self.chain[i].move)

    def save(self):
        with open('traj_T_04.csv', 'w') as f:
            for i in range(N):
                f.write("{0:.6f}".format(float(str(self.chain[i].pos[0]))) + " , " + "{0:.6f}".format(float(str(self.chain[i].pos[1]))) + " , " + "{0:.6f}".format(float(str(self.chain[i].pos[2]))) + "\n")


    def save_1(self):
        with open('traj1_T_04.csv', 'w') as f:
            for i in range(N):
                f.write("{0:.6f}".format(float(str(self.chain[i].pos[0]))) + " , " + "{0:.6f}".format(float(str(self.chain[i].pos[1]))) + " , " + "{0:.6f}".format(float(str(self.chain[i].pos[2]))) + "\n")

    def move(self): # Method returns a 'polymer' object, which is either the same as put in or a shifted one
        backup = copy.deepcopy(self) # "backup = self" does not work as expected, as 'backup' becomes a mere synonym of 'self', but not an independent (immutable) object, as intended

        monomer_index = np.random.randint(low=1, high=N) # choose a random monomer but 0, since it is grafted

        direction = np.random.rand(3) - [0.5, 0.5, 0.5] # random vector consists of movement between [-0.5, -0.5, -0.5] and [0.5, 0.5, 0.5]

        self.chain[monomer_index].shift(direction)
        if self.chain[monomer_index].pos[2] < 0.0:
            return backup

        rnd = np.random.rand(1)
        deltaE = self.Energy() - backup.Energy()

        if np.exp(-(1/k_B*T)*deltaE) < rnd:
            return backup
        else:
            return self # return the shifted polymer

if __name__ == "__main__":

    #list_of_monomers = polymer(monomer.from_file('traj_T_4.csv'), monomer.from_file('traj1_T_4.csv'))
    #list_of_monomers = monomer.from_file('traj1_T_4.csv')

    K = 40.0
    R = 0.3
    r0 = 0.7
    sigma = r0/2**6
    rcutoff = 2.5*sigma
    epsilon = 1.0
    max_delta = 0.21 # -sigma/10 to sigma/10
    N = 20
    k_B = 1
    T = 4.0
    shifts = 100
# Declare two variables to be 'polymers'
    pol1 = polymer(np.loadtxt('traj_T_4.csv', delimiter=","), N)
    #pol1 = polymer([-r0/2, 0.0], N)
    pol2 = polymer(np.loadtxt('traj1_T_4.csv', delimiter=" , "), N)
    #pol2 = polymer([ r0/2, 0.0], N)


    for j in range(shifts):
        pol1 = pol1.move()
    pol1.save()

    for i in range(shifts):
        pol2 = pol2.move()
    pol2.save_1()

Tags: oftheinposselfchainforreturn

热门问题