Skip to content

Commit

Permalink
style: utils package refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
caefleury committed Mar 13, 2024
1 parent 153be95 commit 2d4f733
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 249 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
102 changes: 0 additions & 102 deletions src/scripts/n1_nanocrack.py

This file was deleted.

91 changes: 91 additions & 0 deletions src/scripts/n2_nanocrack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

from utils import read_xyz, write_xyz
import sys
sys.path.append('./src/utils')

# Replicar a célula unitária com os nanocracks lineares (n2)


def replicate_cell(atoms, lattice_constants, n_replications_x, n_replications_y, crack_size):
replicated_atoms = []
for i in range(n_replications_x):
for j in range(n_replications_y):
for index, (atom, position) in enumerate(atoms):
# POSICOES PERMANECEM CONSTANTES
new_position = [
position[0] + (lattice_constants[0]) * i, position[1] + (lattice_constants[1])*j, 0.0]

if i == (n_replications_x // 2 - 1): # meio - 1 (lateral left) (6)
floor = int(n_replications_y//2 -
((crack_size - 1)/2) + 1) # 5
ceiling = int(n_replications_y//2 +
((crack_size - 1)/2) - 1) # 9
if j in range(floor, ceiling+1):
atom = 'H'
if (j == floor and index in [0, 1, 2, 8, 9]) or (j == ceiling and index in [5, 6, 7, 8, 9]):
replicated_atoms.append((atom, new_position))
else:
if index in [8, 9]:
replicated_atoms.append((atom, new_position))
else:
replicated_atoms.append((atom, new_position))

elif i == (n_replications_x // 2): # meio (7)
floor = int(n_replications_y//2 -
((crack_size - 1)/2)) # 4
ceiling = int(n_replications_y//2 +
((crack_size - 1)/2)) # 10
if j in range(floor, ceiling+1):
atom = 'H'
if (j == floor and index in [0, 1, 2, 3, 4, 8, 9]) or (j == ceiling and index in [3, 4, 5, 6, 7, 8, 9]):
replicated_atoms.append((atom, new_position))
else:
replicated_atoms.append((atom, new_position))

elif i == (n_replications_x // 2 + 1): # meio + 1 (lateral right) (8)
floor = int(n_replications_y//2 -
((crack_size - 1)/2) + 1) # 5
ceiling = int(n_replications_y//2 +
((crack_size - 1)/2) - 1) # 9
if j in range(floor, ceiling+1):
atom = 'H'
if (j == floor and index in [0, 1, 2, 3, 4]) or (j == ceiling and index in [3, 4, 5, 6, 7]):
replicated_atoms.append((atom, new_position))
else:
if index in [3, 4]:
replicated_atoms.append((atom, new_position))
else:
replicated_atoms.append((atom, new_position))
else:
replicated_atoms.append((atom, new_position))
return [len(replicated_atoms), replicated_atoms]


# Vetores de rede (lattice constants)
a = 6.3028
b = 4.9302
lattice_constants = [a, b]

# Parâmetros
INPUT_UNIT_CELL_FILE = 'src/simulations/unit_cell.xyz'
OUTPUT_STRUCTURE_FILE = 'src/simulations/n1_nanocrack_structure.xyz'
n_replications_x = 17
n_replications_y = 17
crack_size = 7

OUTPUT_STRUCTURE_FILE = 'src/simulations/n1_nanocrack_structure.xyz'

# Ler a célula unitária
n_atoms, comment, atoms = read_xyz(INPUT_UNIT_CELL_FILE)

# Replicar a célula unitária
replicated_atoms = replicate_cell(
atoms, lattice_constants, n_replications_x, n_replications_y, crack_size)

n_atoms_modified = replicated_atoms[0]
atoms_modified = replicated_atoms[1]

# Escrever o arquivo .xyz com a estrutura replicada
write_xyz(OUTPUT_STRUCTURE_FILE, n_atoms_modified, comment, atoms_modified)

print('Estrutura replicada, arquivo salvo em {}'.format(OUTPUT_STRUCTURE_FILE))
25 changes: 5 additions & 20 deletions src/scripts/replicate_cell.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@

def read_xyz(file):
with open(file, 'r') as f:
n_atoms = int(f.readline())
comment = f.readline()
atoms = []
for line in f:
atom, x, y, z = line.split()
atoms.append((atom, [float(x), float(y), float(z)]))
return n_atoms, comment, atoms
from utils import read_xyz, write_xyz
import sys
sys.path.append('./src/utils')

# Replicar a célula unitária


def replicate_cell(atoms, lattice_constants, n_replications_x, n_replications_y):
Expand All @@ -21,17 +17,6 @@ def replicate_cell(atoms, lattice_constants, n_replications_x, n_replications_y)
return replicated_atoms


def write_xyz(file, n_atoms, comment, atoms):
with open(file, 'w') as f:
f.write(str(n_atoms) + '\n')
f.write(comment)
for atom, position in atoms:
x = format(position[0], '.16f')
y = format(position[1], '.16f')
z = format(position[2], '.16f')
f.write('{} {} {} {}\n'.format(atom, x, y, z))


# Vetores de rede (lattice constants)
a = 6.3028
b = 4.9302
Expand Down
Empty file added src/utils/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions src/utils/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def read_xyz(file):
with open(file, 'r') as f:
n_atoms = int(f.readline())
comment = f.readline()
atoms = []
for line in f:
atom, x, y, z = line.split()
atoms.append((atom, [float(x), float(y), float(z)]))
return n_atoms, comment, atoms


def write_xyz(file, n_atoms, comment, atoms):
with open(file, 'w') as f:
f.write(str(n_atoms) + '\n')
f.write(comment)
for atom, position in atoms:
x = format(position[0], '.16f')
y = format(position[1], '.16f')
z = format(position[2], '.16f')
f.write('{} {} {} {}\n'.format(atom, x, y, z))
63 changes: 0 additions & 63 deletions tests/index.py

This file was deleted.

40 changes: 0 additions & 40 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -1,40 +0,0 @@
import math

hip = 1.42
cateto = math.sqrt((hip**2) / 2) # 1.0023

a = 6.3028
b = 4.9302
x = (a - ((2*cateto) + (2*hip))) / 2 # 0.7238
y = (b - (hip + (2*cateto))) / 2 # 0.75

x1 = [x+cateto, y, 0.0]
x2 = [x+cateto+hip, y, 0.0]
x3 = [x+cateto+(2*hip), y, 0.0]

x4 = [x+(2*cateto)+(2*hip), y+cateto, 0.0]
x5 = [x+(2*cateto)+(2*hip), y+cateto+hip, 0.0]

x6 = [x+cateto+(2*hip), y+(2*cateto)+hip, 0.0]
x7 = [x+cateto+hip, y+(2*cateto)+hip, 0.0]
x8 = [x+cateto, y+(2*cateto)+hip, 0.0]

x9 = [x, y+cateto+hip, 0.0]
x10 = [x, y+cateto, 0.0]


def padraoAtomo(atomo):
x, y, z = atomo
x = format(x, '.16f')
y = format(y, '.16f')
z = format(z, '.16f')
return f"C {x} {y} {z}"


lista = [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10]
listaPadrao = [padraoAtomo(atomo) for atomo in lista]

with open('tests/test.xyz', 'w') as file:
file.write(str(len(listaPadrao)) + '\n')
for atomo in listaPadrao:
file.write(atomo + '\n')
12 changes: 0 additions & 12 deletions tests/test.xyz

This file was deleted.

12 changes: 0 additions & 12 deletions tests/test2.xyz

This file was deleted.

0 comments on commit 2d4f733

Please sign in to comment.