pub struct Graph { /* private fields */ }Implementations§
Source§impl Graph
impl Graph
pub fn new(dim: usize) -> Self
Sourcepub fn simple(dim: usize, length: usize) -> Self
pub fn simple(dim: usize, length: usize) -> Self
Examples found in repository?
More examples
examples/ising.rs (line 63)
56fn main() {
57 println!("Metropolis Algorithm for Classical Ferromagnetic Ising Model");
58
59 let beta = 1.0 / 2.269;
60
61 let dim = 2usize;
62 let length = 32usize;
63 let graph = Graph::simple(dim, length);
64
65 let mut rng = Lcg::new(12345);
66 let mut spins = vec![1.0f64; graph.num_sites()];
67
68 let mut energy = Observable::default();
69 let mut mag2 = Observable::default();
70
71 let sweeps = 65536usize;
72 let therm = sweeps / 8;
73
74 for mcs in 0..(therm + sweeps) {
75 for site in 0..graph.num_sites() {
76 let mut diff = 0.0;
77 for k in 0..graph.num_neighbors(site) {
78 diff += 2.0 * spins[site] * spins[graph.neighbor(site, k)];
79 }
80 if rng.next_f64() < (-beta * diff).exp() {
81 spins[site] = -spins[site];
82 }
83 }
84
85 if mcs > therm {
86 let mut ene = 0.0;
87 for bond in 0..graph.num_bonds() {
88 ene -= spins[graph.source(bond)] * spins[graph.target(bond)];
89 }
90 energy.add(ene / graph.num_sites() as f64);
91
92 let mag = spins.iter().sum::<f64>() / graph.num_sites() as f64;
93 mag2.add(mag * mag);
94 }
95 }
96
97 println!("dimension = {dim}");
98 println!("length = {length}");
99 println!("beta = {beta}");
100 println!(
101 "energy density = {} +/- {}",
102 energy.mean(),
103 energy.error()
104 );
105 println!(
106 "magnetization density^2 = {} +/- {}",
107 mag2.mean(),
108 mag2.error()
109 );
110}pub fn fully_connected(num_sites: usize) -> Self
Sourcepub fn from_basis_unitcell_extent(
basis: &Basis,
cell: &Unitcell,
extent: &ExtentVector,
boundary: &[Boundary],
) -> Self
pub fn from_basis_unitcell_extent( basis: &Basis, cell: &Unitcell, extent: &ExtentVector, boundary: &[Boundary], ) -> Self
Examples found in repository?
examples/construct2.rs (line 15)
6fn main() {
7 let basis = Basis::new(BasisMatrix::from_row_slice(1, 1, &[1.0]));
8
9 let mut unitcell = Unitcell::new(1);
10 unitcell.add_site(CoordinateVector::from_element(1, 0.0), 0);
11 unitcell.add_bond(0, 0, OffsetVector::from_element(1, 1), 0);
12
13 let extent = ExtentVector::from_element(1, 16);
14 let boundary = vec![Boundary::Periodic; 1];
15 let graph = Graph::from_basis_unitcell_extent(&basis, &unitcell, &extent, &boundary);
16 support::print_graph(&graph);
17}More examples
examples/construct4.rs (line 16)
6fn main() {
7 let basis = Basis::new(BasisMatrix::from_row_slice(2, 2, &[1.0, 0.0, 0.0, 1.0]));
8
9 let mut unitcell = Unitcell::new(2);
10 unitcell.add_site(CoordinateVector::from_vec(vec![0.0, 0.0]), 0);
11 unitcell.add_bond(0, 0, OffsetVector::from_vec(vec![1, 0]), 0);
12 unitcell.add_bond(0, 0, OffsetVector::from_vec(vec![0, 1]), 0);
13
14 let extent = ExtentVector::from_vec(vec![4, 4]);
15 let boundary = vec![Boundary::Periodic; 2];
16 let graph = Graph::from_basis_unitcell_extent(&basis, &unitcell, &extent, &boundary);
17 support::print_graph(&graph);
18}examples/construct_xml.rs (line 46)
6fn main() {
7 let args = std::env::args().collect::<Vec<_>>();
8
9 let mut file = support::resolve_lattices_xml().unwrap_or_else(|message| {
10 eprintln!("Error: {message}");
11 std::process::exit(127);
12 });
13 let mut basis_name = "square lattice".to_string();
14 let mut cell_name = "simple2d".to_string();
15 let mut length: usize = 4;
16
17 if args.len() > 1 {
18 if args.len() == 4 || args.len() == 5 {
19 file = std::path::PathBuf::from(&args[1]);
20 basis_name = args[2].clone();
21 cell_name = args[3].clone();
22 if args.len() == 5 {
23 length = args[4].parse::<usize>().unwrap_or_else(|_| {
24 eprintln!("Error: invalid length: {}", args[4]);
25 std::process::exit(127);
26 });
27 }
28 } else {
29 eprintln!("Error: {} xmlfile basis cell [length]", args[0]);
30 std::process::exit(127);
31 }
32 }
33
34 let basis = read_basis_from_file(&file, &basis_name).unwrap_or_else(|error| {
35 eprintln!("Failed to read basis XML entry '{basis_name}': {error}");
36 std::process::exit(127);
37 });
38
39 let cell = read_unitcell_from_file(&file, &cell_name).unwrap_or_else(|error| {
40 eprintln!("Failed to read unitcell XML entry '{cell_name}': {error}");
41 std::process::exit(127);
42 });
43
44 let extent = ExtentVector::from_element(cell.dimension(), length as i64);
45 let boundary = vec![Boundary::Periodic; cell.dimension()];
46 let graph = Graph::from_basis_unitcell_extent(&basis, &cell, &extent, &boundary);
47
48 support::print_graph(&graph);
49}pub fn from_basis_unitcell_length( basis: &Basis, cell: &Unitcell, length: usize, boundary: Boundary, ) -> Self
Sourcepub fn dimension(&self) -> usize
pub fn dimension(&self) -> usize
Examples found in repository?
examples/support/mod.rs (line 5)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}Sourcepub fn num_sites(&self) -> usize
pub fn num_sites(&self) -> usize
Examples found in repository?
examples/support/mod.rs (line 6)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}More examples
examples/ising.rs (line 66)
56fn main() {
57 println!("Metropolis Algorithm for Classical Ferromagnetic Ising Model");
58
59 let beta = 1.0 / 2.269;
60
61 let dim = 2usize;
62 let length = 32usize;
63 let graph = Graph::simple(dim, length);
64
65 let mut rng = Lcg::new(12345);
66 let mut spins = vec![1.0f64; graph.num_sites()];
67
68 let mut energy = Observable::default();
69 let mut mag2 = Observable::default();
70
71 let sweeps = 65536usize;
72 let therm = sweeps / 8;
73
74 for mcs in 0..(therm + sweeps) {
75 for site in 0..graph.num_sites() {
76 let mut diff = 0.0;
77 for k in 0..graph.num_neighbors(site) {
78 diff += 2.0 * spins[site] * spins[graph.neighbor(site, k)];
79 }
80 if rng.next_f64() < (-beta * diff).exp() {
81 spins[site] = -spins[site];
82 }
83 }
84
85 if mcs > therm {
86 let mut ene = 0.0;
87 for bond in 0..graph.num_bonds() {
88 ene -= spins[graph.source(bond)] * spins[graph.target(bond)];
89 }
90 energy.add(ene / graph.num_sites() as f64);
91
92 let mag = spins.iter().sum::<f64>() / graph.num_sites() as f64;
93 mag2.add(mag * mag);
94 }
95 }
96
97 println!("dimension = {dim}");
98 println!("length = {length}");
99 println!("beta = {beta}");
100 println!(
101 "energy density = {} +/- {}",
102 energy.mean(),
103 energy.error()
104 );
105 println!(
106 "magnetization density^2 = {} +/- {}",
107 mag2.mean(),
108 mag2.error()
109 );
110}Sourcepub fn site_type(&self, site: usize) -> i32
pub fn site_type(&self, site: usize) -> i32
Examples found in repository?
examples/support/mod.rs (line 29)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}Sourcepub fn coordinate(&self, site: usize) -> &CoordinateVector
pub fn coordinate(&self, site: usize) -> &CoordinateVector
Examples found in repository?
examples/support/mod.rs (line 10)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}Sourcepub fn num_neighbors(&self, site: usize) -> usize
pub fn num_neighbors(&self, site: usize) -> usize
Examples found in repository?
examples/support/mod.rs (line 22)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}More examples
examples/ising.rs (line 77)
56fn main() {
57 println!("Metropolis Algorithm for Classical Ferromagnetic Ising Model");
58
59 let beta = 1.0 / 2.269;
60
61 let dim = 2usize;
62 let length = 32usize;
63 let graph = Graph::simple(dim, length);
64
65 let mut rng = Lcg::new(12345);
66 let mut spins = vec![1.0f64; graph.num_sites()];
67
68 let mut energy = Observable::default();
69 let mut mag2 = Observable::default();
70
71 let sweeps = 65536usize;
72 let therm = sweeps / 8;
73
74 for mcs in 0..(therm + sweeps) {
75 for site in 0..graph.num_sites() {
76 let mut diff = 0.0;
77 for k in 0..graph.num_neighbors(site) {
78 diff += 2.0 * spins[site] * spins[graph.neighbor(site, k)];
79 }
80 if rng.next_f64() < (-beta * diff).exp() {
81 spins[site] = -spins[site];
82 }
83 }
84
85 if mcs > therm {
86 let mut ene = 0.0;
87 for bond in 0..graph.num_bonds() {
88 ene -= spins[graph.source(bond)] * spins[graph.target(bond)];
89 }
90 energy.add(ene / graph.num_sites() as f64);
91
92 let mag = spins.iter().sum::<f64>() / graph.num_sites() as f64;
93 mag2.add(mag * mag);
94 }
95 }
96
97 println!("dimension = {dim}");
98 println!("length = {length}");
99 println!("beta = {beta}");
100 println!(
101 "energy density = {} +/- {}",
102 energy.mean(),
103 energy.error()
104 );
105 println!(
106 "magnetization density^2 = {} +/- {}",
107 mag2.mean(),
108 mag2.error()
109 );
110}Sourcepub fn neighbor(&self, site: usize, neighbor: usize) -> usize
pub fn neighbor(&self, site: usize, neighbor: usize) -> usize
Examples found in repository?
examples/support/mod.rs (line 23)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}More examples
examples/ising.rs (line 78)
56fn main() {
57 println!("Metropolis Algorithm for Classical Ferromagnetic Ising Model");
58
59 let beta = 1.0 / 2.269;
60
61 let dim = 2usize;
62 let length = 32usize;
63 let graph = Graph::simple(dim, length);
64
65 let mut rng = Lcg::new(12345);
66 let mut spins = vec![1.0f64; graph.num_sites()];
67
68 let mut energy = Observable::default();
69 let mut mag2 = Observable::default();
70
71 let sweeps = 65536usize;
72 let therm = sweeps / 8;
73
74 for mcs in 0..(therm + sweeps) {
75 for site in 0..graph.num_sites() {
76 let mut diff = 0.0;
77 for k in 0..graph.num_neighbors(site) {
78 diff += 2.0 * spins[site] * spins[graph.neighbor(site, k)];
79 }
80 if rng.next_f64() < (-beta * diff).exp() {
81 spins[site] = -spins[site];
82 }
83 }
84
85 if mcs > therm {
86 let mut ene = 0.0;
87 for bond in 0..graph.num_bonds() {
88 ene -= spins[graph.source(bond)] * spins[graph.target(bond)];
89 }
90 energy.add(ene / graph.num_sites() as f64);
91
92 let mag = spins.iter().sum::<f64>() / graph.num_sites() as f64;
93 mag2.add(mag * mag);
94 }
95 }
96
97 println!("dimension = {dim}");
98 println!("length = {length}");
99 println!("beta = {beta}");
100 println!(
101 "energy density = {} +/- {}",
102 energy.mean(),
103 energy.error()
104 );
105 println!(
106 "magnetization density^2 = {} +/- {}",
107 mag2.mean(),
108 mag2.error()
109 );
110}pub fn neighbor_bond(&self, site: usize, neighbor: usize) -> usize
Sourcepub fn num_bonds(&self) -> usize
pub fn num_bonds(&self) -> usize
Examples found in repository?
examples/support/mod.rs (line 7)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}More examples
examples/ising.rs (line 87)
56fn main() {
57 println!("Metropolis Algorithm for Classical Ferromagnetic Ising Model");
58
59 let beta = 1.0 / 2.269;
60
61 let dim = 2usize;
62 let length = 32usize;
63 let graph = Graph::simple(dim, length);
64
65 let mut rng = Lcg::new(12345);
66 let mut spins = vec![1.0f64; graph.num_sites()];
67
68 let mut energy = Observable::default();
69 let mut mag2 = Observable::default();
70
71 let sweeps = 65536usize;
72 let therm = sweeps / 8;
73
74 for mcs in 0..(therm + sweeps) {
75 for site in 0..graph.num_sites() {
76 let mut diff = 0.0;
77 for k in 0..graph.num_neighbors(site) {
78 diff += 2.0 * spins[site] * spins[graph.neighbor(site, k)];
79 }
80 if rng.next_f64() < (-beta * diff).exp() {
81 spins[site] = -spins[site];
82 }
83 }
84
85 if mcs > therm {
86 let mut ene = 0.0;
87 for bond in 0..graph.num_bonds() {
88 ene -= spins[graph.source(bond)] * spins[graph.target(bond)];
89 }
90 energy.add(ene / graph.num_sites() as f64);
91
92 let mag = spins.iter().sum::<f64>() / graph.num_sites() as f64;
93 mag2.add(mag * mag);
94 }
95 }
96
97 println!("dimension = {dim}");
98 println!("length = {length}");
99 println!("beta = {beta}");
100 println!(
101 "energy density = {} +/- {}",
102 energy.mean(),
103 energy.error()
104 );
105 println!(
106 "magnetization density^2 = {} +/- {}",
107 mag2.mean(),
108 mag2.error()
109 );
110}Sourcepub fn bond_type(&self, bond: usize) -> i32
pub fn bond_type(&self, bond: usize) -> i32
Examples found in repository?
examples/support/mod.rs (line 40)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}Sourcepub fn source(&self, bond: usize) -> usize
pub fn source(&self, bond: usize) -> usize
Examples found in repository?
examples/support/mod.rs (line 38)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}More examples
examples/ising.rs (line 88)
56fn main() {
57 println!("Metropolis Algorithm for Classical Ferromagnetic Ising Model");
58
59 let beta = 1.0 / 2.269;
60
61 let dim = 2usize;
62 let length = 32usize;
63 let graph = Graph::simple(dim, length);
64
65 let mut rng = Lcg::new(12345);
66 let mut spins = vec![1.0f64; graph.num_sites()];
67
68 let mut energy = Observable::default();
69 let mut mag2 = Observable::default();
70
71 let sweeps = 65536usize;
72 let therm = sweeps / 8;
73
74 for mcs in 0..(therm + sweeps) {
75 for site in 0..graph.num_sites() {
76 let mut diff = 0.0;
77 for k in 0..graph.num_neighbors(site) {
78 diff += 2.0 * spins[site] * spins[graph.neighbor(site, k)];
79 }
80 if rng.next_f64() < (-beta * diff).exp() {
81 spins[site] = -spins[site];
82 }
83 }
84
85 if mcs > therm {
86 let mut ene = 0.0;
87 for bond in 0..graph.num_bonds() {
88 ene -= spins[graph.source(bond)] * spins[graph.target(bond)];
89 }
90 energy.add(ene / graph.num_sites() as f64);
91
92 let mag = spins.iter().sum::<f64>() / graph.num_sites() as f64;
93 mag2.add(mag * mag);
94 }
95 }
96
97 println!("dimension = {dim}");
98 println!("length = {length}");
99 println!("beta = {beta}");
100 println!(
101 "energy density = {} +/- {}",
102 energy.mean(),
103 energy.error()
104 );
105 println!(
106 "magnetization density^2 = {} +/- {}",
107 mag2.mean(),
108 mag2.error()
109 );
110}Sourcepub fn target(&self, bond: usize) -> usize
pub fn target(&self, bond: usize) -> usize
Examples found in repository?
examples/support/mod.rs (line 39)
4pub fn print_graph(graph: &Graph) {
5 println!("dimension = {}", graph.dimension());
6 println!("sites = {}", graph.num_sites());
7 println!("bonds = {}", graph.num_bonds());
8
9 for site in 0..graph.num_sites() {
10 let coordinate = graph.coordinate(site);
11 let coordinate_text = if coordinate.is_empty() {
12 "[]".to_string()
13 } else {
14 let joined = coordinate
15 .iter()
16 .map(|value| format!("{value:.6}"))
17 .collect::<Vec<_>>()
18 .join(", ");
19 format!("[{joined}]")
20 };
21
22 let neighbors = (0..graph.num_neighbors(site))
23 .map(|k| graph.neighbor(site, k).to_string())
24 .collect::<Vec<_>>()
25 .join(", ");
26
27 println!(
28 "site {site}: type={} coord={} neighbors=[{}]",
29 graph.site_type(site),
30 coordinate_text,
31 neighbors
32 );
33 }
34
35 for bond in 0..graph.num_bonds() {
36 println!(
37 "bond {bond}: {} <-> {} type={}",
38 graph.source(bond),
39 graph.target(bond),
40 graph.bond_type(bond)
41 );
42 }
43}More examples
examples/ising.rs (line 88)
56fn main() {
57 println!("Metropolis Algorithm for Classical Ferromagnetic Ising Model");
58
59 let beta = 1.0 / 2.269;
60
61 let dim = 2usize;
62 let length = 32usize;
63 let graph = Graph::simple(dim, length);
64
65 let mut rng = Lcg::new(12345);
66 let mut spins = vec![1.0f64; graph.num_sites()];
67
68 let mut energy = Observable::default();
69 let mut mag2 = Observable::default();
70
71 let sweeps = 65536usize;
72 let therm = sweeps / 8;
73
74 for mcs in 0..(therm + sweeps) {
75 for site in 0..graph.num_sites() {
76 let mut diff = 0.0;
77 for k in 0..graph.num_neighbors(site) {
78 diff += 2.0 * spins[site] * spins[graph.neighbor(site, k)];
79 }
80 if rng.next_f64() < (-beta * diff).exp() {
81 spins[site] = -spins[site];
82 }
83 }
84
85 if mcs > therm {
86 let mut ene = 0.0;
87 for bond in 0..graph.num_bonds() {
88 ene -= spins[graph.source(bond)] * spins[graph.target(bond)];
89 }
90 energy.add(ene / graph.num_sites() as f64);
91
92 let mag = spins.iter().sum::<f64>() / graph.num_sites() as f64;
93 mag2.add(mag * mag);
94 }
95 }
96
97 println!("dimension = {dim}");
98 println!("length = {length}");
99 println!("beta = {beta}");
100 println!(
101 "energy density = {} +/- {}",
102 energy.mean(),
103 energy.error()
104 );
105 println!(
106 "magnetization density^2 = {} +/- {}",
107 mag2.mean(),
108 mag2.error()
109 );
110}pub fn edge_sites(&self, bond: usize) -> (usize, usize)
pub fn add_site( &mut self, coordinate: CoordinateVector, site_type: i32, ) -> usize
pub fn add_bond( &mut self, source: usize, target: usize, bond_type: i32, ) -> usize
Trait Implementations§
impl StructuralPartialEq for Graph
Auto Trait Implementations§
impl Freeze for Graph
impl RefUnwindSafe for Graph
impl Send for Graph
impl Sync for Graph
impl Unpin for Graph
impl UnsafeUnpin for Graph
impl UnwindSafe for Graph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.