Skip to content

Commit b7e9875

Browse files
committed
allow per element update and tests
1 parent 6f18313 commit b7e9875

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

src/services/DFGVariable.jl

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,36 +362,60 @@ addVariableSolverData!(dfg::AbstractDFG, sourceVariable::DFGVariable, solvekey::
362362
$(SIGNATURES)
363363
Update variable solver data if it exists, otherwise add it.
364364
Notes:
365-
- `useDeepcopy=true` to copy solver data and keep separate memory.
365+
- `useCopy=true` to copy solver data and keep separate memory.
366366
"""
367367
function updateVariableSolverData!(dfg::AbstractDFG,
368368
variablekey::Symbol,
369369
vnd::VariableNodeData,
370370
solvekey::Symbol=:default,
371-
useDeepcopy::Bool=true )::VariableNodeData
371+
useCopy::Bool=true,
372+
fields::Vector{Symbol}=Symbol[] )::VariableNodeData
372373
#
373374
#This is basically just setSolverData
374375
var = getVariable(dfg, variablekey)
375376
if !haskey(var.solverDataDict, solvekey)
376377
@warn "VariableNodeData '$(solvekey)' does not exist, adding"
377378
end
378-
#for InMemoryDFGTypes, cloud would update here
379-
var.solverDataDict[solvekey] = useDeepcopy ? deepcopy(vnd) : vnd
380-
return vnd
379+
380+
# for InMemoryDFGTypes do memory copy or repointing, for cloud this would be an different kind of update.
381+
usevnd = useCopy ? deepcopy(vnd) : vnd
382+
# should just one, or many pointers be updated?
383+
if haskey(var.solverDataDict, solvekey) && isa(var.solverDataDict[solvekey], VariableNodeData) && length(fields) != 0
384+
# change multiple pointers inside the VND var.solverDataDict[solvekey]
385+
for field in fields
386+
destField = getfield(var.solverDataDict[solvekey], field)
387+
srcField = getfield(usevnd, field)
388+
if isa(destField, Array)
389+
# use broadcast (in-place operation)
390+
destField .= srcField
391+
else
392+
# change pointer of destination VND object member
393+
setfield!(var.solverDataDict[solvekey], field, srcField)
394+
end
395+
end
396+
else
397+
# change a single pointer in var.solverDataDict
398+
var.solverDataDict[solvekey] = usevnd
399+
end
400+
401+
return var.solverDataDict[solvekey]
381402
end
382403

383404
updateVariableSolverData!(dfg::AbstractDFG,
384405
sourceVariable::DFGVariable,
385-
solvekey::Symbol=:default, useDeepcopy::Bool=true) =
386-
updateVariableSolverData!(dfg, sourceVariable.label, getSolverData(sourceVariable, solvekey), solvekey, useDeepcopy)
406+
solvekey::Symbol=:default,
407+
useCopy::Bool=true,
408+
fields::Vector{Symbol}=Symbol[] ) =
409+
updateVariableSolverData!(dfg, sourceVariable.label, getSolverData(sourceVariable, solvekey), solvekey, useCopy, fields)
387410

388411
function updateVariableSolverData!(dfg::AbstractDFG,
389412
sourceVariables::Vector{<:DFGVariable},
390413
solvekey::Symbol=:default,
391-
useDeepcopy::Bool=true )
414+
useCopy::Bool=true,
415+
fields::Vector{Symbol}=Symbol[] )
392416
#I think cloud would do this in bulk for speed
393417
for var in sourceVariables
394-
updateVariableSolverData!(dfg, var.label, getSolverData(var, solvekey), solvekey, useDeepcopy)
418+
updateVariableSolverData!(dfg, var.label, getSolverData(var, solvekey), solvekey, useCopy, fields)
395419
end
396420
end
397421

test/iifInterfaceTests.jl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,6 @@ end
281281
#make a copy and simulate external changes
282282
newvar = deepcopy(var1)
283283
getVariablePPEDict(newvar)[:default] = MeanMaxPPE(:default, [150.0], [100.0], [50.0])
284-
#update
285-
# mergeUpdateVariableSolverData!(dfg, newvar)
286-
# mergeVariableSolverData!(getVariable(dfg,getLabel(newvar)), newvar)
287-
# mergeVariableData!(getVariable(dfg,getLabel(newvar)), newvar)
288284
mergeVariableData!(dfg, newvar)
289285

290286
#Check if variable is updated
@@ -297,8 +293,6 @@ end
297293
# Confirm they're different
298294
@test getVariablePPEDict(newvar) != getVariablePPEDict(var1)
299295
# Persist it.
300-
# mergeUpdateVariableSolverData!(dfg, newvar)
301-
# mergeVariableSolverData!(getVariable(dfg, getLabel(newvar)), newvar)
302296
mergeVariableData!(dfg, newvar)
303297
# Get the latest
304298
var1 = getVariable(dfg, :a)
@@ -313,8 +307,7 @@ end
313307
#confirm delete
314308
@test symdiff(collect(keys(getVariablePPEDict(newvar))), [:second]) == Symbol[]
315309
# Persist it., and test
316-
mergeVariableData!(dfg, newvar)
317-
# mergeUpdateVariableSolverData!(dfg, newvar) #357 #358
310+
mergeVariableData!(dfg, newvar) #357 #358
318311

319312
# Get the latest and confirm they're the same, :second
320313
var1 = getVariable(dfg, :a)

test/testBlocks.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,26 @@ function VSDTestBlock!(fg, v1)
568568

569569
# Update update it
570570
@test updateVariableSolverData!(fg, :a, vnd, :parametric) == vnd
571+
# test without deepcopy
572+
@test updateVariableSolverData!(fg, :a, vnd, :parametric, false) == vnd
571573
# Bulk copy update x0
572574
@test updateVariableSolverData!(fg, [v1], :default) == nothing
573575

576+
altVnd = deepcopy(vnd)
577+
altVnd.inferdim = -99.0
578+
retVnd = updateVariableSolverData!(fg, :a, altVnd, :parametric, false, [:inferdim;])
579+
@test retVnd == altVnd
580+
581+
fill!(altVnd.bw, -1.0)
582+
retVnd = updateVariableSolverData!(fg, :a, altVnd, :parametric, false, [:bw;])
583+
@test retVnd == altVnd
584+
585+
altVnd.inferdim = -98.0
586+
@test retVnd != altVnd
587+
588+
# restore without copy
589+
@test updateVariableSolverData!(fg, :a, vnd, :parametric, false, [:inferdim;]) == vnd
590+
574591
# Delete parametric from v1
575592
@test deleteVariableSolverData!(fg, :a, :parametric) == vnd
576593

0 commit comments

Comments
 (0)