Folio-Epicenter example (Julia + uibuilder) - julia

I'm trying to get the basic Forio Epicenter example from the documentation to work, using Julia and the UI Builder. It looks like this:
#in MyModel.jl
module MyModel
# use the Epicenter Julia package
using Epicenter
# expose methods you want to call
# (e.g. using the Run API or Model Operation API,
# or the Model Operation property of a component in UI Builder)
export doubleIt
# expose variables you want to view or update
# (e.g. using the Run API or Model Variable API,
# or the Model Variable property of a component in UI Builder)
export parameters, result
# make exposed variables global
global parameters, result
# define variables that the end user will change as part of a complex type
type ParametersType
input1
input2
function ParametersType()
item = new()
item
end
end
function doubleIt()
global parameters
global result
result = parameters.input1 * 2
record(:result)
end
# other files in your model
include("Utils.jl")
include("OtherModelCalculations.jl")
end
I can't get any variables to connect. When I make a "Text Input" component and change the "Model Variable" property to match the Julia variable (exposed via "export" and "global"), loading the page gives me
400 Bad Request
Full JSON response:
{"status":400,
"errors":{"status":400,
"internal":{
"procedure":"push!(LOAD_PATH, string(ENV[\"HOME\"],
\"\/model\/\")); import Epicenter; using Juliet; using EpicenterSystem; EpicenterSystem.setup_idle_timeout
(1800); require(\"MyModel.jl\"); using MyModel","sessionId":"c5b8189b-4f4d-4377-87a9-ca8d154863c0","trace":""}
},
"message":"Bad Request."
}
I've tried every combination of the "Model Variable" I can think of, including "input1", "parameters", and "parameters.input1"; always comes back with a 400 error.

Forio Support here.
Two problems
a) since "input1" is a field in of variable parameters (type ParameterTypes), it must be referred to in the interface as "parameters.input1"
b) More importantly, the model attempts to include Utils.jl and OtherModelCalculations.jl, neither of which exist. Thus the model never loads.

Related

OpenMDAO Optional Error on Unconnected Input

Is there any way to force OpenMDAO to raise an error if a given input is unconnected? I know for many inputs, defaults can be provided such that the input doesn't need to be connected, however is there a way to tell OpenMDAO to automatically raise an error if certain key inputs are unconnected?
This is not built into OpenMDAO, as of V3.17. However, it is possible to do it. The only caveat is that i had to use some non public APIs to make it work (notice the use of the p.model._conn_global_abs_in2out). So those APIs are subject to changer later.
This code should give you the behavior your want. You could augment things with the use of variable tagging if you wanted a solution that didn't require you to give a list of variable names to the validate function. The list_inputs method can accept tags to filter by instead if you prefer that.
import openmdao.api as om
def validate_connections(prob, force_connected):
# make sure its a set and not a generic iterator (i.e. array)
force_connected_set = set(force_connected)
model_inputs = prob.model.list_inputs(out_stream=None, prom_name=True)
#gets the promoted names from the list of inputs
input_set = set([inp[1]['prom_name'] for inp in model_inputs])
# filter the inputs into connected and unconnected sets
connect_dict = p.model._conn_global_abs_in2out
unconnected_inputs = set()
connected_inputs = set()
for abs_name, in_data in model_inputs:
if abs_name in connect_dict and (not 'auto_ivc' in connect_dict[abs_name]):
connected_inputs.add(in_data['prom_name'])
else:
unconnected_inputs.add(in_data['prom_name'])
# now we need to check if there are any unconnected inputs
# in the model that aren't in the spec
illegal_unconnected = force_connected_set.intersection(unconnected_inputs)
if len(illegal_unconnected) > 0:
raise ValueError(f'unconnected inputs {illegal_unconnected} are are not allowed')
p = om.Problem()
###############################################################################################
# comment and uncomment these three lines to change the error you get from the validate method
###############################################################################################
# p.model.add_subsystem('c0', om.ExecComp('x=3*a'), promotes_outputs=['x'])
# p.model.add_subsystem('c1', om.ExecComp('b=a+17'))
# p.model.connect('c1.b', 'c2.b')
p.model.add_subsystem('c2', om.ExecComp('y=2*x+b'), promotes_inputs=['x'])
p.model.add_subsystem('c3', om.ExecComp('z=x**2+y'))
p.setup()
p.final_setup()
If this is a feature you think should be added to OpenMDAO proper, then feel free to submit a POEM proposing how a formal feature and its API might look.

Xcos throws "Undefined variable: scifunc_block_m" message in console

When I run a Xcos model containing a scifunc_block_m block like shown below
I get an error message relating to data dimensions inconsistency:
"Data dimensions are inconsistent:"
" Variable size=[1,1]"
"Block output size=[100,1]."
But when I double click in the block in order to see what can I change to make the dimensions correct I get a message in the console saying
Undefined variable: scifunc_block_m
What bugs me is that scifunc_block_m is not the name of any variable, but rather the name of the block itself like can be seen in the official docs.
Of course I double checked that nowhere in my function phase_shifter neither anywhere else I have any variable named like that.
I tried with Scilab 6.1.1 and 6.1.0 believing that it might be a bug from apparently not.
In your phase_shifter.sce file generating the input variable,
the signalIn variable does not comply with the From Workspace block requirements, whose documentation says that the input variable
must be a structure with time and values fields
.time must be a column vector, and in your case
.values must also be a column
So,
t = (0:1/fs:Npp/fs - 1/fs); // time vector
signalIn = A*%e^(%i*w*t);
should be replaced with
t = (0:1/fs:Npp/fs - 1/fs)'; // time column vector
signalIn = struct("time",t, "values",A*%e^(%i*w*t));
This fixes the inconsistent dimensions message.
In addition, i am not able to reproduce your issue about Undefined variable: scifunc_block_m. The parameters interface opens as expected.
You may get this kind of messages if you try to run some xcos parts out of xcos, without beforehand loading xcos-related libraries.
Then, we get an unclear "Output should be of complex type." message on the From workspace block.
By the way, you try to plot some complex values. Please have a look to the MATMAGPHI block before entering MUX: https://help.scilab.org/docs/6.1.1/en_US/MATMAGPHI.html

how to get list of Auto-IVC component output names

I'm switching over to using the Auto-IVC component as opposed to the IndepVar component. I'd like to be able to get a list of the promoted output names of the Auto-IVC component, so I can then use them to go and pull the appropriate value out of a configuration file and set the values that way. This will get rid of some boilerplate.
p.model._auto_ivc.list_outputs()
returns an empty list. It seems that p.model__dict__ has this information encoded in it, but I don't know exactly what is going on there so I am wondering if there is an easier way to do it.
To avoid confusion from future readers, I assume you meant that you wanted the promoted input names for the variables connected to the auto_ivc outputs.
We don't have a built-in function to do this, but you could do it with a bit of code like this:
seen = set()
for n in p.model._inputs:
src = p.model.get_source(n)
if src.startswith('_auto_ivc.') and src not in seen:
print(src, p.model._var_allprocs_abs2prom['input'][n])
seen.add(src)
assuming 'p' is the name of your Problem instance.
The code above just prints each auto_ivc output name followed by the promoted input it's connected to.
Here's an example of the output when run on one of our simple test cases:
_auto_ivc.v0 par.x

How to call "public void" method through rJava

In testing a Java API, I need to change a default setting. According to the API's document, it should be done using a method defined within the class using "public void setType". Suppose the class name is 'Node', which is referred using
library(rJava)
.jinit(classpath=jarPath)
Node <- J("Node")
In an Java example from its documents, it's called as
Node nodeX = new Node("X", new Variable[]{x});
nodeX.setType(Type.TEMP);
The default type of nodeX is 'CONTEMP'. How the "setType" method can be called in R through rJava to change its default value to another one? Let's assume 'Type' is an enum variable which has several options, including "CONTEMP","TEMP", etc.
I think you want
library(rJava)
.jinit(classpath=jarPath)
variable <- .jarray(new(J("package.name.Variable", input_arg))
Node <- new(J("package.name.Node"), variable)
Then you can do
type <- J("package.name.Type")$TEMP
Node$setType(type)

Show the euro simbol in a shiny R application

I'm trying to create a shiny R application. I have some troubles to show euro symbol (and return it) in a radio button. I've tried different version of code:
library(shiny)
runApp(list(
ui= navbarPage(title = 'shoe euro',
radioButtons('var', 'var',
c("INCOME_MGL",
"INCOME_MGL€",
"INCOME_MGL€",
"INCOME_MGL€",
"INCOME_MGL\u20ac")
)),
server=function(input, output, session) {
}))
But the "€" doesn't appear in the web page. Moreover if I select the second option the page returns an error:
"Error in fromJSON(content, handler, default.size, depth, allowComments, :
invalid JSON input"
The problem lies in the class shiny-options-group in the div function. The way this class works appears to convert & to &, preventing the browser from converting € into € because it first changes it into &#8364;. Try the following as your ui.R to see it happen.
library(shiny)
options = as.list(c("a","b",HTML("€"),"€"))
shinyUI(fluidPage("test",fluidRow(div(class = "shiny-options-group", options))))
The reason this is happening in your radio button widget is that radioButtons calls generateOptions to create the list of options, and generateOptions returns div(class = "shiny-options-group", options), where options is derived from a manipulation of the choices parameter in the radioButtons function. Here is what you could do:
Get the code for generateOptions here (line 653).
Define the new function generateOptions2 using the exact same code as generateOptions, but change the class parameter at the end.
Get the code for radioButtons by running shiny::radioButtons
Define a new function radioButtons2 and in it replace the generateOptions with generateOptions2.
Set the environment for both new functions to shiny by running something like environment(radioButtons2) <- environment(radioButtons)
Use radioButtons2 in your code instead of radioButtons
The only thing I don't know is what to change the class parameter to in generateOptions2. I don't know much about div classes.
Update
I may have spoken too soon. It looks like the problem goes far deeper. The div function calls tags$div, which calls tag with the first argument set to "div". tag then calls the base function structure with the parameter class = "shiny.tag". structure then calls the primitive function attributes with the parameter class = "shiny.tag". attributes with this parameter then converts & in to &. Therefore, it looks like there is no way to use the div function and get back the euro symbol. I tried replacing div in generateOptions2 with HTML, but that returned a bunch of garbage when I ran the app. You'd have to change the behavior of class = "shiny.tag", but that could have far-reaching unpredictable effects.
Update 2
Figured it out! The trick is to use gsub to replace every & with just & before it is returned from generateOptions2. I put a functioning version of what you posted on github. It requires loading the two functions I mentioned earlier, which (in my repository) are stored in a file called functions.R. The important line in generateOptions2 is gsub(pattern = "&", replacement = "&", div(class = "shiny-options-group", options)).

Resources