How to simplify my codes? - python

I wrote a program for a class using recursion to mimic certain kinds of simple branching structures like trees. I thought my code was great until I showed my professor. He told my code was too complicated and said I would need to simplify it. Besides spacing them out, I'm not sure what else I could do. Any tips? (I'm a beginner so go easy on me.) This program creates multiple trees with varying thickness, number of branch and at different coordinates.
import random
import turtle
##I'm using a python module called turtle to visualize results
p1 = turtle.Pen()
##Creates a pen
p1.tracer(True)
## Shows pen drawing
p1.up()
p1.left(90)
d=random.randint(0,2)
## Varying thickness of branch
length=150
##Length of branches
contract=random.uniform(.5,1)
## Varying degree of contraction
branch=random.randint(5,8)
## Varying amount of branches
first=random.randint(30,70)
## Varying first degree of branch
next=random.randint(1,30)
## Varying degree between each branches
number1=random.randint(10,20)
number2=random.randint(-100,100)
number3=random.randint(-100,100)
# Range of numbers used for coordinates
def drawFern1(pen, depth, length, contractBy, branches, firstBranchAngle, nextBranchAngle):
if depth > 0:
#Pen's Position and heading
heading = pen.heading()
position = pen.position()
pen.width(depth)
pen.forward(length)
pen.left(firstBranchAngle)
for i in range(branches):
drawFern1(pen, depth-1, contractBy*length, contractBy,branches,firstBranchAngle,nextBranchAngle)
pen.right(nextBranchAngle)
pen.setheading(heading)
pen.setposition(position)
# Ensures that multiple trees are created each at different coordinates.
for i in range(number1):
p1.sety(number2)
p1.setx(number3)
p1.down()
drawFern1(p1,d,length,contract,branch,first,next)
number2 = random.randint(-100,100)
number3 = random.randint(-100,100)
p1.up()

This code looks pretty solid to me, especially for a Python beginner. I've seen much worse.
If I were writing it, I think I'd calculate number2 and number3 only inside the main for loop - a priming definition as you have here is often convenient for a while loop, but not necessary in this case. I would also try to use more explanatory variable names, and depending on the problem statement I might require the randomly generated depth value to be at least 1 - if depth is generated as 0, nothing will be drawn.
My version of this would look like this:
import random
import turtle
def drawFern(pen, depth, length, contraction, branches, firstBranchAngle, nextBranchAngle):
if depth > 0:
# Pen's Position and heading
heading = pen.heading()
position = pen.position()
pen.width(depth)
pen.forward(length)
pen.left(firstBranchAngle)
for i in xrange(branches):
drawFern(pen, depth-1, contraction*length, contraction, branches, firstBranchAngle, nextBranchAngle)
pen.right(nextBranchAngle)
pen.setheading(heading)
pen.setposition(position)
# I'm using a python module called turtle to visualize results
# Creates a pen
pen = turtle.Pen()
# Shows pen drawing
pen.tracer(True)
pen.up()
pen.left(90)
# Configure initial state
# Varying depth of recursive fern
depth = random.randint(1,2)
# Length of branches
length = 150
# Varying degree of contraction
contraction = random.uniform(.5,1)
# Varying number of branches
branches = random.randint(5,8)
# Varying first degree of branch
first_angle = random.randint(30,70)
# Varying degree between each branches
next_angle = random.randint(1,30)
number_of_trees =random.randint(10,20)
for i in xrange(number_of_trees):
new_x = random.randint(-100, 100)
new_y = random.randint(-100, 100)
pen.setx(new_x)
pen.sety(new_y)
pen.down()
drawFern(pen, depth, length, contraction, branches, first_angle, next_angle)
pen.up()
In addition to moving the x and y coordinate randomization into the main loop, moving the recursive function definition earlier in the file, and using some more explicit variable names, I've used xrange calls instead of range calls - a trivial optimization if you're on Python 2.x. If you're on Python 3, range is correct. But these are minor changes.
You could also throw in an if clause before the range(branches) loop to not even try if depth equals 1 - that's another minor optimization, although not one that will make a big difference.

I thought my code was great until I showed my professor. He told my
code was too complicated and said I would need to simplify it.
This is rather complicated code given the quality of trees that it draws:
Drawing just vertical lines and blank screens are within the random parameters of the program as written! Let's rework the program to move some of the randomness from the static configuration code into the recursive routine itself. We'll also fine tune the random ranges a bit and clean up the code, primarily by eliminating variables that are only set and used once:
from random import randint, uniform
from turtle import Screen, Pen # Using python turtle module to visualize results
# Configure initial state
DEPTH = randint(3, 4) # Varying thickness and splitting of branches
LENGTH = randint(125, 150) # Length of branches
CONTRACT_BY = uniform(0.4, 0.8) # Varying degree of contraction
def drawFern(pen, depth, length, contractBy):
if depth < 1:
return
# Save pen's position and heading
heading = pen.heading()
position = pen.position()
pen.width(depth * 1.5) # pen thickness depends on branching
pen.forward(length)
pen.left(randint(30, 70)) # Varying first degree of branch)
for _ in range(randint(5, 8)): # Varying amount of branches
drawFern(pen, depth - 1, contractBy * length, contractBy)
pen.right(randint(5, 30)) # Varying degree between each branches
# Restore pen's Position and heading
pen.setheading(heading)
pen.setposition(position)
screen = Screen()
pen = Pen(visible=False)
pen.left(90)
screen.tracer(False)
# Ensure that multiple trees are created each at different coordinates.
for i in range(randint(10, 20)):
pen.penup()
pen.setposition(randint(-200, 200), randint(-300, 0))
pen.pendown()
drawFern(pen, DEPTH, LENGTH, CONTRACT_BY)
screen.tracer(True)
screen.mainloop()

Related

PySimpleGUI need help planning an intricate measurements calculator

i'm working on a program that should figure out the dimensions of individual pieces in kitchen cabinet modules, so you only set: height, depth, width of the material(18mm), and then you select modules from a list and after setting the dimensions of each you should be presented with a list of pieces and their dimensions.
Since all of this is somewhat standardized individual pieces's dimensions are figured out by simple math, but each consists of it's own set of operations, which should be ran once and display the results in the interface(eventually i'll figure out how to write it to an excel compatible format)
as you can see here it can get to be complex, i can work it out over time no problem, but right now i'm not sure PYGUI is what i need.
import PySimpleGUI as sg
layout1 = [[sg.Text('Altura', size=(10,1)),sg.Input('',key='Alt')], #Height
[sg.Text('Densidad Placa', size=(10,1)),sg.Input('',key='Placa')],# Material's density
[sg.Text('Profundidad', size=(10,1)),sg.Input('',key='Prof')]] #Depth
layout2 = [[sg.Text('Ancho Modulo', size=(10,1)),sg.Input('',key='WM')], #Module's width
[sg.Text('lateral', size=(10,1)),sg.Text('',key='Lat'),sg.Text('x'),sg.Text('',key='Prof2')], #side pieces
[sg.Text('Piso', size=(10,1)),sg.Text('',key='WM2'),sg.Text('x'),sg.Text('',key='Prof2')], # bottom piece
[sg.Button('Go')]]
#Define Layout with Tabs
tabgrp = [[sg.TabGroup([[sg.Tab('1', layout1),
sg.Tab('2', layout2)]])]]
window =sg.Window("Tabs",tabgrp)
#Read values entered by user
while True:
event,values=window.read()
if event in (sg.WINDOW_CLOSED, 'Close'):
break
elif event == 'Go':
anc = values['WM']
altura = values['Alt']
placa = values['Placa']
prof = values['Prof']
try:
v = int(anc) #width
w = int(prof)#depth
x = int(altura)#height
y = int (placa)# Material's density
altlat = str(x - y) #height of side pieces
prof2 = int(w - y) #depth of pieces,(total depth incluiding the door)
ancm = int(v) #width
except ValueError:
altlat = "error"
prof2 = "error"
ancm = "error"
window['Lat'].update(value=altlat)
window['Prof2'].update(value=prof2)
window['WM2'].update(value=ancm)
window.refresh
#access all the values and if selected add them to a string
window.close()
i figured i use functions for every set of operations and call them as i need them, but keys can't be reused and every tutorial i've seen points towards them, and other implementations i tried failed,. i've been using python since last night, so i'm not sure how many options i have, nor how limited my options will be with PYGUI's toolset.
I think what you are asking is, how can I make a function that will take the values and run an operation on them? This seems to be more of a general python question than one about pyGUI, but here is a quick answer.
def calc_side_panel_height(altura, placa):
x = int(altura)#height
y = int (placa)# Material's density
return (x - y) #height of side pieces
try {
height_of_side = calc_side_panel_height(altura, place)
# use height here.
altlat = str(height_of_side)
}
does that start to make sense? You would call functions and in those functions do the calculations, so you don't have to rewrite the code.
more info: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Functions
Let me know if I'm confused!

Moving the cursor in relation to a certain fix-point in Python

is there any possibility to move the cursor in Python with % starting from a certain coordinate like (1,1)?
I am using pyautogui atm to automate and I thought it would be quite convenient if this is independent from the monitor size making it universal.
Thanks for your help in advance!
It's possible indirectly. As detailed at the top of the Mouse Control Functions page of the documentation, you can get the screen size using the size() function (which returns a tuple of (X, Y)). You can then do the math to figure out how many screen pixels equal the percentage you're looking for, then call moveTo() to go there.
# script to move mouse 50% to the right and down
import pyautogui as pag
percentage = 0.5
cur_X, cur_Y = pag.position() # current X and Y coordinates
size_X, size_Y = pag.size() # screen size
goto_X = (size_X - cur_X) * percentage + cur_X # current location plus half
goto_Y = (size_Y - cur_Y) * percentage + cur_Y # the distance to the edge
pag.moveTo(goto_X, goto_Y, 1) # move to new position, taking 1 second

Distance measuring with turtle Python

I have written a programme that creates a random walk using certain distribution. For example, if it is a Cauchy distribution, the program generates an adequate random number and appends it to the list. Then this list is used by the turtle as a list of steps in a random direction. Now I would like to measure the distance between the turtle in each step and point (0,0) where the random walk is starting and I can't seem to find a good solution for that. It cannot be done with turtle.distance() because I want to draw a graph of dependence this distance on step count. My first idea was to get coordinates of each point that my turtle stops and then try to calculate distance using the Pythagoras theorem, but I make mistakes somewhere along the road and can't acquire them. Could someone explain why, please? Or maybe there is a better way to do this?
cauchylist = []
for i in randomlist:
cauchylist.append(0.15*math.tan(((i-0.5)*math.pi)) )
Franklin = turtle.Turtle()
u = input("Which walk to walk? C - Cauchy or G - Gauss.")
if u == "C":
start()
walk(Franklin, cauchylist)
cv = turtle.getcanvas()
cv.postscript(file="walk_cauchy.ps")
Franklin.penup()
Franklin.fd()
Franklin.end_poly()
coordinates = Franklin.get_poly()
turtle.done()
The distance of the current position of the turtle to the origin is calculated by squaring the posi-values, adding them and getting the square root from that:
from turtle import *
from math import sqrt
from random import seed, choice
seed(42) # ensure same randoms for demonstation purposes
def calc_dist(t):
return sqrt(sum(map(lambda x:x*x, t.pos())))
t = Turtle()
dists = []
for _ in range(20):
t.forward(choice(range(100))-50)
t.left(choice(range(360))-180)
dists.append(calc_dist(t))
print(dists)
Output:
[31.0, 68.97157492789178, 87.25113481192517, 98.48389701852805, 134.17622966161073,
141.40760908816227, 138.90181656585844, 128.76423765225354, 113.79561063931857,
108.70118700467432, 66.53516784607132, 87.40888728250773, 113.65616399911761,
115.22672747425487, 122.12225694530929, 128.35176588895283, 157.57222689310848,
128.33399580245668, 129.3846600939952, 153.87822281203412]
Do the calculation sqrt(sum(map(lambda x:x*x, t.pos()))) after each move of the turtle and store the result in a list.
get_poly() will return a tuple of tuples.
Example:
coordinates = turtle.get_poly()
coordinates = ((0.00,0.00), (100.00,0.00), (128.19,10.26), (136.87,59.50))
You can easily acess each tuple with a for loop
for c in coordinates:
print(c)
Output:
(0.00,0.00)
(100.00,0.00)
(128.19,10.26)
(136.87,59.50)
I would like to measure the distance between the turtle in each step
and point (0,0) where the random walk is starting and I can't seem to
find a good solution for that. It cannot be done with
turtle.distance()
This premise doesn't hold. We can do turtle.distance(0, 0) which doesn't affect the turtle's state but gives us the distance to the origin. To demonstrate, my rework of #PatrickArtner's example using distance():
from turtle import Screen, Turtle
from random import seed, randint
seed(42) # repeatable randomness for demonstration purposes
screen = Screen()
turtle = Turtle()
distances = []
for _ in range(20):
turtle.forward(randint(-50, 50))
turtle.left(randint(-180, 180))
distances.append(turtle.distance(0, 0))
print(distances)
screen.exitonclick()
Console Output
> python3 test.py
[31.0, 68.97157492789178, 87.25113481192517, 98.48389701852807, 134.17622966161073,
141.40760908816227, 138.90181656585844, 128.7642376522535, 113.79561063931855,
108.70118700467431, 66.5351678460713, 87.4088872825077, 113.65616399911758,
115.22672747425486, 122.12225694530927, 128.35176588895283, 157.57222689310848,
128.33399580245668, 129.3846600939952, 153.87822281203412]
>
I cannot use t.forward and t.left commands because I get an error
"invalid command name . canvas turtle". I think it's because my turtle
walk is defined by function looking like that:
def walk(turtle, steps):
x = 0
y = 0
for i in steps:
kat = random.random() * 360
turtle.setheading(kat)
turtle.forward(math.fabs(i))
bounds(turtle)
However, if I don't use it as on the example You gave, I create list
full of only last distance value. Can anything be done about that?
We'd need to see more code to address this issue, e.g. the definition of bounds() and so forth.

Pyglet. How to change picture(animate) for vertices dynamically. OpenGL

Environment:
Python: 3.6.6
pyglet version: 1.3.2
Code base:
abstract_model.py
import pyglet
def get_texture_group(file, order_group_index):
texture = pyglet.image.load(file).texture
order_group = pyglet.graphics.OrderedGroup(order_group_index)
return pyglet.graphics.TextureGroup(texture, order_group)
class AbstractModel(object):
def _create_as_vertex(self):
v_x = self.cell_data.get("x") * 32
v_y = self.cell_data.get("y") * -1 * 32
texture_group = self.map_type_iamge.get(self.cell_data.get("t"))
x_offset = self.x_offset * self.scale
x, y, z = v_x + x_offset, v_y, self.z
x_ = (texture_group.texture.width * self.scale + x_offset + v_x)
y_ = (texture_group.texture.height * self.scale + v_y)
tex_coords = ('t2f', (0, 0, 1, 0, 1, 1, 0, 1))
self.vertices = self.batch.add(
4, pyglet.gl.GL_QUADS,
texture_group,
('v3f', (x, y, z,
x_, y, z,
x_, y_, z,
x, y_, z)),
tex_coords)
def _animate(self, dt):
# lets assume that I have list of pyglet.graphics.TextureGroup
# and they should somehow be drawn one after other
print("I need change image. dt=", dt, self)
pyglet.clock.schedule_once(self._animate, 1)
ground3d.py
import os
import pyglet
import settings
from models import abstract_model
GROUND_DIR = os.path.join(settings.STATIC_DIR, "ground")
order_group_index = 0
map_type_iamge = {
1: abstract_model.get_texture_group(os.path.join(GROUND_DIR, "w1.png"), order_group_index),
2: abstract_model.get_texture_group(os.path.join(GROUND_DIR, "t1.png"), order_group_index),
1001: abstract_model.get_texture_group(os.path.join(GROUND_DIR, "t1_direction.png"), order_group_index),
}
class Ground3D(abstract_model.AbstractModel):
def __init__(self, cell_data, batch):
self.batch = batch
self.cell_data = cell_data
self.map_type_iamge = map_type_iamge
self.scale = 1
self.x_offset = 0
self.z = 0
self.entity = None
self._create_as_vertex()
pyglet.clock.schedule_once(self._animate, 1)
Explanation:
I have models(just flat rect for an example) which should be placed on 3 dimensions. And these models should be animated, like picture_1, after second picture_2, ... etc.
As I understood from my previous question using pyglet.sprite.Sprite() in 3D batch is not a good idea.
Question:
How I can change pictures(using TextureGroup or any other approaches) on self.vertices?
Or which arroach/classes I use use to implement it. I can't find any examples for such (as for my simple vision) usual case as animation for some flat models in 3 dimensions.
There are many example about rotating/moving/resizing of vertices, but how to build a correct question(is animation aspect) for getting answer in google - I don't know.
PS: If you, reader, have any usefull links on this subject(for pyglet or just for OpenGL) I would be very appreciated you share this link(s) in comment.
Texture coordinates.
You should have a single texture atlas for all frames of all different things that are animated ever.
Preferably, everything should have same animation speed and same amount of frames all the time.
Let's say there's two sprites that have 2 frames for entire animation, and they are stored in 64x64 texture atlas. (EDIT: sorry for ambiguity, 64x64 PIXELS, just because it could imply that we have 64x64 tile atlas, same everywhere else where I mention this)
Now, you need to have a global timer with global value which indicates current animation frame, not game frame. It should be independent of framerate.
Said value should be updated every once in a while at your desired speed like this:
current_frame = (current_frame + 1) % animation_length
Since we have 2 frames in this example, it will turn out like this:
# init
animation_length = 2
current_frame = 0
# updates:
current_frame = (0 + 1) % 2 # 1 % 2 -> 1
current_frame = (1 + 1) % 2 # 2 % 2 -> 0
...
Now, you need to update UV's of all your sprites only when the frame changes.
UV's start from left right and go from 0 to 1 (as far as I remember, for the sake of this example, they do, shhh).
Since we have 2 frames each, we can calculate "tiles" in the UV coordinates like this:
tile_width = 1.0 / frames # 2 frames each, width will be 0.5
tile_height = 1.0 / sprites # 2 sprites each, height will be 0.5 too, perfect
Now, on first frame, you generate your UV's like normal, you just take vertical ID or something, and use tile_height * sprite_id to get current V coordinate, and your U is calculated like tile_width * current_frame.
This assumes that you already have sprite batching so what you do is go over every sprite on update, and basically just recalculate new UV's with new frame, meaning all sprites change their frame to the next one, yay!
If you want to have systems that are independent of eachother, say, very slow animations for some, and faster for others, you'll need different sprite batches or proper calculation on from where to where you need to update UV's in vertex buffer array. Everything else is exactly the same, except now current_frame won't be global but rather contained, preferebly in some list or separate object that manages timers for animations.
You don't need to change anything in your shaders, they just need right UV's for the frames and you're set.
By the way, this is very basic, you could apply some logic yourself so you could instead have 16x16 grid of 32x32 pixels in your texture, each line of sprites having 4 different animations, these could be either sprite's states (attack, run, etc), but how you do it is entirely on you, most importantly, get it to work. Goodluck.
But if you do it the way I said, then state will be another integer, and UV for state, assuming all states have exactly the same width, it would be like this:
state_width = 1 / states
tile_width = 1 / (states * frames_per_state)
U = state_width * current_state + tile_width * current_frame
Now, one issue arises, player could start his animation at the last attack frame.
It's normal, entities with actions should all have individual timers, what I described above, is for tons of sprites that are just background, like grass. Now when you divided it up, you could have a proper way to reset current frame to 0 when new state is assigned.
If your entities are objects, you could write proper methods that recalculate UV's every time you rebuild the sprite batch using those sprites, and then timers itselves could be contained in objects.
We need to draw something? Check animation state, has it changed, no? Send UV's that were calculated before, otherwise, wait a bit, we need to recalculate, only then add those to VBO, and well, render your thing, in the end, it will appear as if you have animations, even though really, it's just a simple, but great UV manipulation.
Goodluck.

Self Avoiding Random Walk in a 3d lattice using pivot algorithm in python

I was working on a problem from past few days, It is related to creating a self avoiding random walks using pivot algorithm and then to implement another code which places spherical inclusions in a 3d lattice. I have written two codes one code generates coordinates of the spheres by using Self avoiding random walk algorithm and then another code uses the co-ordinates generated by the first program and then creates a 3d lattice in abaqus with spherical inclusions in it.
The result which will be generated after running the codes:
Now the zoomed in portion (marked in red boundary)
Problem: The spheres generated are merging with each other, How to avoid this, i ran out of ideas. I just need some direction or algorithm to work on.
The code which i wrote is as follows:
code 1: generates the coordinates of the spheres
# Functions of the code: 1) This code generates the coordinates for the spherical fillers using self avoiding random walk which was implemented by pivot algorithm
# Algorithm of the code: 1)Prepare an initial configuration of a N steps walk on lattice(equivalent N monomer chain)
# 2)Randomly pick a site along the chain as pivot site
# 3)Randomly pick a side(right to the pivot site or left to it), the chain on this side is used for the next step.
# 4)Randomly apply a rotate operation on the part of the chain we choose at the above step.
# 5)After the rotation, check the overlap between the rotated part of the chain and the rest part of the chain.
# 6)Accept the new configuration if there is no overlap and restart from 2th step.
# 7)Reject the configuration and repeat from 2th step if there are overlaps.
################################################################################################################################################################
# Modules to be imported are below
import numpy as np
import timeit
from scipy.spatial.distance import cdist
import math
import random
import sys
import ast
# define a dot product function used for the rotate operation
def v_dot(a):return lambda b: np.dot(a,b)
def distance(x, y): #The Euclidean Distance to Spread the spheres initiallly
if len(x) != len(y):
raise ValueError, "vectors must be same length"
sum = 0
for i in range(len(x)):
sum += (x[i]-y[i])**2
return math.sqrt(sum)
x=1/math.sqrt(2)
class lattice_SAW: # This is the class which creates the self avoiding random walk coordinates
def __init__(self,N,l0):
self.N = N #No of spheres
self.l0 = l0 #distance between spheres
# initial configuration. Usually we just use a straight chain as inital configuration
self.init_state = np.dstack((np.arange(N),np.zeros(N),np.zeros(N)))[0] #initially set all the coordinates to zeros
self.state = self.init_state.copy()
# define a rotation matrix
# 9 possible rotations: 3 axes * 3 possible rotate angles(45,90,135,180,225,270,315)
self.rotate_matrix = np.array([[[1,0,0],[0,0,-1],[0,1,0]],[[1,0,0],[0,-1,0],[0,0,-1]]
,[[1,0,0],[0,0,1],[0,-1,0]],[[0,0,1],[0,1,0],[-1,0,0]]
,[[-1,0,0],[0,1,0],[0,0,-1]],[[0,0,-1],[0,1,0],[-1,0,0]]
,[[0,-1,0],[1,0,0],[0,0,1]],[[-1,0,0],[0,-1,0],[0,0,1]]
,[[0,1,0],[-1,0,0],[0,0,1]],[[x,-x,0],[x,x,0],[0,0,1]]
,[[1,0,0],[0,x,-x],[0,x,x]]
,[[x,0,x],[0,1,0],[-x,0,x]],[[-x,-x,0],[x,-x,0],[0,0,1]]
,[[1,0,0],[0,-x,-x],[0,x,-x]],[[-x,0,x],[0,1,0],[-x,0,-x]]
,[[-x,x,0],[-x,-x,0],[0,0,1]],[[1,0,0],[0,-x,x],[0,-x,-x]]
,[[-x,0,-x],[0,1,0],[x,0,-x]],[[x,x,0],[-x,x,0],[0,0,1]]
,[[1,0,0],[0,x,x],[0,-x,x]],[[x,0,-x],[0,1,0],[x,0,x]]])
# define pivot algorithm process where t is the number of successful steps
def walk(self,t): #this definitions helps to start walking in 3d
acpt = 0
# while loop until the number of successful step up to t
while acpt <= t:
pick_pivot = np.random.randint(1,self.N-1) # pick a pivot site
pick_side = np.random.choice([-1,1]) # pick a side
if pick_side == 1:
old_chain = self.state[0:pick_pivot+1]
temp_chain = self.state[pick_pivot+1:]
else:
old_chain = self.state[pick_pivot:] # variable to store the coordinates of the old chain
temp_chain = self.state[0:pick_pivot]# for the temp chain
# pick a symmetry operator
symtry_oprtr = self.rotate_matrix[np.random.randint(len(self.rotate_matrix))]
# new chain after symmetry operator
new_chain = np.apply_along_axis(v_dot(symtry_oprtr),1,temp_chain - self.state[pick_pivot]) + self.state[pick_pivot]
# use cdist function of scipy package to calculate the pair-pair distance between old_chain and new_chain
overlap = cdist(new_chain,old_chain) #compare the old chain and the new chain to check if the new chain overlaps on any previous postion
overlap = overlap.flatten() # just to combine the coordinates in a list which will help to check the overlap
# determinte whether the new state is accepted or rejected
if len(np.nonzero(overlap)[0]) != len(overlap):
continue
else:
if pick_side == 1:
self.state = np.concatenate((old_chain,new_chain),axis=0)
elif pick_side == -1:
self.state = np.concatenate((new_chain,old_chain),axis=0)
acpt += 1
# place the center of mass of the chain on the origin, so then we can translate these coordinates to the initial spread spheres
self.state = self.l0*(self.state - np.int_(np.mean(self.state,axis=0)))
#now write the coordinates of the spheres in a file
sys.stdout = open("C:\Users\haris\Desktop\CAME_MINE\HIWI\Codes\Temp\Sphere_Positions.txt", "w")
n = 30
#text_file.write("Number of Spheres: " + str(n) + "\n")
#Radius of one sphere: is calculated based on the volume fraction here it 2 percent
r = (3*0.40)/(4*math.pi*n)
#print r
r = r**(1.0/3.0)
#print "Sphere Radius is " + str(r)
sphereList = [] # list to maintain track of the Spheres using their positions
sphereInstancesList = [] # to maintain track of the instances which will be used to cut and or translate the spheres
sphere_pos=[]
#Create n instances of the sphere
#After creating n instances of the sphere we trie to form cluster around each instance of the sphere
for i in range(1, n+1):
InstanceName = 'Sphere_' + str(i) # creating a name for the instance of the sphere
#print InstanceName
#text_file.write(InstanceName)
#Maximum tries to distribute sphere
maxTries = 10000000
while len(sphereList) < i:
maxTries -= 1
if maxTries < 1:
print "Maximum Distribution tries exceded. Error! Restart the Script!"
break;
# Make sure Spheres dont cut cube sides: this will place the spheres well inside the cube so that they does'nt touch the sides of the cube
# This is done to avoid the periodic boundary condition: later in the next versions it will be used
vecPosition = [(2*r)+(random.random()*(10.0-r-r-r)),(2*r)+(random.random()*(10.0-r-r-r)),(2*r)+(random.random()*(10.0-r-r-r))]
sphere_pos.append(vecPosition)
for pos in sphereList:
if distance(pos, vecPosition) < 2*r: # checking whether the spheres collide or not
break
else:
sphereList.append(vecPosition)
print vecPosition
#text_file.write(str(vecPosition) + "\n")
cluster_Size=[10,12,14,16,18,20,22,24,26,28,30] #list to give the random number of spheres which forms a cluster
for i in range(1,n+1):
Number_of_Spheres_Clustered=random.choice(cluster_Size) #selecting randomly from the list cluster_Size
radius_sphr=2*r #Distance between centers of the spheres
pivot_steps=1000 # for walking the max number of steps
chain = lattice_SAW(Number_of_Spheres_Clustered,radius_sphr) #initializing the object
chain.walk(pivot_steps) # calling the walk function to walk in the 3d space
co_ordinates=chain.state # copying the coordinates into a new variable for processing and converting the coordinates into lists
for c in range(len(co_ordinates)):
temp_cds=co_ordinates[c]
temp_cds=list(temp_cds)
for k in range(len(temp_cds)):
temp_cds[k]=temp_cds[k]+sphere_pos[i-1][k]
#text_file.write(str(temp_cds) + "\n")
print temp_cds #sys.stdout redirected into the file which stores the coordinates as lists
sys.stdout.flush()
f2=open("C:\Users\haris\Desktop\CAME_MINE\HIWI\Codes\Temp\Sphere_Positions.txt", "r")
remove_check=[]
for line in f2:
temp_check=ast.literal_eval(line)
if (temp_check[0]>10 or temp_check[0]<-r or temp_check[1]>10 or temp_check[1]<-r or temp_check[2]>10 or temp_check[2]<-r):
remove_check.append(str(temp_check))
f2.close()
flag=0
f2=open("C:\Users\haris\Desktop\CAME_MINE\HIWI\Codes\Temp\Sphere_Positions.txt", "r")
f3=open("C:\Users\haris\Desktop\CAME_MINE\HIWI\Codes\Temp\Sphere_Positions_corrected.txt", "w")
for line in f2:
line=line.strip()
if any(line in s for s in remove_check):
flag=flag+1
else:
f3.write(line+'\n')
f3.close()
f2.close()
The other code would not be required because there is no geometry computation in the second code. Any help or some direction of how to avoid the collision of spheres is very helpful, Thank you all
To accommodate non-intersecting spheres with turns of 45,135,225,315 (really, only 45 and 315 are issues), you just need to make your spheres a little bit smaller. Take 3 consecutive sphere-centers, with a 45-degree turn in the middle. In the plane containing the 3 points, that makes an isosceles triangle, with a 45-degree center angle:
Note that the bottom circles (spheres) overlap. To avoid this, you shrink the radius by multiplying by a factor of 0.76:

Categories