I've been trying now for a very long time to come up with a way to easily perform a PES scan in ASE. However, often I am facing the problem that for a change in the bond lengths my structures either do not change at all or completely distort.
I am sending here the code I came up with so far, for a test on a simple CO2 molecule to extend the CO bondlength of one bond up to 3.5 Angstrom while relaxing the structure. The code is then supposed to write a .xyz file for every strcuture and write an output file. However, as I said this does not work unfortunatly.
from ase import Atoms
from ase.calculators.emt import EMT
#from ase.build import molecule
import numpy as np
from ase.constraints import FixBondLength, FixAtoms
from ase.optimize import BFGS
#from ase.io import read, write
import os
atoms = Atoms('CO2', [(0, 0, 0), (0, 0, 1.1), (0, 0, -1.1)])
#atoms = read('POSCAR.xyz', format = 'xyz')
atoms.calc = EMT()
atoms.set_constraint()
constraints= FixAtoms(indices=[0])
atoms.set_constraint(constraints)
bl_values = np.linspace(0.4,3.5,10)
potential_energies = []
if not os.path.exists('output'):
os.mkdir('output')
for i, bl in enumerate(bl_values):
atoms.positions[0,1] = bl
opt = BFGS(atoms, logfile='out.txt')
opt.run(fmax=0.001)
energy = atoms.get_potential_energy()
print('{:<2f} {:<} {:>8} {:>8f} {:<}'.format(round(bl,4), 'A',':',round(energy,4), 'eV'))
filename = f'output/atoms_step{i:02d}.xyz'
atoms.write(filename)
Related
I want to use the control module of python for my transferfunctions. It works like a charm however i want to increase the size my bode plot that the module can produce. And see the log scale inside the plot.
This is the code i use in jupyter lab:
import matplotlib.pyplot as plt
import numpy as np
import time
plt.rcParams['font.size'] = 14
import os
import control
f = np.logspace(0,6,1000)
f.min()
f.max()
len(f)
w = 2*np.pi*f
# s = 1.0j*w
s = control.TransferFunction.s
R1=31600
R2=5230
R3=4420
R4=5110
C1=10e-9
C2=1.8e-9
C3=150e-12
num = ((s*C2*(R1+R3)+1))*(s*C1*R2+1)
den = (s*R1*C1)*(s*C2*R3+1)*(s*R2*C3+1)
Hs = control.tf((num/den))
print(Hs)
plt.figure
out = control.bode_plot(Hs,w,dB=1,Hz=1,deg=1,plot=1,margins=1)
It shows the following:
bodeplot
How can i have increase the size to make it better readable and how do i see de log lines????
enter image description here
Here is my code and I have given an image of my dataset "Market_Basket_Optimisation". I have made list of lists transaction to give the input in apriori algorithm.But I am not getting the rules. I am new to machine learning and I am not able to find out the error.
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Data Preprocessing
dataset = pd.read_csv('Market_Basket_Optimisation.csv', header = None)
transactions = []
for i in range(0, 7501):
transactions.append([str(dataset.values[i,j]) for j in range(0, 20)])
# Training Apriori on the dataset
from apyori import apriori
rules = apriori(transactions, min_support = 0.003, min_confidence = 0.2, min_lift = 3, min_length = 2)
# Visualising the results
results = list(rules)
It is not clear from your question if you are using jupyter notebook or an IDE such as Spyder. If you are using an IDE such as Spyder, you are not likely to see the result unless you use a print statement. I suggest adding another line as follows:
print(resuult)
You should see the rules list. This is the same issue I had and using the print statement solved the problem for me. You will still need to define a function to output the result in a tabular format that makes sense.
Im looking from smart ways to optimise this looped euclidean distance calculation. This calculation is looking for the mean distance from all other vectors.
As my vector arrays are really big to just do: eucl_dist = euclidean_distances(eigen_vs_cleaned)
Im running a loop row by row.
Typical eigen_vs_cleaned shape is at least (300000,1000) at the moment and I have to go up way more. (like 2000000,10000)
Any smarter way to do this?
eucl_dist_meaned = np.zeros(eigen_vs_cleaned.shape[0],dtype=float)
from sklearn.metrics.pairwise import euclidean_distances
for z in range(eigen_vs_cleaned.shape[0]):
if z%10000==0:
print(z)
eucl_dist_temp = euclidean_distances(eigen_vs_cleaned[z].reshape(1, -1), eigen_vs_cleaned)
eucl_dist_meaned[z] = eucl_dist_temp.mean(axis=1)
Im no python/numpy guru but this is the first step I took optimising this. it runs way better on my MacPro at least.
from joblib import Parallel, delayed
import multiprocessing
import os
import tempfile
import shutil
from sklearn.metrics.pairwise import euclidean_distances
# Creat a temporary directory and define the array pat
path = tempfile.mkdtemp()
out_path = os.path.join(path,'out.mmap')
out = np.memmap(out_path, dtype=float, shape=eigen_vs_cleaned.shape[0], mode='w+')
eucl_dist_meaned = np.zeros(eigen_vs_cleaned.shape[0],dtype=float)
num_cores = multiprocessing.cpu_count()
def runparallel(row, out):
if row%10000==0:
print(row)
eucl_dist_temp = euclidean_distances(eigen_vs_cleaned[row].reshape(1, -1), eigen_vs_cleaned)
out[row] = eucl_dist_temp.mean(axis=1)
##
nothing = Parallel(n_jobs=num_cores)(delayed(runparallel)(r, out) for r in range(eigen_vs_cleaned.shape[0]))
Then I save the output:
eucl_dist_meaned = np.array(out,copy=True,dtype=float)
I'm working with a Geiger counter which can be hooked up to a computer and which records its output in the form of a .txt file, NC.txt, where it records the time since starting and the 'value' of the radiation it recorded. It looks like
import pylab
import scipy.stats
import numpy as np
import matplotlib.pyplot as plt
x1 = []
y1 = []
#Define a dictionary: counts
f = open("NC.txt", "r")
for line in f:
line = line.strip()
parts = line.split(",") #the columns are separated by commas and spaces
time = float(parts[1]) #time is recorded in the second column of NC.txt
value = float(parts[2]) #and the value it records is in the third
x1.append(time)
y1.append(value)
f.close()
xv = np.array(x1)
yv = np.array(y1)
#Statistics
m = np.mean(yv)
d = np.std(yv)
#Strip out background radiation
trueval = yv - m
#Basic plot of counts
num_bins = 10000
plt.hist(trueval,num_bins)
plt.xlabel('Value')
plt.ylabel('Count')
plt.show()
So this code so far will just create a simple histogram of the radiation counts centred at zero, so the background radiation is ignored.
What I want to do now is perform a chi-squared test to see how well the data fits, say, Poisson statistics (and then go on to compare it with other distributions later). I'm not really sure how to do that. I have access to scipy and numpy, so I feel like this should be a simple task, but just learning python as I go here, so I'm not a terrific programmer.
Does anyone know of a straightforward way to do this?
Edit for clarity: I'm not asking so much about if there is a chi-squared function or not. I'm more interested in how to compare it with other statistical distributions.
Thanks in advance.
You can use SciPy library, here is documentation and examples.
I'm trying to save three sets of vector quantities corresponding to the same structured grid (velocity, turbulence intensity and standard deviation of velocity fluctuations). Ideally, I'd like them to be a part of the same vtk file but so far I have only been able to get one of them into the file like so:
sg = tvtk.StructuredGrid(dimensions=x.shape, points=pts)
sg.point_data.vectors = U
sg.point_data.vectors.name = 'U'
write_data(sg, 'vtktestWake.vtk')
I've spent past few hours searching for an example of how to add more then one vector or scalar field but failed and so thought I'd ask here. Any guidance will be most appreciated.
Thanks,
Artur
After some digging around I found the following solution based on this and this example. You have to add the additional data field using the add_array method see:
from tvtk.api import tvtk, write_data
import numpy as np
data = np.random.random((3,3,3))
data2 = np.random.random((3,3,3))
i = tvtk.ImageData(spacing=(1, 1, 1), origin=(0, 0, 0))
i.point_data.scalars = data.ravel()
i.point_data.scalars.name = 'scalars'
i.dimensions = data.shape
# add second point data field
i.point_data.add_array(data2.ravel())
i.point_data.get_array(1).name = 'field2'
i.point_data.update()
write_data(i, 'vtktest.vtk')