Search code examples
connectionnodesomnet++

Connect, repetitively, nodes based at their euclidean distance in omnet++


I have created a random topology in ned file. In that topology, every node has the same radian connection distance. I want to make connections for every node that is in the rc distance.

This is as far I have get:

network ranTop
{
    parameters:
        int n = 100;
        int rc = 5;
        volatile int posX = intuniform (0,100);
        volatile int posY = intuniform (0,100);

    submodules:
        node[n] : Node {
            parameters:
                @display("p=$posX,$posY");
        }

}

Solution

  • You should have (non volatile!) posX, posY parameter for each node and then create a loop over all possible connections and connect the nodes only if the distance is less than range. You can access the node parameters in the loop and in the if condition too:

    simple MeshNode extends Node
    {
        parameters:
            int posX = default(intuniform(0,100));
            int posY = default(intuniform(0,100));
    
            @display("i=misc/node_vs;p=$posX,$posY");
        gates:
            inout g[];
    }
    
    
    //
    // A network of nodes randomly placed where nodes 
    // closer than 'range' are connected.
    //
    network MeshNetwork
    {
        parameters:
            int num @prompt("Number of nodes") = default(100);
            int range = 15;  // nodes closer than this should be connected
    
        submodules:
            node[num] : MeshNode;
    
        connections allowunconnected:
            for i=0..(num-2), for j=(i+1)..(num-1) {
                node[i].g++ <--> node[j].g++ if pow(node[i].posX-node[j].posX, 2)+pow(node[i].posY-node[j].posY, 2) < range*range;
            }
    }