From b61f6cd585580e02f6a07db9631a37974b00b7d3 Mon Sep 17 00:00:00 2001 From: baco Date: Fri, 1 Apr 2022 16:11:34 +0200 Subject: [PATCH] Fix for agent spawners not completing spine function initialization --- src/agent/AgentHelperMethods.java | 56 +++++++++++++++++++ src/agent/Body.java | 1 + .../agentStaging/DistributedSpawner.java | 2 + .../agentStaging/RandomSpawner.java | 2 + src/idynomics/Global.java | 8 +++ .../library/AgentRelaxation.java | 15 ++--- 6 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 src/agent/AgentHelperMethods.java diff --git a/src/agent/AgentHelperMethods.java b/src/agent/AgentHelperMethods.java new file mode 100644 index 000000000..4017fbde5 --- /dev/null +++ b/src/agent/AgentHelperMethods.java @@ -0,0 +1,56 @@ +package agent; + +import dataIO.Log; +import expression.Expression; +import idynomics.Global; +import idynomics.Idynomics; +import referenceLibrary.AspectRef; +import surface.link.LinearSpring; +import surface.link.Spring; +import surface.link.TorsionSpring; +import utility.Helper; + +public class AgentHelperMethods { + public static void springInitialization(Agent a) + { + Body b = (Body) a.get(AspectRef.agentBody); + for( Spring s : b.getSpringsToEvaluate()) + { + if( s != null ) + { + if( !s.ready()) + { + s.setStiffness( Helper.setIfNone( a.getDouble(AspectRef.spineStiffness), + 1e6)); + //TODO warn user if not set + if( s instanceof LinearSpring) + { + Expression spineFun; + if ( !Helper.isNullOrEmpty( a.getValue( + AspectRef.agentSpineFunction ))) + spineFun = new Expression((String) + a.getValue(AspectRef.agentSpineFunction )); + else + spineFun = Global.fallback_spinefunction; + s.setSpringFunction( spineFun ); + } + else if( s instanceof TorsionSpring) + { + Expression torsFun = null; + if ( !Helper.isNullOrEmpty( + a.getValue(AspectRef.torsionFunction))) + torsFun = (Expression) + a.getValue(AspectRef.torsionFunction); + else + { + /* TODO set default maybe? */ + Idynomics.simulator.interupt( + "missing torsion spring function in relax"); + } + s.setSpringFunction( torsFun ); + } + } + } + } + } +} diff --git a/src/agent/Body.java b/src/agent/Body.java index 9155f12a3..66caf3db3 100644 --- a/src/agent/Body.java +++ b/src/agent/Body.java @@ -230,6 +230,7 @@ public Body(Morphology morphology, double[] positionA, double[] positionB, default: break; } + this.constructBody(); } /** diff --git a/src/compartment/agentStaging/DistributedSpawner.java b/src/compartment/agentStaging/DistributedSpawner.java index dff9fb540..12b805beb 100644 --- a/src/compartment/agentStaging/DistributedSpawner.java +++ b/src/compartment/agentStaging/DistributedSpawner.java @@ -2,6 +2,7 @@ import java.util.LinkedList; +import agent.AgentHelperMethods; import org.w3c.dom.Element; import agent.Agent; @@ -120,6 +121,7 @@ private void spawnAgent(double[] location) newAgent.set(AspectRef.agentBody, new Body( this.getMorphology(), location, 0.0, 0.0 ) ); newAgent.setCompartment( this.getCompartment() ); + AgentHelperMethods.springInitialization(newAgent); newAgent.registerBirth(); } } diff --git a/src/compartment/agentStaging/RandomSpawner.java b/src/compartment/agentStaging/RandomSpawner.java index d72af4038..a1b11d0f6 100644 --- a/src/compartment/agentStaging/RandomSpawner.java +++ b/src/compartment/agentStaging/RandomSpawner.java @@ -1,5 +1,6 @@ package compartment.agentStaging; +import agent.AgentHelperMethods; import org.w3c.dom.Element; import agent.Agent; @@ -32,6 +33,7 @@ public void spawn() newRandom.set(AspectRef.agentBody, new Body( this.getMorphology(), this.getSpawnDomain() )); newRandom.setCompartment( this.getCompartment() ); + AgentHelperMethods.springInitialization(newRandom); newRandom.registerBirth(); } } diff --git a/src/idynomics/Global.java b/src/idynomics/Global.java index 902761a0a..b4ed963a9 100644 --- a/src/idynomics/Global.java +++ b/src/idynomics/Global.java @@ -4,6 +4,7 @@ import java.text.SimpleDateFormat; import java.util.Date; +import expression.Expression; import org.w3c.dom.Element; import dataIO.Log; @@ -382,4 +383,11 @@ public void updateSettings() * Number of digits (including leading zeros) in file numbering */ public static int file_number_of_digits = 5; + + /** + * fall back spine function for if nothing is set TODO it may be a better option to just + * warn and instruct the user, function might not be the best as a default + */ + public static Expression fallback_spinefunction = + new Expression( "stiffness * ( dh + SIGN(dh) * dh * dh * 100.0 )" ); } diff --git a/src/processManager/library/AgentRelaxation.java b/src/processManager/library/AgentRelaxation.java index 8ddf236df..641b40c76 100644 --- a/src/processManager/library/AgentRelaxation.java +++ b/src/processManager/library/AgentRelaxation.java @@ -177,13 +177,13 @@ private enum Method * limit duration of biofilm compression */ private double compresionDuration = 0.0; - + /** * TODO check whether implementation is finished * Default spine function, fall back for if none is defined by the agent. */ - private Expression _spineFunction = - new Expression( "stiffness * ( dh + SIGN(dh) * dh * dh * 100.0 )" ); + //private Expression _spineFunction = + // new Expression( "stiffness * ( dh + SIGN(dh) * dh * dh * 100.0 )" ); private Boolean _decompression; @@ -274,13 +274,6 @@ public void init(Element xmlElem, EnvironmentContainer environment, this.compresionDuration = Helper.setIfNone( this.getDouble(COMPRESSION_DURATION), 0.0 ); - /* Set default spine function for rod type agents, this function is - * used if it is not overwritten by the agent, obtain - * ComponentExpression from process manager otherwise fall back default - * is used. */ - if ( ! Helper.isNullOrEmpty( this.getValue(SPINE_FUNCTION) ) ) - this._spineFunction = new Expression((String) this.getValue(SPINE_FUNCTION)); - /* Include decompression */ this._decompression = Helper.setIfNone( this.getBoolean(DECOMPRESSION), false); @@ -609,7 +602,7 @@ private void springEvaluation(Agent a, Body b) spineFun = new Expression((String) a.getValue(AspectRef.agentSpineFunction )); else - spineFun = this._spineFunction; + spineFun = Global.fallback_spinefunction; s.setSpringFunction( spineFun ); } else if( s instanceof TorsionSpring )