Running multiple simulations using VB script

Running multiple simulations using VB script

Anonymous
Not applicable
2,306 Views
12 Replies
Message 1 of 13

Running multiple simulations using VB script

Anonymous
Not applicable

Hello Modelers,

I am trying to learn API. I wrote a visual basic script with creates multiple study files from a study file named 'sample.sdy' by changing injection time values. I also have command to start the analysis added in the script. I have written the script in the post for reference.

 

The problem is when i run the script, the multiple study files are created but all of them are simulated with same injection time, and not the injection time i set using the script. Mostly they take the injection time value of the last study file created (Refer the script : Injection time = 4s, from xyz.sdy is taken by all the studies (abc.sdy and lmn.sdy) here). Please help me with this thing because i have no idea what is going on here. Is this a bug or i am implementing incorrectly?

 

Regards,

Arjav

 

<----------------------------Test.vbs------------------------------------>

'%RunPerInstance
'@ DESCRIPTION
'@ Macro recorded by Synergy on 24-Apr-2018 at 15:17:56
SetLocale("en-us")
Dim SynergyGetter, Synergy
On Error Resume Next
Set SynergyGetter = GetObject(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%SAInstance%"))
On Error GoTo 0
If (Not IsEmpty(SynergyGetter)) Then
Set Synergy = SynergyGetter.GetSASynergy
Else
Set Synergy = CreateObject("synergy.Synergy")
End If
Synergy.OpenProject "E:\Scripting_trials\Scripting_trials.mpi"
Synergy.SetUnits "English"
Set Project = Synergy.Project()
Project.OpenItemByName "sample", "Study"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.SaveAs "abc"
Set PropEd = Synergy.PropertyEditor()
Set Prop = PropEd.FindProperty(30011, 1)
Set DVec = Synergy.CreateDoubleArray()
DVec.AddDouble 2
Prop.FieldValues 10100, DVec
PropEd.CommitChanges "Process Conditions"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalyzeNow True, True
WScript.Sleep 10000
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.SaveAs "lmn"
Set PropEd = Synergy.PropertyEditor()
Set Prop = PropEd.FindProperty(30011, 1)
Set DVec = Synergy.CreateDoubleArray()
DVec.AddDouble 3
Prop.FieldValues 10100, DVec
PropEd.CommitChanges "Process Conditions"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalyzeNow True, True
WScript.Sleep 10000
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.SaveAs "xyz"
Set PropEd = Synergy.PropertyEditor()
Set Prop = PropEd.FindProperty(30011, 1)
Set DVec = Synergy.CreateDoubleArray()
DVec.AddDouble 4
Prop.FieldValues 10100, DVec
PropEd.CommitChanges "Process Conditions"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.AnalyzeNow True, True

0 Likes
Accepted solutions (2)
2,307 Views
12 Replies
Replies (12)
Message 2 of 13

bernor_mf
Advisor
Advisor

Hi @Anonymous,

you need to add a save of study to store data.

Something like this:

Project.OpenItemByName "sample", "Study"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.Save
WScript.Sleep 3000

The Sleep is to give system some time to save study.

 

And after changing process condtions for new study:

PropEd.CommitChanges "Process Conditions"
StudyDoc.Save
WScript.Sleep 3000

Outprompt this when testing:

'StudyDoc.AnalyzeNow True, True
'WScript.Sleep 10000

To make testing pass faster.

 

Also, set WScript.Sleep for each, as system depending.

 

I did a test of above, and it worked to set injection time of each of 1,2,3,4 s

checked by Compare Studies.

 

Hope this helps.

Regards,

Berndt

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
0 Likes
Message 3 of 13

Anonymous
Not applicable

Hi @bernor_mf

Thanks for the reply.

I tried the solution suggested by you of incorporating a SAVE command. See in the script below.

 

The problem still persists. Like i said i am also able to generate multiple sdy files with different values of injection as 2s, 3s and 4s. But when analysis are run on it using 'StudyDoc.AnalyzeNow True, True', all the study files run with injection time of 4s. 

 

Can you tell me why this is happening?

You can generate a simple sdy file and then run my script here, over it to see the issue.

Can you let me what fill time are you getting in the result from analysis of different study files?

 

Regards,

Arjav

 

<-------------------------------------Modified_test.vbs-------------------------------------------->

'%RunPerInstance
'@ DESCRIPTION
'@ Macro recorded by Synergy on 24-Apr-2018 at 15:17:56
SetLocale("en-us")
Dim SynergyGetter, Synergy
On Error Resume Next
Set SynergyGetter = GetObject(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%SAInstance%"))
On Error GoTo 0
If (Not IsEmpty(SynergyGetter)) Then
Set Synergy = SynergyGetter.GetSASynergy
Else
Set Synergy = CreateObject("synergy.Synergy")
End If
Synergy.OpenProject "E:\Scripting_trials\Scripting_trials.mpi"
Synergy.SetUnits "English"
Set Project = Synergy.Project()
Project.OpenItemByName "sample", "Study"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.SaveAs "abc"
Set PropEd = Synergy.PropertyEditor()
Set Prop = PropEd.FindProperty(30011, 1)
Set DVec = Synergy.CreateDoubleArray()
DVec.AddDouble 2
Prop.FieldValues 10100, DVec
PropEd.CommitChanges "Process Conditions"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.Save
WScript.Sleep 5000
StudyDoc.AnalyzeNow True, True
WScript.Sleep 10000
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.SaveAs "lmn"
Set PropEd = Synergy.PropertyEditor()
Set Prop = PropEd.FindProperty(30011, 1)
Set DVec = Synergy.CreateDoubleArray()
DVec.AddDouble 3
Prop.FieldValues 10100, DVec
PropEd.CommitChanges "Process Conditions"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.Save
WScript.Sleep 5000
StudyDoc.AnalyzeNow True, True
WScript.Sleep 10000
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.SaveAs "xyz"
Set PropEd = Synergy.PropertyEditor()
Set Prop = PropEd.FindProperty(30011, 1)
Set DVec = Synergy.CreateDoubleArray()
DVec.AddDouble 4
Prop.FieldValues 10100, DVec
PropEd.CommitChanges "Process Conditions"
Set StudyDoc = Synergy.StudyDoc()
StudyDoc.Save
WScript.Sleep 5000
StudyDoc.AnalyzeNow True, True

0 Likes
Message 4 of 13

bernor_mf
Advisor
Advisor

Hi Arjav,

thank you for the feedback.

I better understand your issue.

Weird behavior. Have no explanation of why this is happening.

It should accept the change of parameter in study and use in analysis.

Will see when I get a time slot to dive in to this.

Might need another way of doing this.

 

Regards,

Berndt

 

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
0 Likes
Message 5 of 13

Anonymous
Not applicable

Hi @bernor_mf

Thank you for the response.

Please do let me know when you get time and have worked on this.

 

Regards,

Arjav

0 Likes
Message 6 of 13

bernor_mf
Advisor
Advisor
Accepted solution

Hi
first, I could replicate the issue.

Investigating this, it is related to settings in Options.
Enter Options and Results tab, and the Duplicated Studies area:
Duplicate study - include results linked from original study
is the default setting.
Hence in script and StudyDoc.SaveAs will use this,
and that is why all studies get the same result, no matter changes of parameter.

They are linked to first study.

Changing to
Duplicate study - do not include any results
resolves the issue

Changing the parameters of duplicating studies

As I could replicate your issue, doing above change, gave the expected results.

The set injection time was used as expected.

 

Now, there is an object that can deal with this:
DuplicateStudyByName3

From Synergy API reference:

Boolean DuplicateStudyByName3  ( String  aName,  
  Boolean  aSave,  
  long  aDuplicateOption   
 )    

Duplicates a study by its name, whether to save study before duplicate, also includes the type of duplication. 

Parameters:
 aName  name of the study to duplicate  
 aSave  whether to save study before duplicate  
 aDuplicateOption  the type of duplication to perform.

0 = complete copy of results files.
1 = no results files included.
2 = include result files linked to original study [Existing DEFAULT].

 

Returns:
True if successful; False otherwise 
Note:
The application tries to prevent you from duplicating study names in the project; however if you have duplicated study or report names in your project, this function will duplicate the first item that matches the name. If the duplicate option specified is invalid the default option will be chosen. 
Example
This example duplicates the unsaved study named "My Study", including results that are linked to the study being duplicated.


 Set Project = Synergy.Project()
 Project.DuplicateStudyByName3 "My Study",FALSE,2

 

Better to use DuplicateStudyByName3, as in control compared to StudyDoc.SaveAs.

After duplication, you can use RenameItemByName to rename your study.

Get the details from Synergy API reference.

Method is up to you.

 


Hope this will resolve the issue for you.

Regards,
Berndt

 

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
Message 7 of 13

Anonymous
Not applicable

Hello @bernor_mf

Thank you so much for first acknowledging the problem i was facing and secondly for providing the correct solution.

I tried the first method and it works very well. I will also try the second method.

 

Regards,

Arjav

0 Likes
Message 8 of 13

Anonymous
Not applicable

Where can I find this Setting in MFI 2013 ?

0 Likes
Message 9 of 13

bernor_mf
Advisor
Advisor

Hi @Anonymous,

if you refer to "Changing the parameters of duplicating studies" option, it was introduced to Moldflow Insight 2017, I believe. Hence not available in Moldflow Insight 2016 and older releases.

 

Hope this helps.

Regards,

Berndt

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
0 Likes
Message 10 of 13

Anonymous
Not applicable

Hello @bernor_mf,

 

Thank you for the fast and kind answer.

Yes I reffer to duplicating a study without results.

 

For my Bachelorthesis I'm trying to process multiple Studies and change the Process parameters, the University only provides MFI 2013.

 

I have written the skript attached at the end to do this. but also get in each study (and exported log file) the same results, not beeing calculated for each setting.

 

Is there a way to delte the copied result from the duplicateStudy ?

 

Best regards,

Manuel

 

startTime = Now

SetLocale(1031) 'Germany
Set Synergy = CreateObject("synergy.Synergy")
Synergy.SetUnits "Metric"
Set Project = Synergy.Project()
c = 0

for counter = 0.1 To 0.5 Step 0.1

	counter = Round(counter,1)
	
	Project.DuplicateStudyByName2 "Start", False
	Set Project = Synergy.Project()
	Project.OpenItemByName "Start (Kopie)", "Study"
	
	Set PropEd = Synergy.PropertyEditor()
	Set Prop = PropEd.FindProperty(30011, 1) 'TCS für Einspritzzeit
	Set DVec = Synergy.CreateDoubleArray()
	DVec.AddDouble counter
	Prop.FieldValues 10100, DVec
	PropEd.CommitChanges "Process Conditions"
	Set Project = Synergy.Project()
	
	Project.RenameItemByName "Start (Kopie)", "Study", counter
	Set StudyDoc = Synergy.StudyDoc()
	StudyDoc.AnalyzeNow True, True
	
	logloc = "C:\\Users\\seitz-1\\Desktop\\Moldflow Makros\\Log\\" + CStr(counter) + ".log"
	StudyDoc.ExportAnalysisLog logloc
	
	StudyDoc.Close
	
	c = c+1
	
	next
	

diff = FormatDateTime (startTime - Now, 3) 
msg = + CStr(c) + " Studien durchgefuehrt in: " + diff
MsgBox msg, 0, "Studie Abgeschlossen"
0 Likes
Message 11 of 13

bernor_mf
Advisor
Advisor
Accepted solution

Hello Manuel,

you need to use StudyDoc.DeleteResults(0) to break the connection to previous study.

 

Also save , and wait save to completed and analysis to complete.

 

Below code works. (Did some changes to run on my side, which you have to adjust on your side.)

Synergy uses SetLocale("en-us"), so I think it should be kept.

 

Hope this helps.

Regards,

Berndt

 

'SetLocale(1031) 'Germany
SetLocale("en-us") '** locale for synergy
Set Synergy = CreateObject("synergy.Synergy")
Synergy.SetUnits "Metric"
Set Project = Synergy.Project()
c = 0

for counter = 0.1 To 0.5 Step 0.1

	counter = Round(counter,1)
	
	Project.DuplicateStudyByName2 "Start", False
	Set Project = Synergy.Project()
	Project.OpenItemByName "Start (copy)", "Study"

Set StudyDoc = Synergy.StudyDoc()
StudyDoc.DeleteResults(0)
	Set PropEd = Synergy.PropertyEditor()
	Set Prop = PropEd.FindProperty(30011, 1) 'TCS für Einspritzzeit
	Set DVec = Synergy.CreateDoubleArray()
	DVec.AddDouble counter
	Prop.FieldValues 10100, DVec
	PropEd.CommitChanges "Process Conditions"

StudyDoc.Save
WScript.Sleep 3000 '*** wait 3s for study to be saved
	'Set Project = Synergy.Project()
	
	Project.RenameItemByName "Start (copy)", "Study", counter
	'Set StudyDoc = Synergy.StudyDoc()
	'StudyDoc.AnalyzeNow True, True
StudyDoc.AnalyzeNow2 False, True, False '** last False will prevent showing prompt on GUI 
WScript.Sleep 10000	'*** wait 10s for analysis to complete (or check status in JobMgr)
	'logloc = "C:\\Users\\seitz-1\\Desktop\\Moldflow Makros\\Log\\" + CStr(counter) + ".log"
	logloc = "C:\\Temp\\" + CStr(counter) + ".log"
	StudyDoc.ExportAnalysisLog logloc
	
	StudyDoc.Close
	
	c = c+1
	
	next
	

diff = FormatDateTime (startTime - Now, 3) 
msg = + CStr(c) + " Studien durchgefuehrt in: " + diff
MsgBox msg, 0, "Studie Abgeschlossen"
( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
Message 12 of 13

Anonymous
Not applicable

Thank you a lot @bernor_mf. It works now !! Thank you for your investment and time and very accurate, helpful and friendly relply and for even testing it.

0 Likes
Message 13 of 13

bernor_mf
Advisor
Advisor

Hi Manuel,

thank you for your kind words.

Happy to help.

 

Happy programming and simulation. Smiley Happy

 

Regards,

Berndt

 

( If my comments are accepted, provide "Kudos" as appreciation. If your request is answered/resolved, please click the "Accept as Solution" button. Thanks.)
0 Likes