<jupyter_start><jupyter_text>Simple Domain formation illustration- This notebook shows that all you need for domain formation is an energy penalty proportional to stray field and a favourable energy for alginment    - Start with initally 'random' polarisations    - Align moments with application of E field    - See what structure the moments relax to when the field is removed    Linke to Google Colab: https://colab.research.google.com/drive/1gtZINYXnT4gf0IygCJTA64T-8GVmKv7X?usp=sharing  Import<jupyter_code>import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import minimize
from scipy.optimize import Bounds<jupyter_output><empty_output><jupyter_text>Create initially random dipoles- Dipole can either point up (red) or down (blue) in this simple model)<jupyter_code># Initialise in random state
P = np.random.randint(0,2,(60,60))*2-1
# Add vacuum
P = np.pad(P,(40,40))

# Plot
plt.imshow(P,cmap='RdBu_r',vmax=1,vmin=-1)
plt.colorbar()
plt.title('Initial polarisation')<jupyter_output><empty_output><jupyter_text>Align with an external field<jupyter_code># Apply a field - either up or down
E = 1
particle = np.where(P!=0)
P[particle] = E

# Plot
plt.imshow(P,cmap='RdBu_r',vmax=1,vmin=-1)
plt.colorbar()
plt.title('Polarisation after applying field of %i' %E)<jupyter_output><empty_output><jupyter_text>Define the total energy depending on stray penalty / favourable alignment- In this very simple model, we make many approximations:    - Dipoles can only point up or down    - Each column contributes +1 stray field if pointing up, or -1 stray field if pointing down    - The total stray field is the sum across all columns in the material    - If neighbouring columns both point in the same direction, they get +1 favourable energy<jupyter_code>def total_energy(vertical_directions,stray_energy=1,alignment_energy=1):   
    # Penalty = Sum all columns (+1 up, -1 down)
    total_penalty = np.sum(vertical_directions*stray_energy)
    
    # Favourable = Sum number of aligned columns
    total_alignment = 0
    for i, d in enumerate(vertical_directions[:-1]):
        if d == vertical_directions[i+1] and d !=0:
            total_alignment +=alignment_energy
            
    # Net energy
    total_energy = total_penalty - total_alignment
    
    return total_energy
vertical_directions = np.mean(P,axis=0)[40:-40]
total_energy(vertical_directions,stray_energy=1,alignment_energy=1)<jupyter_output><empty_output><jupyter_text>Define a function which relaxes the structure to minimum total energy- Physical systems always minimise their energy- We can use a numerical solver to do this for us- See what happens if we change the penalty/favourable variables<jupyter_code>def relax_structure(P,stray_energy=1,alignment_energy=1):
    # Look at column directions
    vertical_directions = np.mean(P,axis=0)[40:-40]
    
    # Minimise energy using our total_energy calculator
    bounds = Bounds(1,-1)
    res = minimize(total_energy, vertical_directions, args=(stray_energy,alignment_energy),method='trust-constr',bounds=bounds)
    
    # Redefine shape using minimised structure
    P_min = res.x*np.ones((60,60))
    P_min = np.pad(P_min,(40,40))
    P_min[np.where(P_min>0)] = 1
    P_min[np.where(P_min<0)] = -1
    
    return P_min<jupyter_output><empty_output><jupyter_text>Now let's see what happens<jupyter_code>import warnings
warnings.filterwarnings("ignore")
stray_energy=3
alignment_energy=1

P_min = relax_structure(P,stray_energy=stray_energy,alignment_energy=alignment_energy)

# plot
plt.imshow(P_min,cmap='RdBu_r',vmax=1,vmin=-1)
plt.colorbar()
Etot = total_energy(vertical_directions,stray_energy=stray_energy,alignment_energy=alignment_energy)
plt.title('Minimised - Etot = %.2f' % Etot)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0930540827147402, 'fraction_numerical': 0.018292682926829267, 'mean_word_length': 5.236363636363636, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '302335', 'license': 'MIT', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1216, 'n_words': 332, 'path': 'Jupyter_notebooks/Domain formation.ipynb', 'repo_name': 'Gio-A-Oakes/Materials_IA', 'size': '3772'}