Fastnet.FastNetType
FastNet(n,k,c,tlist;<keyword arguments>)

Create a FastNet object that represents a network structe.

Memory will be allocated for up to n nodes, up to k links. Nodes can be in one of c different states.

The argument tlist is an array or tuple of LinkType. This list tells the networks which types of links are important for you. For example in an epidemic simulation the we are particularly interested in links between infected and susceptible nodes. FastNet will do the necessary bookkeeping, to enable very fast counting, selection, etc. of the links that are in a state listed in tlist.

Note that the order of elements of tlist is not arbitrary. FastNet will think of links that match the first element of tlist as being in link state 1. The links that match the sceond type in link state 2, and so on.

WARNING: Each link in the network can only be in any one state at any time passing a tlist that contains overlapping link types (e.g. [LinkType([1,2],3),LinkType(3,1)] ) will result in an ArgumentError being thrown.

FastNet supports a number of optional keyword arguments:

  • nodealias : a vector of strings that will be used as names of node states in outputs
  • linkalias : a vector of strings that will be used as names of link states in outputs
  • rng : specifies a custom random number generator

The network will initially be empty (i.e. a null graph)

Examples

julia> using Fastnet

julia> FastNet(10,9,1,[])
Network of 0 nodes and 0 links

julia> FastNet(100,1000,3,[LinkType(1,2),LinkType(3,"*",1)])
Network of 0 nodes and 0 links

julia> using Random

julia> mt = MersenneTwister(1234);

julia> const S=1;

julia> const I=2;

julia> SI_link=LinkType(S,I)
Links of the form:  (1) --- (2)

julia> net=FastNet(10000,60000,2,[SI_link],nodealias=["S","I"],linkalias=["S-I"],rng=mt)
Network of 0 nodes and 0 links
Fastnet.FastSimType
FastSim(net,rates!,processes;<keyword arguments>)

Create a FastSim structure, representing a FastNet simulation run.

The first argument net is a FastNet structure that is be used in the simulation.

The second argument is the rates! function of the simulation. The rates function is a function that accepts two arguments. The first of these arguments is an MVector{Float64} (see StaticArrays Documentation for details). The second argument is a the current time in the simulation.

When the rates! function is called it should compute the total rates of at which the different processes occur in the system, given the current state of the network. The rates functions returns these values by filling the array that was passed as the first argument. The rates! should not have a return value.

Note that when rates are time dependent then the rates! function should use the time value passed to it rather than obtaining a time form the simulation structure. The simulation code assumes that the rates will remain constant until the next event. This should be harmless in almost all cases but can cause inaccuracy if your rates depend explicitely on time, the rates are very senstitive to time and events are rare.

The third argument is a Vector of functions that implements the processes. The processes are functions without arguments when they are called they should implement effect of the respecive process running once. Note that elemets of the process function vector should be in the same order as the corresponding rates computed by the rates! vector.

FastSim supports a number of optional keyword arguments:

  • saveas : A String specifying the pathname where results should be saved. If unspecified results aren't saved but can be optained using the results function.

  • output : a boolean variable that specifies if results should be printed on the console, by default this is true. Alternatively also an IOStream can be provided to which results should be written.

  • repfunc : This argument can be used to specify an alternative function to generate outputs and store results. See custimization for details.

Examples

julia> using Fastnet

julia> const Bored=1; const Excited=2;

julia> net=FastNet(1000,5000,2,[LinkType(Excited,Bored,1)]);

julia> randomgraph!(net);

julia> function rates!(rates,t)
         rates[1]=countlinks(net,1)*0.1
         rates[2]=countnodes(net,Excited)*0.2
         rates[3]=countnodes(net,Bored)*0.001
         end;

julia> function excitement_spreads()
         link=randomlink(net,1)
         nodestate!(net,linkdst(net,link),Excited)
         end;

julia> function get_bored()
         node=randomnode(net,Excited)
         nodestate!(net,node,Bored)
         end;

julia> function great_idea()
         node=randomnode(net,Bored)
         nodestate!(net,node,Excited)
         end;

julia> sim=FastSim(net,rates!,[excitement_spreads,get_bored,great_idea])
Simulation run, currently at time 0.0
Fastnet.LinkTypeType
LinkType(from,to,dir=2)

Create a LinkType structure that describes the properties of a type of link in the network.

Think of a LinkType as a set of criteria that describe a certain sort of link. The first two arguments specify the states of the nodes at the start and end of the link respectively. The third argument specified if the LinkType can either be 1 or 2, where dir=1 signifies that the link type should be interpreted as directed (unidirectional) and dir=2 signifies that it should be interpreted as undirected (bidirectional).

The state of the node at the start and end of this type of link can be specified in different ways. A value of 0 or * for from or to means that the respective node can be in any state. An integer value corresponding to a node state means that the node must be in the respective state. An Array or Tuple of Ints means that the node must be in one of the states listed.

Examples

julia> using Fastnet

julia> LinkType(3,4)
Links of the form:  (3) --- (4)

julia> LinkType(3,4,1)
Links of the form:  (3) --> (4)

julia> LinkType(3,4,2)
Links of the form:  (3) --- (4)

julia> LinkType("*",4)
Links of the form:  (any) --- (4)

julia> LinkType(4,0)
Links of the form:  (4) --- (any)

julia> LinkType((1,2),3)
Links of the form:  (1/2) --- (3)

julia> LinkType(4,[1,2],1)
Links of the form:  (4) --> (1/2)
Fastnet.adjacency!Method
adjacency!(net,mat;S=1)

Create a network with given adjacency matrix.

The network in net is replaced with the new topology that is specified by the adjacency matrix mat. If direction of links matters note that the element mat[i,j] corresponds to the link from j to i.

Symmeric matrices will not result in parallel links, instead the link is placed in an arbitrary direction.

Note that node n in the matrix will be the node in position n in net after creation, which is not necessarily the node with ID n, if you need to find a particular node at a later time then it is best to save its id using the node(net,pos) function directly after calling adjacency!(net,mat).

If net is not large enough to accomodate the desired number of nodes or links an argument error will be thrown.

Keyword arguments are

  • S : The state of the nodes. All nodes will be set to this state.

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> mat=[0 1 0; 1 0 1; 0 1 0]
3×3 Matrix{Int64}:
 0  1  0
 1  0  1
 0  1  0

julia> adjacency!(net,mat)
Network of 3 nodes and 2 links
Fastnet.adjacentMethod
adjacent(net,a,b)
adjacent_f(net,a,b)

Check if nodes with the id's a and b are adjacent in network net. If they are return the id a of the link connecting them. Otherwise return 0.

If multiple links connect the nodes the function will return a link in the direction a->b if such a link exists.

Calling adjacent(net,a,a) will return a self-loop on a if one exists.

The worst case performance of this function scales with the degree of node a. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> makenodes!(net,100,1);

julia> net
Network of 100 nodes and 0 links
Fastnet.configmodel!Method
configmodel!(net::FastNet,degreedist;<keyword arguments>)

Create a configuration model style network with prescribed degree distribution, degreedist.

The network in net is replaced with the new topology. The degree distribution is specified in terms of a vector of Float64 variables such that degreedist[k] is specifies, p<sub>k</sub>, the probability that a randomly drawn node has degree k. If the elements of degreedist add up to less than 1.0 the remaining nodes will have degree zero.

The network generation is fast and unbiased but isn't guaranteed to result in a simple graph. The algorithm will try to match the desired degree distribution as closely as possible, but small discrepancies can appear if the degree distribution would result in an odd degree sum or non integer numbers of nodes of certain degrees.

If there FastNet is not large enough to accomodate the desired number of links or nodes an argument error will be thrown.

The keyword arguments are

  • N : The number of nodes that will be used in the creation of the network
  • S : The state of the nodes. All nodes will be set to this state.

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> configmodel!(net,[0.5,0.25,0.25],N=200)
Network of 200 nodes and 175 links

julia> degreedist(net)
3-element Vector{Float64}:
 0.5
 0.25
 0.25

julia> configmodel!(net,[0.5,0.25],N=200)
Network of 200 nodes and 100 links

julia> degreedist(net)
2-element Vector{Float64}:
 0.5
 0.25
Fastnet.countlinksMethod
countlinks(net)
countlinks(net,s)
countlinks_f(net)
countlinks_f(net,s)

Count the links in state s, or, if no state is provided, in the entire network.

Instead of the state s also an Array or Tuple of states can be passed. In this case the total number of nodes in all of the listed states is returned.

The links in a sincle class or the entire network are counted in constant time. For the tuples or array arguments the performance scales with the number of elements in the Tulps/Array. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

If performance is critical use this function rather than linkcounts.

See also linkcounts

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[LinkType(1,2,1),LinkType(2,2,2)])
Network of 0 nodes and 0 links

julia> makenodes!(net,20,1)

julia> makenodes!(net,20,2)

julia> for i=1:20
         from=node(net,1,i)
         to=node(net,2,i)
         makelink!(net,from,to)
       end

julia> makelink!(net,node(net,2,1),node(net,2,2));

julia> countlinks(net)
21

julia> countlinks(net,1)
20

julia> countlinks(net,2)
1

julia> countlinks(net,[1,2])
21
Fastnet.countnodesMethod
countnodes(net)
countnodes(net,s)
countnodes_f(net)
countnodes_f(net,s)

Count the nodes in state s, or, if no state is provided, in the entire network.

Instead of the state s also an Array or Tuple of states can be passed. In this case the total number of nodes in all of the listed states is returned.

All versions of this function run in constant time. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

If performance is critical use this function rather than nodecounts.

See also nodecounts

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> for i=1:20
         makenode!(net,1)
         end

julia> for i=1:10
         makenode!(net,2)
         end

julia> countnodes(net)
30

julia> countnodes(net,1)
20

julia> countnodes(net,(1,2))
30
Fastnet.degreeMethod
degree(net,nid)
degree_f(net,nid)

Return the degree of the node with id nid in network net.

Here degree is interpreted as the number of times this node appears an an endpoint of a link, hence self-loops contribute 2 to the degree of the node that they link to.

The worst case performance scales only with the degree of the affected node. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also indegree, outdegree

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> n3=makenode!(net,1);

julia> makelink!(net,n1,n2);

julia> makelink!(net,n2,n3);

julia> degree(net,n1)
1

julia> degree(net,n2)
2
Fastnet.degreedistMethod
degreedist(net::FastNet)

Return a vector of Float64 that specifies the networks degree distribution.

The element k of the returned vector is the probability that the probability that the a randomly drawn node from the network has degree k.

If the elements of the vector do not add up to 1.0, the remainder is the probability that a randomly drawn node has degree zero.

Example

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> makenodes!(net,4,1)

julia> makelink!(net,node(net,1),node(net,2));

julia> makelink!(net,node(net,2),node(net,3));

julia> degreedist(net)
2-element Vector{Float64}:
 0.5
 0.25
Fastnet.destroylink!Method
destroylink!(net,kid)
destroylink_f!(net,kid)

Destroy the link with id kid in network net.

All versions of this function run in constant time. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also makelink!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,2);

julia> makelink!(net,n1,n2);

julia> net
Network of 2 nodes and 1 links

julia> lnk=randomlink(net);

julia> destroylink!(net,lnk)

julia> net
Network of 2 nodes and 0 links
Fastnet.destroynode!Method
destroynode!(net,nid)
destroynode_f!(net,nid)

Destroy the node with id nid in network net.

Worst-case performance of both versions of this function is O(ks*k)+O(ns) where ks is the number of tracked link states, k is the degree of the affected node and ns is the number of node states.

The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also makenode!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n=makenode!(net,1);

julia> net
Network of 1 nodes and 0 links

julia> destroynode!(net,n)

julia> net
Network of 0 nodes and 0 links
Fastnet.firstlinkinMethod
firstlinkin(net,nid)
firstlinkin_f(net,nid)

Return the link id of the first incoming link to the node with id nid in network net.

If there are no incoming links then the return value is 0

All versions of this function run in constant time. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also firstlinkout, nextlinkin

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> l1=makelink!(net,n1,n2);

julia> firstlink=firstlinkin(net,n2);

julia> firstlink==l1
true

julia> linksrc(net,firstlink)==n1
true

julia> linkdst(net,firstlink)==n2
true
Fastnet.firstlinkoutMethod
firstlinkout(net,nid)
firstlinkout_f(net,nid)

Return the link id of the first outgoing link from the node with id nid in network net.

If there are no outgoing links then the return value is 0

All versions of this function run in constant time. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also firstlinkout, nextlinkin

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> l1=makelink!(net,n1,n2);

julia> firstlink=firstlinkout(net,n1);

julia> firstlink==l1
true

julia> linksrc(net,firstlink)==n1
true

julia> linkdst(net,firstlink)==n2
true
Fastnet.healthcheckMethod
healthcheck(net)

Perform an internal consistencey check on a FastNet net.

To achieve the desired performance Fastnet engages in a certain amount of double bookeeping. In an ideal world the FastNet structures should always stay internally consistent. However, inconsistencies could arise from a number of sources including software bugs, CPU and memeory errors. This function checks the internal data stored in FastNet for consistency to make sure that everything is alright.

The return value is true if all chacks have been passed, false otherwise.

See also link

Examples

julia> using Fastnet

julia> net=FastNet(100,200,10,[])
Network of 0 nodes and 0 links

julia> healthcheck(net);
  Checking repository consistency ... OK
  Checking node accounting ... OK
  Checking link accounting ... OK
  Checking endpoint consistency ... OK
  Checking node stateification ... OK
  Checking link stateification ... OK
Fastnet.indegreeMethod
indegree(net,nid)
indegree_f(net,nid)

Return the incoming degree of the node with id nid in network net.

The worst case performance scales only with the indegree of the affected node. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also degree, outdegree

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> n3=makenode!(net,1);

julia> makelink!(net,n1,n2);

julia> makelink!(net,n3,n2);

julia> indegree(net,n1)
0

julia> indegree(net,n2)
2
Fastnet.linkMethod

link(net,rp) link(net,s,rp) linkf(net,rp) linkf(net,s,rp)

Determine link id from relative rp position and node state s.

The link function provides a way to access links form the set of nodes in a certain link state, or from the set of all links. The two-argument version returns the id of the link at poition rp in network net. The three-argument version returns the id of the link at poition rp within the set of links that are in state s.

All version of this function run in constant time, but fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also adjacent

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[LinkType(1,2),LinkType(1,1),LinkType(2,2)])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> n3=makenode!(net,2);

julia> makelink!(net,n1,n2);

julia> makelink!(net,n2,n3);

julia> makelink!(net,n3,n1);

julia> lid=link(net,2,1);

julia> linkdst(net,lid)==n2
true

julia> linksrc(net,lid)==n1
true
Fastnet.linkcountsMethod
linkcounts(net)
linkcounts_f(net)

Return an Array containing the number of link in the vairous link states.

The elements of the array will show the counts in the same order in which the link types were passed to the FastNet Constructor.

The time required for this function scales only with the number of link states (it is independent of the number of links).

The alternative (_f) version of this function is identical only provided for convenience.

See also countlinks

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[LinkType(1,2),LinkType(1,1),LinkType(2,2)])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);  

julia> n2=makenode!(net,1);

julia> n3=makenode!(net,2);

julia> makelink!(net,n1,n2);

julia> makelink!(net,n2,n3);

julia> makelink!(net,n3,n1);

julia> linkcounts(net)
3-element Vector{Int64}:
 2
 1
 0
Fastnet.linkdstMethod
linkdst!(net,kid)
linkdst_f!(net,kid)

Return the id of the node at the destination of link kid in net.

All versions of this function run in constant time. The fast (_f) verion sacrifices some safty checks for better performance. See basic concepts for details.

See also makelink!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,2);

julia> k1=makelink!(net,n1,n2);

julia> linkdst(net,k1)==n2
true
Fastnet.linkexistsMethod
linkexists(net,kid)
linkexists_f(net,kid)

Return true node with id kid exists in net, false otherwise.

This function runs in constant time. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also makelink!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> linkexists(net,7)
false

julia> k=makelink!(net,n1,n2);

julia> linkexists(net,k)
true
Fastnet.linksrcMethod
linksrc!(net,kid)
linksrc_f!(net,kid)

Return the id of the node at the source of link kid in net.

All versions of this function run in constant time. The fast (_f) verion sacrifices some safty checks for better performance. See basic concepts for details.

See also makelink!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,2);

julia> k1=makelink!(net,n1,n2);

julia> linksrc(net,k1)==n1
true
Fastnet.linkstateMethod
linkstate(net,kid)
linkstate_f(net,kid)

Return the state of the link with id kid in network net.

Note that the link states are numbered in the order in which they were passed to the FastNet Constructor.

All version of this function run in constant time, but fast (_f) verion sacrifices some safty checks for better performance. See basic concepts for details.

See also nodestate!, FastNet

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[LinkType(1,2),LinkType(1,1),LinkType(2,2)])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> lnk=makelink!(net,n1,n2);

julia> linkstate(net,lnk)
2
Fastnet.listlinksMethod
listlinks(net)

Return an array that contains the source and destination of all links in net.

The return value will be a two-dimensional array of dimensions (K,3), where K is the number of links in the network. Each row corresponds to one link. The contests of the array are

  • First column: Identical to the linkid of the respective link
  • Second column: Source node of the link
  • Third column: Destination node of the link

Warning: Do not rely on the row index to be identical to the link ID

See also savelinklist

Example

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> makenodes!(net,5,1)

julia> makelink!(net,1,2)
1

julia> makelink!(net,1,3)
2

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> makenodes!(net,5,1)

julia> makelink!(net,1,2)
1

julia> makelink!(net,2,3)
2

julia> makelink!(net,3,4)
3

julia> makelink!(net,4,5)
4

julia> makelink!(net,5,1)
5

julia> listlinks(net)
5×3 Matrix{Int64}:
 1  1  2
 2  2  3
 3  3  4
 4  4  5
 5  5  1
Fastnet.listneighborsMethod
listneighbors(FastNet,nid)

Return a vector of the IDs of all nodes that are asjacent to node nid in FastNet net.

This function is comparatively slow as it needs to allocate the vector. In your rates! and process functions it is preferable to iterate over the neighbors using firstlinkout, firstlinkin, nexlinkout, nextlinkin.

See also listnodes

Example

julia> using Fastnet

julia> net=FastNet(10,10,2,[])
Network of 0 nodes and 0 links

julia> makenodes!(net,3,1)

julia> makenodes!(net,2,2)

julia> listnodes(net)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

 julia> listnodes(net,2)
 2-element Vector{Int64}:
 4
 5 
Fastnet.listnodesMethod
listnodes(FastNet)    
listnodes(FastNet,state)

Return a vector of the IDs of all nodes in FastNet net or all nodes in state state in net.

The one-argument method of this function returns a Vector containing the IDs of all nodes that are in the network. The two-argument method creates a vector of all nodes in state state.

See also listneighbors

Example

julia> using Fastnet

julia> net=FastNet(10,10,2,[])
Network of 0 nodes and 0 links

julia> makenodes!(net,3,1)

julia> makenodes!(net,2,2)

julia> listnodes(net)
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> listnodes(net,2)
2-element Vector{Int64}:
 4
 5
Fastnet.listnodestatesMethod
listnodestates(net)

Return an array that contains the ids and states of all nodes in net.

The return value will be a two-dimensional array of dimension (K,2), where K is the number of nodes in the network. Each row corresponds to one node. The contests of the array ar

  • First Column: ID of the respective node
  • Second column: State of the node

Warning: Do not rely on the row index to be identical to the node ID

See also listlinks

Example

julia> using Fastnet

julia> net=FastNet(100,100,3,[])
Network of 0 nodes and 0 links

julia> makenodes!(net,2,1)

julia> makenodes!(net,2,2)

julia> makenodes!(net,3,3)

julia> listnodestates(net)
7×2 Matrix{Int64}:
 1  1
 2  1
 3  2
 4  2
 5  3
 6  3
 7  3
Fastnet.makelink!Method
makelink!(net,src,dst)
makelink_f!(net,src,dst)

Create a new link from node src to nodedst in the network net and return it's id.

Worst-case performance of both versions of this function scales only with the number of tracked link states.

The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also destroylink!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> makelink!(net,n1,n2);

julia> net
Network of 2 nodes and 1 links
Fastnet.makenode!Method
makenode!(net,s)
makenode_f!(net,s)

Create a new node in state s in the network net and return it's id.

Worst-case performance of both versions of this function scales only with the number of node states.

The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also destroynode!, makenodes!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> makenode!(net,2)
1

julia> net
Network of 1 nodes and 0 links

julia> nodestate(net,1)
2
Fastnet.makenodes!Method
makenodes!(net,N,s)
makenodes_f!(net,N,s)

Create N nodes in state s in the network *net.

Worst case performance of this function scales only with the number of node states. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also makenode!, destroynode!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> makenodes!(net,100,1);

julia> net
Network of 100 nodes and 0 links
Fastnet.nextlinkinMethod
nextlinkin!(net,kid)
nextlinkin_f!(net,kid)

Get the id of the next incoming link to the node at the destination of link kid in net.

This function can be used to iterate over the incoming links of a node. If kid is the nodes last link the return value is zero.

All versions of this function run in constant time. The fast (_f) verion sacrifices some safty checks for better performance. See basic concepts for details.

See also firstlinkin, nextlinkout

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> n3=makenode!(net,1);

julia> n4=makenode!(net,1);

julia> makelink!(net,n2,n1)
1

julia> makelink!(net,n3,n1)
2

julia> makelink!(net,n4,n1)
3

julia> k=firstlinkin(net,n1);

julia> while k!=0
           println(k)
           k=nextlinkin(net,k)
           end
3
2
1
Fastnet.nextlinkoutMethod
nextlinkout!(net,kid)
nextlinkout_f!(net,kid)

Get the id of the next outgoing link from the node at the source of link kid in net.

This function can be used to iterate over the outgoing links of a node. If kid is the nodes last link the return value is zero.

All versions of this function run in constant time. The fast (_f) verion sacrifices some safty checks for better performance. See basic concepts for details.

See also firstlinkout, nextlinkin

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> n3=makenode!(net,1);

julia> n4=makenode!(net,1);

julia> makelink!(net,n1,n2)
1

julia> makelink!(net,n1,n3)
2

julia> makelink!(net,n1,n4)
3

julia> k=firstlinkout(net,n1);

julia> while k!=0
           println(k)
           k=nextlinkout(net,k)
           end
3
2
1
Fastnet.nodeMethod
node(net,rp)
node(net,s,rp)
node_f(net,rp)
node_f(net,s,rp)

Determine node id from relative position and node state.

The node function provides a way to access nodes form a the set of nodes in certain states, or from the set of all nodes in a simple way. The two-argument version returns the id of the node at poition rp in network net. The three-argument version returns the id of the node at poition rp within all nodes in state s.

All version of this function run in constant time, but fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> randomgraph!(net)
Network of 1000 nodes and 2000 links

julia> nodestate!(net,123,2)

julia> nodestate!(net,345,2)

julia> node(net,2,1)
345

julia> node(net,2,2)
123

julia> node(net,1)
1

julia> destroynode!(net,1)

julia> node(net,1)
998
Fastnet.nodecountsMethod
nodecounts(net)
nodecounts_f(net)

Return an Array containing the number of nodes in the vairous node states.

The time required for this function scales only with the number of node states (it is independent of the number of nodes).

The alternative (_f) version of this function is identical to nodecounts and is provided only for convenience.

See also countnodes

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> randomgraph!(net)
Network of 1000 nodes and 2000 links

julia> for i=1:20
         n=node(net,1,1)
         nodestate!(net,n,2)
       end

julia> nodecounts(net)
2-element Vector{Int64}:
 980
  20
Fastnet.nodeexistsMethod
nodeexists(net,nid)
nodeexists_f(net,nid)

Return if a node with id nid exists in net, false otherwise.

This function runs in constant time. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also makenode!, destroynode!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> nodeexists(net,n1)
true
Fastnet.nodestate!Method
nodestate!(net,nid,s)
nodestate_f!(net,nid,s)

Set the node with id nid in net net to s.

Worst-case performance of both versions of this function is O(ks*k)+O(ns) where ks is the number of tracked link states, k is the degree of the affected node and ns is the number of node states.

The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also nodestate

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> randomgraph!(net)
Network of 1000 nodes and 2000 links

julia> nodestate(net,1)
1

julia> nodestate!(net,1,2)

julia> nodestate(net,1)
2
Fastnet.nodestateMethod
nodestate(net,nid)
nodestate_f(net,nid)

Return the state of the node with id nid in network net.

All version of this function run in constant time, but fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also nodestate!

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> randomgraph!(net)
Network of 1000 nodes and 2000 links

julia> nodestate(net,1)
1

julia> nodestate!(net,1,2)

julia> nodestate(net,1)
2
Fastnet.nullgraph!Method
nullgraph!(net)

Remove all nodes and links from the network.

The first argument net is a FastNet structure that is be used in the simulation.

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,1,[])
Network of 0 nodes and 0 links

julia> randomgraph!(net)
Network of 1000 nodes and 2000 links

julia> nullgraph!(net)
Network of 0 nodes and 0 links
Fastnet.outdegreeMethod
outdegree(net,nid)
outdegree_f(net,nid)

Return the outgoing degree of the node with id nid in network net.

The worst case performance scales only with the outdegree of the affected node. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

See also degree, indegree

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,1);

julia> n3=makenode!(net,1);

julia> makelink!(net,n1,n2);

julia> makelink!(net,n3,n2);

julia> outdegree(net,n1)
1

julia> outdegree(net,n2)
0
Fastnet.randomgraph!Method
randomgraph!(net;<keyword arguments>)

Create an ER random graph in the network net.

The network isn't guaranteed to be a simple graph, but in large sparse networks it is simple with high probability.

By default all nodes and links that the network can accommodate will be used and all nodes will be set to state one. This behavior can be controlled by the following keyword arguments:

  • N : The number of nodes that will be used in the creation of the random graph. All other nodes will be removed from the network.
  • K : The number of links that will be used in the creation of the random graph. All other links will be removed from the network.
  • S : The state of the nodes. All nodes will be set to this state.

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,1,[])
Network of 0 nodes and 0 links

julia> randomgraph!(net)
Network of 1000 nodes and 2000 links

julia> nullgraph!(net)
Network of 0 nodes and 0 links

julia> randomgraph!(net,N=100,K=10)
Network of 100 nodes and 10 links
Fastnet.randomlinkMethod
randomlink(net)
randomlink(net,s)
randomlink_f(net)
randomlink_f(net,s)

Return the id of a random link drawn from net.

If the second argument s is not provided the link will be drawn uniformly from all links in the network. If s is an integer then the link will be drawn uniformly from the links in state s. If s is an Array or Tuple of Ints then the link will be drawn uniformly from the links in the states listed.

This function runs in constant time if s is integer or omitted. If s is an Array or Tuple the worst case performance scales only with the number of tracked link states. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

The safe versions of this function will throw an ArgumentError with an informative error message when trying to pick a link from an empty set. With the fast (_f) version, trying to pick a link from an empty set will also result in an ArgumentError being thrown, but in this case the message will be something like "Range must be non-empty".

See also link

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[LinkType(2,2,2)])
Network of 0 nodes and 0 links

julia> randomgraph!(net);

julia> for i=1:500
         nd=randomnode(net,1)
         nodestate!(net,nd,2)
       end

julia> lnk=randomlink(net,1);

julia> linkstate(net,lnk)
1
Fastnet.randomnodeMethod
randomnode(net)
randomnode(net,s)
randomnode_f(net)
randomnode_f(net,s)

Return the id of a random node drawn from net.

If the second argument s is not provided the node will be drawn uniformly from all nodes in the network. If s is an integer then the node will be drawn uniformly from the nodes in state s. If s is an Array or Tuple of Ints then the node will be drawn uniformly from the nodes in the states listed.

This function runs in constant time if s is integer or omitted. If s is an Array or Tuple the worst case performance scales only with the number of node states. The fast (_f) verions sacrifice some safty checks for better performance. See basic concepts for details.

The safe versions of this function will throw an ArgumentError with an informative error message when trying to pick a node from an empty set. With the fast (_f) version, trying to pick a node from an empty set will also result in an ArgumentError being thrown, but in this case the message will be something like "Range must be non-empty".

See also node

Examples

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> makenodes!(net,100,1);

julia> makenodes!(net,100,2);

julia> nodestate(net,randomnode(net,1))
1
julia> nodestate(net,randomnode(net,2))
2
Fastnet.rectlattice!Method
rectlattice!(net::FastNet,dims, <keyword arguments>)

Create a rectangular lattice with given dimensions dims.

The network in net is replaced with the new topology, that is a lattice specified by dims. dims can be a number, in this case it indicates the number of nodes to be arranged into a 1D lattice. Alternatively, dims can be a vector of Ints. In this case the dimension of the lattice is identical to the length of dims and each element of dims specifies the length of the lattice in one of these dimensions.

If there FastNet is not large enough to accomodate the desired number of nodes or links an argument error will be thrown.

Keyword arguments are

  • periodic : If this argument is true the lattice is generated with periodic boundary conditions in all dimensions. Alternatively a Vector of Bool of the same length as dims can be supplied. In this case the n'th argument of the vector specifies if the lattice is periodic in the n'th dimension.
  • S : The state of the nodes. All nodes will be set to this state.

Examples

julia> using Fastnet

julia>  net=FastNet(2000,6000,2,[])
Network of 0 nodes and 0 links

julia> rectlattice!(net,[10,20,10],periodic=[true,false,true])
Network of 2000 nodes and 5900 links

julia> degreedist(net)
6-element Vector{Float64}:
 0.0
 0.0
 0.0
 0.0
 0.1
 0.9
Fastnet.regulargraph!Method
regulargraph!(net::FastNet,deg;<keyword arguments>)

Create a regular graph with node degree deg.

The network in net is replaced with the new topology in which all nodes have degree deg and are randomly connected. If the number of nodes is not specified the function will try to use all nodes allowed by net.

The network generation is fast and unbiased, but isn't guaranteed to result in a simple graph.

Note that finite regular graphs with odd node degree and odd number of nodes do not exist. Hence either deg or the number of nodes must be even.

If there FastNet is not large enough to accomodate the desired number of links or nodes an argument error will be thrown.

The keyword arguments are

  • N : The number of nodes that will be used in the creation of the network
  • S : The state of the nodes. All nodes will be set to this state.

Examples

julia> using Fastnet

julia>  net=FastNet(1000,2000,2,[])
Network of 0 nodes and 0 links

julia> regulargraph!(net,4)
Network of 1000 nodes and 2000 links

julia> degreedist(net)
4-element Vector{Float64}:
 0.0
 0.0
 0.0
 1.0
Fastnet.resultsMethod

results(FastNet)

Return a refernce to the results of sim as a DataFrame

Example

julia> using Fastnet

julia> net=FastNet(1000,2000,2,[]);

julia> makenodes!(net,12,1)

julia> makenodes!(net,34,2)

julia> sim=FastSim(net,(r,t)->nothing,[])
Simulation run, currently at time 0.0

julia> runsim!(sim,100,25)
Time     Node state 1    Node state 2
  0.0              12              34
 25.0              12              34
 50.0              12              34
 75.0              12              34
100.0              12              34

julia> results(sim)
5×3 DataFrame
 Row │ Time     Node state 1  Node state 2 
     │ Float64  Int64         Int64        
─────┼─────────────────────────────────────
   1 │     0.0            12            34
   2 │    25.0            12            34
   3 │    50.0            12            34
   4 │    75.0            12            34
   5 │   100.0            12            34 
Fastnet.runsim!Function
runsim!(sim,dur,out=1.0)

Run the simulation sim for time dur, producing out at intervals out.

During the simulation the FastSim and its associated FastNet object will be updated to reflect the current state of the network (though see notes on the simulation time, here).

This function simulates the FastSim at least for a certain time. If there are still events occuring in the simulation by the end of the simulation run the simulation will stop directly after the first event that happens after dur. So the simulation time will always be greater than dur. In general the difference and the actual simulation time will be tiny, but in case events are extreley rare the simulation may run significantly beyond dur. This behaviour is necessary to avoid a watchdog-paradox artifact when repeatedly starting short runs.

Output is generated once at the start of the simulation and then at every multiple of out. For example, if the simulation time is t=3.12 at the start of the run, dur=10 and out=5 then outputs will be generated at times 3.12, 5 and 10. As a result the network will be left in a state that differs from the statistics in the last output.

See also FastSim,simstep!

Examples

julia> using Fastnet

julia> net=FastNet(2,1,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,2);

julia> function rates!(r,t)
         r[1]=countnodes(net,2)
         end
rates! (generic function with 1 method)

julia> function simpleproc!()
         node=randomnode(net,2)
         nodestate!(net,node,1)
         end
simpleproc! (generic function with 1 method)

julia> sim=FastSim(net,rates!,[simpleproc!]; output=false)
Simulation run, currently at time 0.0

julia> runsim!(sim,100)

julia> nodecounts(net)
2-element Vector{Int64}:
 2
 0
Fastnet.savelinklistMethod
savelinklist(net,filename)

Save network link list of network net to file filename.

The file is written in text mode. Each line that is written correosponds to one link. The lines have the form

LINKID SOURCE DESTINATION

Where LINKID is the respective link, SOURCE is the node id of the source node and DESTINATION is the node id of the destination node. The elemnts are separated by space. The line is ended by a line feed '\n'.

See also linklist

Fastnet.savenodeinfoMethod
savenodeinfo(net,filename)

Save information about the nodes to file filename.

The file is written in text mode. Each line that is written correosponds to one nodes. The lines have the form

NODEID STATE INDEGREE OUTDEGREE IN-NEIGHBORS OUT-NEIGBORS

Where NODEID is the ID of the node, STATE is the state of the node, in degree is the number of links that have the node as the destination, OUTDEGREE is the number of links that have the node as the source, IN-NEIGHBORS is a list of all nodes from which the focal node receives an incoming link, OUTNEIGHBORS is a list of the IDs of all nodes from which the focal node casts an outgoing link. All elements are separated by spaces The line is ended by a line feed '\n'.

See also linklist

Fastnet.showlinksMethod
showlinks(net)

Print information on all links in FastNet net.

This function is mainly intended for testing/debugging. Use it only for networks with few links.

See also shownodes

Examples

```jldoctest julia> using Fastnet

julia> net=FastNet(1000,2000,2,[LinkType(1,1),LinkType(1,2),LinkType(2,2)]);

julia> makenodes!(net,5,1)

julia> makenodes!(net,5,2)

julia> makelink!(net,node(net,1,1),node(net,1,2)); 1

julia> makelink!(net,node(net,1,1),node(net,2,1)) 2

julia> makelink!(net,node(net,2,1),node(net,2,2)) 3

julia> makelink!(net,node(net,2,2),node(net,1,2)) 4

julia> showlinks(net) id src dest state 1 1 2 1 2 1 6 2 3 6 7 3 4 7 2 2

Fastnet.shownodesMethod
shownodes(net)

Print information on all nodes in FastNet net.

This function is mainly intended for testing/debugging. You might want to think twice before calling it for a large network. Having 10 million nodes printed to your REPL is much less fun than it sounds.

See also showlinks

Examples

```jldoctest julia> using Fastnet

julia> net=FastNet(1000,2000,2,[]);

julia> makenodes!(net,5,1)

julia> makenodes!(net,5,2)

julia> shownodes(net) id state 1 1 2 1 3 1 4 1 5 1 6 2 7 2 8 2 9 2 10 2

Fastnet.simstep!Method
simstep!(sim)

Simulate the next event in sim.

This function will always advance the sim by exactly one event. If no event is possible it will advance time by one timeunit. Output is generated at start time and directly after the event has occured.

See also FastSim,simstep

Examples

julia> using Fastnet

julia> net=FastNet(2,1,2,[])
Network of 0 nodes and 0 links

julia> n1=makenode!(net,1);

julia> n2=makenode!(net,2);

julia> function rates!(r,t)
         r[1]=countnodes(net,2)
         end
rates! (generic function with 1 method)

julia> function simpleproc!()
         node=randomnode(net,2)
         nodestate!(net,node,1)
         end
simpleproc! (generic function with 1 method)

julia> sim=FastSim(net,rates!,[simpleproc!]; output=false)
Simulation run, currently at time 0.0

julia> simstep!(sim)

julia> nodecounts(net)
2-element Vector{Int64}:
 2
 0