Skip to content

Commit

Permalink
try snes now
Browse files Browse the repository at this point in the history
  • Loading branch information
brucefan1983 committed Jan 19, 2025
1 parent 7f49f2c commit 4671ec2
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 172 deletions.
4 changes: 2 additions & 2 deletions tools/for_coding/tbmd/find_cost.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
L=[9.483921 9.483921 9.483921];
for n_pop = 1 : N_pop
para=population(n_pop,:);
[NN,NL]=find_neighbor(N_atom,L,[1 1 1],3.6,r);
[f_cal]=find_force(N_atom,3,NN,NL,L,[1 1 1],r,para);
[NN,NL]=find_neighbor(N_atom,L,3,r);
[energy,f_cal]=find_force(N_atom,NN,NL,L,r,para);
cost(n_pop) = sqrt(mean(mean((f_cal - f_ref).^2))) ...
+ 0.0e-5 * 0.5 * sum(para.^2) + 0.0e-5 * sum(abs(para));
end
4 changes: 2 additions & 2 deletions tools/for_coding/tbmd/find_force.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function [energy, force] = find_force(N, neighbor_number, neighbor_list, box, r)
function [energy, force] = find_force(N, neighbor_number, neighbor_list, box, r, para)
[energy_repulsive,force_repulsive]=find_force_repulsive(N, neighbor_number, neighbor_list, box, r);
[energy_band,force_band]=find_force_band(N, neighbor_number, neighbor_list, box, r);
[energy_band,force_band]=find_force_band(N, neighbor_number, neighbor_list, box, r, para);
energy=energy_repulsive+energy_band;
force=force_repulsive+force_band;
end
13 changes: 7 additions & 6 deletions tools/for_coding/tbmd/find_force_band.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
function [energy, force] = find_force_band(N, neighbor_number, neighbor_list, box, r)
function [energy, force] = find_force_band(N, neighbor_number, neighbor_list, box, r, para)
D=3;
N4 = N*4; N2 = N4/2; H0 = zeros(N4, N4); H = zeros(N4, N4);
energy = 0; force = zeros(N, D);
on_site_matrix = diag([-2.99, 3.71, 3.71, 3.71]);
v_sss = -5.0;
v_sps = 4.7;
v_pps = 5.5;
v_ppp = -1.55;
%on_site_matrix = diag([-2.99, 3.71, 3.71, 3.71]);
on_site_matrix = diag([para(1), para(2), para(2), para(2)]);
v_sss = para(3); %-5.0;
v_sps = para(4); %4.7;
v_pps = para(5); %5.5;
v_ppp = para(6); %-1.55;
for n1 = 1 : N
H(4*n1 - 3 : 4*n1, 4*n1 - 3 : 4*n1) = on_site_matrix;
for k = 1 : neighbor_number(n1)
Expand Down
153 changes: 0 additions & 153 deletions tools/for_coding/tbmd/find_force_para.m

This file was deleted.

2 changes: 1 addition & 1 deletion tools/for_coding/tbmd/my_snes.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function [best_fitness, elite] = my_snes(number_of_variables, maximal_generation)
population_size = 20;
population_size = 30;
best_fitness = ones(maximal_generation, 1);
elite = zeros(maximal_generation, number_of_variables);
mu = 10*rand(1, number_of_variables)-5; % initial mean
Expand Down
6 changes: 4 additions & 2 deletions tools/for_coding/tbmd/test_cohesive.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
energy_diamond=zeros(Ne,1);
energy_graphene=zeros(Ne,1);

para=[-2.99, 3.71, -5.0, 4.7, 5.5, -1.55];

tic;
for n=1:Ne
[N_diamond,L,r]=find_diamond(2,bond_diamond(n));
[NN,NL]=find_neighbor(N_diamond,L,3,r);
[energy_diamond(n)]=find_force(N_diamond,NN,NL,L,r);
[energy_diamond(n)]=find_force(N_diamond,NN,NL,L,r,para);
[N_graphene,L,r]=find_graphene(bond_graphene(n));
[NN,NL]=find_neighbor(N_graphene,L,3,r);
[energy_graphene(n)]=find_force(N_graphene,NN,NL,L,r);
[energy_graphene(n)]=find_force(N_graphene,NN,NL,L,r,para);
end
toc

Expand Down
8 changes: 5 additions & 3 deletions tools/for_coding/tbmd/test_force.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
clear; close all;

para=[-2.99, 3.71, -5.0, 4.7, 5.5, -1.55];

[N,L,r]=find_graphene(1.42);
r=r+rand(N,3)*0.1;
[NN,NL]=find_neighbor(N,L,3,r);
tic
[energy,f_analytical]=find_force(N,NN,NL,L,r);
[energy,f_analytical]=find_force(N,NN,NL,L,r,para);
toc

f_finite=zeros(N,3);
Expand All @@ -14,8 +16,8 @@
for d=1:3
rpx=r;rpx(n,d)=rpx(n,d)+delta;
rmx=r;rmx(n,d)=rmx(n,d)-delta;
[ep]=find_force(N,NN,NL,L,rpx);
[em]=find_force(N,NN,NL,L,rmx);
[ep]=find_force(N,NN,NL,L,rpx,para);
[em]=find_force(N,NN,NL,L,rmx,para);
f_finite(n,d)=(em-ep)/(2*delta);
end
toc
Expand Down
4 changes: 3 additions & 1 deletion tools/for_coding/tbmd/test_size_effects.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
energy_diamond=zeros(Ne,1);
N_diamond=zeros(Ne,1);

para=[-2.99, 3.71, -5.0, 4.7, 5.5, -1.55];

tic;
for n=1:Ne
[N_diamond(n),L,r]=find_diamond(n,1.55);
[NN,NL]=find_neighbor(N_diamond(n),L,3,r);
[energy_diamond(n)]=find_force(N_diamond(n),NN,NL,L,r);
[energy_diamond(n)]=find_force(N_diamond(n),NN,NL,L,r,para);
end
toc

Expand Down
4 changes: 2 additions & 2 deletions tools/for_coding/tbmd/test_snes.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
clear; close all;

% Call my_snes to evolve
dim = 7;
[best_fitness, elite] = my_snes(dim, 1000);
dim = 6;
[best_fitness, elite] = my_snes(dim, 100);
num_generations = length(best_fitness);

% Evolution of the best fitness:
Expand Down

0 comments on commit 4671ec2

Please sign in to comment.