Skip to content

Commit

Permalink
Merge foundation modifiers and generators
Browse files Browse the repository at this point in the history
  • Loading branch information
dean0x7d committed Jul 14, 2017
1 parent ebabdde commit affad64
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 174 deletions.
5 changes: 2 additions & 3 deletions cppcore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ add_library(cppcore
include/system/Foundation.hpp
include/system/HoppingBlocks.hpp
include/system/Shape.hpp
include/system/StructureModifiers.hpp
include/system/Symmetry.hpp
include/system/System.hpp
include/system/Generators.hpp
include/system/SystemModifiers.hpp
include/utils/Chrono.hpp
include/KPM.hpp
include/Lattice.hpp
Expand All @@ -90,9 +89,9 @@ add_library(cppcore
src/system/Foundation.cpp
src/system/HoppingBlocks.cpp
src/system/Shape.cpp
src/system/StructureModifiers.cpp
src/system/Symmetry.cpp
src/system/System.cpp
src/system/SystemModifiers.cpp
src/utils/Chrono.cpp
src/KPM.cpp
src/Lattice.cpp
Expand Down
10 changes: 4 additions & 6 deletions cppcore/include/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include "system/System.hpp"
#include "system/Shape.hpp"
#include "system/Symmetry.hpp"
#include "system/SystemModifiers.hpp"
#include "system/Generators.hpp"
#include "system/StructureModifiers.hpp"
#include "leads/Leads.hpp"
#include "hamiltonian/Hamiltonian.hpp"
#include "hamiltonian/HamiltonianModifiers.hpp"
Expand Down Expand Up @@ -73,9 +72,9 @@ class Model {
double hamiltonian_build_seconds() const { return hamiltonian_build_time.elapsed_seconds(); }

public:
void clear_system_modifiers() { system_modifiers.clear(); }
void clear_structure_modifiers() { structure_modifiers.clear(); }
void clear_hamiltonian_modifiers() { hamiltonian_modifiers.clear(); }
void clear_all_modifiers() { clear_system_modifiers(); clear_hamiltonian_modifiers(); }
void clear_all_modifiers() { clear_structure_modifiers(); clear_hamiltonian_modifiers(); }

private:
std::shared_ptr<System> make_system() const;
Expand All @@ -93,9 +92,8 @@ class Model {
TranslationalSymmetry symmetry;
Cartesian wave_vector = {0, 0, 0};

std::vector<SystemModifier> system_modifiers;
std::vector<StructureModifier> structure_modifiers;
HamiltonianModifiers hamiltonian_modifiers;
HoppingGenerators hopping_generators;

mutable std::shared_ptr<System const> _system;
mutable Hamiltonian _hamiltonian;
Expand Down
16 changes: 15 additions & 1 deletion cppcore/include/detail/sugar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,18 @@ namespace cpb { namespace detail {
template<class... Ts> void eval_unordered(Ts&&...) {}
template<class T> void eval_ordered(std::initializer_list<T>) {}

}} // namespace cpb::detail
template<class It1, class It2>
struct range {
It1 _begin;
It2 _end;

It1 begin() const { return _begin; }
It2 end() const { return _end; }
};

} // namespace detail

template<class It1, class It2>
detail::range<It1, It2> make_range(It1 begin, It2 end) { return {begin, end}; }

} // namespace cpb
44 changes: 0 additions & 44 deletions cppcore/include/system/Generators.hpp

This file was deleted.

136 changes: 136 additions & 0 deletions cppcore/include/system/StructureModifiers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#pragma once
#include "Lattice.hpp"

#include "numeric/dense.hpp"

#include <vector>
#include <algorithm>
#include <memory>

namespace cpb {

class Foundation;
struct System;

/**
Modify the state (valid or invalid) of lattice sites, e.g. to create vacancies
*/
class SiteStateModifier {
public:
using Function = std::function<void(Eigen::Ref<ArrayX<bool>> state, CartesianArrayConstRef pos,
string_view sublattice)>;
Function apply; ///< to be user-implemented
int min_neighbors; ///< afterwards, remove sites with less than this number of neighbors

SiteStateModifier(Function const& apply, int min_neighbors = 0)
: apply(apply), min_neighbors(min_neighbors) {}
};

/**
Modify the position of lattice sites, e.g. to apply geometric deformations
*/
class PositionModifier {
public:
using Function = std::function<void(CartesianArrayRef position, string_view sublattice)>;
Function apply; ///< to be user-implemented

PositionModifier(Function const& apply) : apply(apply) {}
};

/**
Helper class for passing sublattice information
*/
struct SubIdRef {
ArrayX<storage_idx_t> const& ids;
std::unordered_map<std::string, storage_idx_t> name_map;
};

/**
Introduces a new hopping family (with new hop_id) via a list of index pairs
This can be used to create new hoppings independent of the main Lattice definition.
It's especially useful for creating additional local hoppings, e.g. to model defects.
*/
class HoppingGenerator {
public:
/// Site index pairs which should form new hoppings
struct Result {
ArrayXi from;
ArrayXi to;
};
using Function = std::function<Result(cpb::CartesianArray const&, SubIdRef)>;

std::string name; ///< friendly hopping identifier - will be added to lattice registry
std::complex<double> energy; ///< hopping energy - also added to lattice registry
Function make; ///< function which will generate the new hopping index pairs

HoppingGenerator(std::string const& name, std::complex<double> energy, Function const& make)
: name(name), energy(energy), make(make) {}

explicit operator bool() const { return static_cast<bool>(make); }
};

template<class M> void apply(M const&, Foundation&) {}
void apply(SiteStateModifier const& m, Foundation& f);
void apply(PositionModifier const& m, Foundation& f);

template<class M> void apply(M const&, System&) {}
void apply(HoppingGenerator const& g, System& s);

template<class M> constexpr bool requires_system(M const&) { return false; }
constexpr bool requires_system(HoppingGenerator const&) { return true; }

template<class M> constexpr bool is_generator(M const&) { return false; }
constexpr bool is_generator(HoppingGenerator const&) { return true; }

/**
Polymorphic storage for system/foundation modifiers
Behaves like a common base for several classes but without actually needing
to inherit from anything -- a class just needs to satisfy the interface.
This allows us to use value semantics with polymorphic behavior.
See: "Inheritance Is The Base Class of Evil" by Sean Parent.
*/
class StructureModifier {
public:
template<class T>
StructureModifier(T x) : impl(std::make_shared<Storage<T>>(std::move(x))) { }

friend void apply(StructureModifier const& x, Foundation& f) { x.impl->v_apply(f); }
friend void apply(StructureModifier const& x, System& s) { x.impl->v_apply(s); }
friend bool requires_system(StructureModifier const& x) { return x.impl->v_requires_system(); }
friend bool is_generator(StructureModifier const& x) { return x.impl->v_is_generator(); }

private:
struct Interface {
Interface() = default;
virtual ~Interface() = default;

Interface(Interface const&) = delete;
Interface(Interface&&) = delete;
Interface& operator=(Interface const&) = delete;
Interface& operator=(Interface&&) = delete;

virtual void v_apply(Foundation&) const = 0;
virtual void v_apply(System&) const = 0;
virtual bool v_requires_system() const = 0;
virtual bool v_is_generator() const = 0;
};

template<class T>
struct Storage : Interface {
Storage(T x) : data(std::move(x)) { }

void v_apply(Foundation& f) const override { apply(data, f); }
void v_apply(System& s) const override { apply(data, s); }
bool v_requires_system() const override { return requires_system(data); }
bool v_is_generator() const override { return is_generator(data); }

T data;
};

std::shared_ptr<Interface const> impl;
};

} // namespace cpb
4 changes: 0 additions & 4 deletions cppcore/include/system/System.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "Lattice.hpp"
#include "system/CompressedSublattices.hpp"
#include "system/HoppingBlocks.hpp"
#include "system/Generators.hpp"

#include "numeric/dense.hpp"
#include "numeric/sparse.hpp"
Expand Down Expand Up @@ -31,8 +30,6 @@ struct System {
std::vector<Boundary> boundaries;

System(Lattice const& lattice) : lattice(lattice) {}
System(Foundation const& foundation, TranslationalSymmetry const& symmetry,
HoppingGenerators const& hopping_generators);

/// The total number of lattice sites i.e. unique positions. Note that a single site may
/// consist of several orbitals/spins which means that the size of the Hamiltonian matrix
Expand Down Expand Up @@ -72,7 +69,6 @@ namespace detail {
void populate_system(System& system, Foundation const& foundation);
void populate_boundaries(System& system, Foundation const& foundation,
TranslationalSymmetry const& symmetry);
void add_extra_hoppings(System& system, HoppingGenerator const& gen);
} // namespace detail

} // namespace cpb
84 changes: 0 additions & 84 deletions cppcore/include/system/SystemModifiers.hpp

This file was deleted.

Loading

0 comments on commit affad64

Please sign in to comment.