//magnets model

MagnetsModel.java

MagnetSpace.java
MagnetAgent.java
Magnet.java

package magnets2;
//RepastJ -> SymphonyRepast modifications are marked with //SR or /*SR ... */
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import uchicago.src.sim.analysis.*;
import uchicago.src.sim.engine.*;
import uchicago.src.sim.gui.*;
import uchicago.src.sim.util.SimUtilities;

// Referenced classes of package magnets2:
//            MagnetSpace, Magnet

public class MagnetsModel extends SimModelImpl
{
    class FluxInSpace
        implements DataSource, Sequence
    {

        public Object execute()
        {
            return new Double(getSValue());
        }

        public double getSValue()
        {
            return (double)sSpace.getTotalFlux();
        }

        final MagnetsModel this$0;

        FluxInSpace()
        {
            this$0 = MagnetsModel.this;
            super();
        }
    }

    class AgentFlux
        implements BinDataSource
    {

        public double getBinValue(Object o)
        {
            Magnet sa = (Magnet)o;
            return (double)sa.getDirection();
        }

        final MagnetsModel this$0;

        AgentFlux()
        {
            this$0 = MagnetsModel.this;
            super();
        }
    }


    public MagnetsModel()
    {
        numMagnetAgents = 2;
        worldXSize = 43;
        worldYSize = 41;
        totalFlux = 0;
        radius = 1;
        adjacent = true;
    }

    public static void main(String args[])
    {
        SimInit init = new SimInit();
        MagnetsModel model = new MagnetsModel();
        init.loadModel(model, "", false);
    }

    public String[] getInitParam()
    {
        String initParams[] = {
            "NumMagnetAgents", "WorldXSize", "WorldYSize", "TotalFlux", "Radius", "Adjacent"
        };
        return initParams;
    }

    public void setup()
    {
        sSpace = null;
        magnetList = new ArrayList();
        schedule = new Schedule(1.0D);
        if(displaySurf != null)
            displaySurf.dispose();
        displaySurf = null;
        if(amountOfFluxInSpace != null)
            amountOfFluxInSpace.dispose();
        amountOfFluxInSpace = null;
        if(magnetDirectionDistribution != null)
            magnetDirectionDistribution.dispose();
        magnetDirectionDistribution = null;
        displaySurf = new DisplaySurface(this, "Magnet Simulation Model");
        amountOfFluxInSpace = new OpenSequenceGraph("Amount Of Flux In Space", this);
        magnetDirectionDistribution = new OpenHistogram("Magnet Directions", 6, 0L);
        registerDisplaySurface("Magnet Simulation Model", displaySurf);
        registerMediaProducer("Plot", amountOfFluxInSpace);
        setupCustomAction();
    }

    public Schedule getSchedule()
    {
        return schedule;
    }

    public void begin()
    {
        buildModel();
        buildSchedule();
        buildDisplay();
        displaySurf.display();
        amountOfFluxInSpace.display();
        magnetDirectionDistribution.display();
    }

    public void buildModel()
    {
        sSpace = new MagnetSpace(worldXSize, worldYSize, adjacent);
        sSpace.spreadFlux(totalFlux);
        for(int i = 0; i < numMagnetAgents; i++)
            if(addNewMagnetAgent(magnetList))
            {
                ((Magnet)magnetList.get(i)).step(radius);
            } else
            {
                setNumMagnetAgents(getNumMagnetAgents() - 1);
                i--;
            }

    }

    public void buildSchedule()
    {
        class MagnetStep extends BasicAction
        {

            public void execute()
            {
                if(radius < 1)
                    radius = 1;
                if(radius > 2)
                    radius = 2;
                MagnetsModel.tick++;
                if(MagnetsModel.tick % 3 == 0)
                {
                    for(int i = 0; i < numMagnetAgents; i++)
                        addNewMagnetAgent(magnetList);

                }
                SimUtilities.shuffle(magnetList);
                for(int i = 0; i < magnetList.size(); i++)
                {
                    Magnet sa = (Magnet)magnetList.get(i);
                    sa.step(radius);
                }

                displaySurf.updateDisplay();
            }

            final MagnetsModel this$0;

            MagnetStep()
            {
                this$0 = MagnetsModel.this;
                super();
            }
        }

        schedule.scheduleActionBeginning(0.0D, new MagnetStep());
        class UpdateFluxInSpace extends BasicAction
        {

            public void execute()
            {
                amountOfFluxInSpace.step();
            }

            final MagnetsModel this$0;

            UpdateFluxInSpace()
            {
                this$0 = MagnetsModel.this;
                super();
            }
        }

        schedule.scheduleActionAtInterval(1.0D, new UpdateFluxInSpace());
        class SocialUpdateAgentDirection extends BasicAction
        {

            public void execute()
            {
                magnetDirectionDistribution.step();
            }

            final MagnetsModel this$0;

            SocialUpdateAgentDirection()
            {
                this$0 = MagnetsModel.this;
                super();
            }
        }

        schedule.scheduleActionAtInterval(1.0D, new SocialUpdateAgentDirection());
    }

    public void buildDisplay()
    {
        ColorMap map = new ColorMap();
        for(int i = 1; i < 17; i++)
            map.mapColor(-i, new Color(256 - i * 16, 127, 0));

        for(int i = 1; i < 17; i++)
            map.mapColor(i, new Color(0, 127, 256 - i * 16));

        map.mapColor(0, Color.white);
        Value2DHexaDisplay displayGrass = new Value2DHexaDisplay(sSpace.getCurrentFluxSpace(), map);
        Object2DHexaDisplay displayAgents = new Object2DHexaDisplay(sSpace.getCurrentMagnetAgentSpace());
        displayAgents.setObjectList(magnetList);
        displaySurf.addDisplayable(displayGrass, "Flux");
        displaySurf.addDisplayable(displayAgents, "Magnets");
        amountOfFluxInSpace.addSequence("Total Flux", new FluxInSpace());
        magnetDirectionDistribution.createHistogramItem("Magnet directions", magnetList, new AgentFlux(), 1, 1);
    }

    private boolean addNewMagnetAgent(ArrayList agentlist)
    {
        boolean retval = false;
        Magnet a = new Magnet(radius);
        if(sSpace.addAgent(a))
        {
            agentlist.add(a);
            retval = true;
        }
        return retval;
    }

    private void setupCustomAction()
    {
        modelManipulator.init();
        modelManipulator.addButton("Add Magnets", new ActionListener() {

            public void actionPerformed(ActionEvent evt)
            {
                for(int i = 0; i < numMagnetAgents; i++)
                    addNewMagnetAgent(magnetList);

            }

            final MagnetsModel this$0;

            
            {
                this$0 = MagnetsModel.this;
                super();
            }
        }
);
        modelManipulator.addButton("Remove Magnets", new ActionListener() {

            public void actionPerformed(ActionEvent evt)
            {
                if(magnetList.size() > 0)
                {
                    for(int i = 1; i < magnetList.size(); i++)
                    {
                        Magnet sa = (Magnet)magnetList.get(i);
                        sSpace.removeAgentAt(sa.getX(), sa.getY());
                        magnetList.remove(i);
                        sSpace.updateFluxRing(sa.getX(), sa.getY(), sa.getDirection(), true);
                    }

                }
            }

            final MagnetsModel this$0;

            
            {
                this$0 = MagnetsModel.this;
                super();
            }
        }
);
    }

    public String getName()
    {
        return "Magnets";
    }

    public int getNumMagnetAgents()
    {
        return numMagnetAgents;
    }

    public void setNumMagnetAgents(int na)
    {
        numMagnetAgents = na;
    }

    public int getWorldXSize()
    {
        return worldXSize;
    }

    public void setWorldXSize(int wxs)
    {
        worldXSize = wxs;
    }

    public int getWorldYSize()
    {
        return worldYSize;
    }

    public void setWorldYSize(int wys)
    {
        worldYSize = wys;
    }

    public int getTotalFlux()
    {
        return totalFlux;
    }

    public void setTotalFlux(int i)
    {
        totalFlux = i;
    }

    public int getRadius()
    {
        return radius;
    }

    public void setRadius(int radius)
    {
        this.radius = radius;
    }

    public boolean isAdjacent()
    {
        return adjacent;
    }

    public void setAdjacent(boolean adjacent)
    {
        this.adjacent = adjacent;
    }

    private static final long serialVersionUID = 1L;
    private static final int NUMAGENTS = 2;
    private static final int WORLDXSIZE = 43;
    private static final int WORLDYSIZE = 41;
    private static final int TOTALFLUX = 0;
    private static int tick = 0;
    private int numMagnetAgents;
    private int worldXSize;
    private int worldYSize;
    private int totalFlux;
    private int radius;
    private boolean adjacent;
    private Schedule schedule;
    private MagnetSpace sSpace;
    private ArrayList magnetList;
    private DisplaySurface displaySurf;
    private OpenSequenceGraph amountOfFluxInSpace;
    private OpenHistogram magnetDirectionDistribution;












}
//