Skip to main content

Graph

Struct Graph 

Source
pub struct Graph { /* private fields */ }

Implementations§

Source§

impl Graph

Source

pub fn new(dim: usize) -> Self

Source

pub fn simple(dim: usize, length: usize) -> Self

Examples found in repository?
examples/construct3.rs (line 7)
6fn main() {
7    let graph = Graph::simple(2, 4);
8    support::print_graph(&graph);
9}
More examples
Hide additional examples
examples/construct1.rs (line 7)
6fn main() {
7    let graph = Graph::simple(1, 16);
8    support::print_graph(&graph);
9}
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}
Source

pub fn fully_connected(num_sites: usize) -> Self

Source

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
Hide additional 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}
Source

pub fn from_basis_unitcell_length( basis: &Basis, cell: &Unitcell, length: usize, boundary: Boundary, ) -> Self

Source

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}
Source

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
Hide additional 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}
Source

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}
Source

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}
Source

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
Hide additional 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}
Source

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
Hide additional 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}
Source

pub fn neighbor_bond(&self, site: usize, neighbor: usize) -> usize

Source

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
Hide additional 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}
Source

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}
Source

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
Hide additional 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}
Source

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
Hide additional 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}
Source

pub fn edge_sites(&self, bond: usize) -> (usize, usize)

Source

pub fn add_site( &mut self, coordinate: CoordinateVector, site_type: i32, ) -> usize

Source

pub fn add_bond( &mut self, source: usize, target: usize, bond_type: i32, ) -> usize

Trait Implementations§

Source§

impl Clone for Graph

Source§

fn clone(&self) -> Graph

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Graph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Graph

Source§

fn eq(&self, other: &Graph) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,