Search in sources :

Example 21 with EquationSet

use of gov.sandia.n2a.eqset.EquationSet in project n2a by frothga.

the class ExportJob method analyze.

/**
 *        Find references to $index in connection endpoints, and set up info for ConnectionContext.
 */
public void analyze(EquationSet s) {
    for (EquationSet p : s.parts) analyze(p);
    for (final Variable v : s.variables) {
        Visitor visitor = new Visitor() {

            public boolean visit(Operator op) {
                if (op instanceof AccessVariable) {
                    VariableReference r = ((AccessVariable) op).reference;
                    Variable rv = r.variable;
                    if (rv.container != v.container && !r.resolution.isEmpty()) {
                        Object o = r.resolution.get(r.resolution.size() - 1);
                        if (o instanceof ConnectionBinding) {
                            ConnectionBinding c = (ConnectionBinding) o;
                            // This is somewhat of a hack, but ConnectionContext assumes the mappings A->0 and B->1.
                            if (c.alias.equals("A"))
                                r.index = 0;
                            if (c.alias.equals("B"))
                                r.index = 1;
                        }
                    }
                }
                return true;
            }
        };
        v.visit(visitor);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Visitor(gov.sandia.n2a.language.Visitor) AccessVariable(gov.sandia.n2a.language.AccessVariable) VariableReference(gov.sandia.n2a.eqset.VariableReference) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)

Example 22 with EquationSet

use of gov.sandia.n2a.eqset.EquationSet in project n2a by frothga.

the class ExportJob method process.

public void process(MPart source) {
    if (source.get("$metadata", "backend.lems.part").isEmpty()) {
        for (EquationSet p : equations.parts) topLevelPart((MPart) p.source);
    } else {
        topLevelPart(source);
    }
    for (IonChannel ic : channels) ic.append();
    for (Synapse s : synapses) s.append();
    // Collate
    Element root = doc.createElement("neuroml");
    root.setAttribute("id", modelName);
    root.setAttribute("xmlns", "http://www.neuroml.org/schema/neuroml2");
    root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    root.setAttribute("xsi:schemaLocation", "http://www.neuroml.org/schema/neuroml2 ../Schemas/NeuroML2/NeuroML_v2beta4.xsd");
    sequencer.append(root, elements);
    doc.appendChild(root);
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet) MPart(gov.sandia.n2a.eqset.MPart) Element(org.w3c.dom.Element)

Example 23 with EquationSet

use of gov.sandia.n2a.eqset.EquationSet in project n2a by frothga.

the class ExportJob method input.

public String input(MPart source, List<Element> parentElements, Synapse synapse) {
    String type = source.get("$metadata", "backend.lems.part");
    List<Element> inputElements = new ArrayList<Element>();
    List<String> skip = new ArrayList<String>();
    for (MNode c : source) {
        MPart p = (MPart) c;
        if (p.isPart()) {
            skip.add(p.key());
            input(p, inputElements, null);
        } else if (p.key().equals("times")) {
            skip.add("times");
            String[] pieces = p.get().split("\\[", 2)[1].split("]", 2)[0].split(";");
            for (int i = 0; i < pieces.length; i++) {
                if (Scalar.convert(pieces[i]) == Double.POSITIVE_INFINITY)
                    continue;
                Element spike = addElement("spike", inputElements);
                spike.setAttribute("id", String.valueOf(i));
                spike.setAttribute("time", biophysicalUnits(pieces[i]));
            }
        }
    }
    if (// decide between them
    type.contains("ramp") && type.contains("pulse")) {
        NameMap nameMap = partMap.importMap("rampGenerator");
        EquationSet sourceEquations = getEquations(source);
        Variable high1 = sourceEquations.find(new Variable(nameMap.importName("startAmplitude"), 0));
        Variable high2 = sourceEquations.find(new Variable(nameMap.importName("finishAmplitude"), 0));
        try {
            double value1 = ((Scalar) high1.eval(context)).value;
            double value2 = ((Scalar) high2.eval(context)).value;
            if (value1 == value2)
                type = "pulseGenerator";
            else
                type = "rampGenerator";
        } catch (// eval can fail if eqset contains fatal errors
        Exception e) {
            if (source.get("high2").equals("high1"))
                type = "pulseGenerator";
            else
                type = "rampGenerator";
        }
    } else if (// more action is needed
    type.contains(",")) {
        if (synapse != null) {
            String[] types = type.split(",");
            for (String t : types) {
                if (// prefix of both "Synapse" and "Synaptic"
                t.contains("Synap")) {
                    type = t;
                    break;
                }
            }
        }
        // remove any remaining ambiguity
        type = type.split(",")[0];
    }
    Element input = addElement(type, parentElements);
    String id;
    if (synapse == null) {
        id = source.get("$metadata", "backend.lems.id");
        if (id.isEmpty())
            id = source.key();
    } else {
        id = synapse.id;
        input.setAttribute("synapse", synapse.chainID);
        input.setAttribute("spikeTarget", "./" + synapse.chainID);
    }
    input.setAttribute("id", id);
    standalone(source, input, inputElements);
    genericPart(source, input, skip.toArray(new String[] {}));
    sequencer.append(input, inputElements);
    return id;
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet) MPart(gov.sandia.n2a.eqset.MPart) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) NameMap(gov.sandia.n2a.backend.neuroml.PartMap.NameMap) MNode(gov.sandia.n2a.db.MNode) IncommensurableException(javax.measure.IncommensurableException) UnconvertibleException(javax.measure.UnconvertibleException) ParseException(gov.sandia.n2a.language.ParseException) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 24 with EquationSet

use of gov.sandia.n2a.eqset.EquationSet in project n2a by frothga.

the class ExportJob method genericPart.

public void genericPart(MPart part, Element result, String... skip) {
    EquationSet partEquations = getEquations(part);
    List<Element> resultElements = new ArrayList<Element>();
    List<String> skipList = Arrays.asList(skip);
    for (MNode c : part) {
        MPart p = (MPart) c;
        String key = p.key();
        // Skip connection bindings. They are unpacked elsewhere.
        if (partEquations.findConnection(key) != null)
            continue;
        if (key.startsWith("$"))
            continue;
        if (skipList.contains(key))
            continue;
        if (p.isPart()) {
            genericPart(p, resultElements);
        } else {
            // We need to check two things:
            // * Has the variable been overridden after it was declared in the base part?
            // * Is it an expression or a constant?
            // An override that is an expression should trigger a LEMS extension part.
            // A constant that is either overridden or required should be emitted here.
            boolean expression = true;
            String value = p.get();
            Variable v = partEquations.find(new Variable(key));
            try {
                // This could convert an expression to a constant.
                Type evalue = v.eval(context);
                // TODO: if it is a simple AccessVariable, then it shouldn't be viewed as an expression.
                Operator op = Operator.parse(value);
                if (op instanceof Constant) {
                    // "direct" value
                    Type dvalue = ((Constant) op).value;
                    expression = !evalue.equals(dvalue);
                }
            } catch (Exception e) {
            }
            boolean overridden = p.isFromTopDocument() || isOverride(part.get("$inherit").replace("\"", ""), key);
            String name = sequencer.bestFieldName(result, p.get("$metadata", "backend.lems.param"));
            if (name.isEmpty())
                name = key;
            boolean required = sequencer.isRequired(result, name);
            if (required || (overridden && !expression)) {
                // biophysicalUnits() should return strings (non-numbers) unmodified
                result.setAttribute(name, biophysicalUnits(p.get()));
            }
        }
    }
    sequencer.append(result, resultElements);
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) MPart(gov.sandia.n2a.eqset.MPart) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Constant(gov.sandia.n2a.language.Constant) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MNode(gov.sandia.n2a.db.MNode) IncommensurableException(javax.measure.IncommensurableException) UnconvertibleException(javax.measure.UnconvertibleException) ParseException(gov.sandia.n2a.language.ParseException) Type(gov.sandia.n2a.language.Type)

Example 25 with EquationSet

use of gov.sandia.n2a.eqset.EquationSet in project n2a by frothga.

the class XyceBackend method analyze.

public void analyze(EquationSet s) {
    for (EquationSet p : s.parts) analyze(p);
    XyceBackendData bed = new XyceBackendData();
    bed.internal = (InternalBackendData) s.backendData;
    s.backendData = bed;
    bed.analyze(s);
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet)

Aggregations

EquationSet (gov.sandia.n2a.eqset.EquationSet)34 Variable (gov.sandia.n2a.eqset.Variable)10 AccessVariable (gov.sandia.n2a.language.AccessVariable)9 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)7 ArrayList (java.util.ArrayList)7 Operator (gov.sandia.n2a.language.Operator)6 Scalar (gov.sandia.n2a.language.type.Scalar)6 MPart (gov.sandia.n2a.eqset.MPart)5 VariableReference (gov.sandia.n2a.eqset.VariableReference)4 Constant (gov.sandia.n2a.language.Constant)4 Type (gov.sandia.n2a.language.Type)4 Visitor (gov.sandia.n2a.language.Visitor)4 TreeSet (java.util.TreeSet)4 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)3 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)3 MNode (gov.sandia.n2a.db.MNode)3 ParseException (gov.sandia.n2a.language.ParseException)3 Text (gov.sandia.n2a.language.type.Text)3 AbortRun (gov.sandia.n2a.plugins.extpoints.Backend.AbortRun)3 FileNotFoundException (java.io.FileNotFoundException)3