Function Reference

Constructors

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.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

Network Setup

Fastnet.nullgraph!Function
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.randomgraph!Function
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

Node Functions

Fastnet.adjacentFunction
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.countnodesFunction
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.degreeFunction
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.destroynode!Function
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.firstlinkinFunction
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.firstlinkoutFunction
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.indegreeFunction
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.makenode!Function
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!Function
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.nodeFunction
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.nodecountsFunction
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.nodeexistsFunction
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.nodestateFunction
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.nodestate!Function
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.outdegreeFunction
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.randomnodeFunction
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.countlinksFunction
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.destroylink!Function
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.makelink!Function
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.nextlinkinFunction
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.nextlinkoutFunction
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.linkFunction

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.linkcountsFunction
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.linkdstFunction
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.linkexistsFunction
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.linksrcFunction
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.linkstateFunction
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.randomlinkFunction
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

Simulation

Fastnet.simstep!Function
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
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

Debug & Analysis

Fastnet.healthcheckFunction
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.listnodesFunction
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.listneighborsFunction
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> 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.resultsFunction

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.showlinksFunction
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

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.shownodesFunction
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

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