# Presentation: 709348

## VIRTUAL ACCELERATOR MANAGER

## Motivation 

- At this time we have “Trace3D”,”Parmila”, and “Transport” as existing and possible engines for virtual accelerator.
- Common:
-  FORTRAN
-  Input ASCII file with input parameters
- Tracking particles or beam center through the lattice elements at a stage of dynamics calculation
- Having huge main program
- We can write one C/C++  and FORTRAN interface to these programs that will deal with the CAS. We can call this interface as “Virtual Accelerator Manager”. It enable users to work only with FORTRAN code of engines during developing the virtual accelerator.

## The Structure of the Model

- LAN

- Portable CAS
- with real and model dependent PVs

- CA client
- with an interface to
- the FORTRAN code

- CA Client
- MATLAB or JAVA
- scripts with CA
- commands

- Trace3D, Parmila, or Transport

## The Manager Structure

- The Class baseFileTranslator
- Reads input file, change it and write as a temporary input file

- caDefinition.cc
- CA functions and structures

- CInterface2model.cc
- C interface to the FORTRAN subroutines and functions

- The Class modelManager.cc
- Puts and gets PVs from CAS
- Starts and restarts the model

- Modified FORTRAN Code of the Model

## File Translator

- line 1 ========= 0.111 ===
- line 2 ========= MEBT_Mag:DCH_1:FieldSet ===
- line 3 ========= 0.333 ===
- line 4 ========= 0.444 ===

- Input
- File:

- line 1 ========= 0.111 ===
- line 2 ========= 0.00407 ===
- line 3 ========= 0.333 ===
- line 4 ========= 0.444 ===

- Output
- File:

-  MEBT_Mag:DCH_1:FieldSet  MEBT_Mag:DCH_1:FieldRB      0.00407
-  MEBT_Mag:DCH_4:FieldSet  MEBT_Mag:DCH_4:FieldRB      3700.0
-  MEBT_Mag:DCH_5:FieldSet  NULL                                          0.099403
-  MEBT_Mag:DCH_10:FieldSet MEBT_Mag:DCH_10:FieldRB   0.05
-  MEBT_Mag:DCH_11:FieldSet NULL                                         0.040178

- According to the file:

## The C Interface to the FORTRAN Subroutines 

- //Calls main subroutine in the Model initial code
- extern "C" void submain_(const char* file_name, int length_of_name);
- //Disables output for the Model code
- extern "C" void dsblout_();
- //Permits output for the Model code
- extern "C" void prmtout_();
- //Initializes interface variables
- extern "C" void intrini_();
- //Restores information for interface variables to repeat run
- extern "C" void intrres_();
- //Defines how to calculate PV values inside the model
- extern "C" void pvinit_(int* i_cell,int* i_type);
- //Returns the calculated values of PVs
- extern "C" void pvget_(int* i_n, int* i_cell, int* i_type, double* value);

## FORTRAN Interface Subroutines

- C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- C-----These subroutines are called from C to manage the Model
- C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-       subroutine submain(p_in_file)
- c----------------------------------------------------
- c-----p_in_file - name of input file for Model
- c----------------------------------------------------
-       implicit real*8 (a-h,o-z)
-       character p_in_file*64
-       call   mainParmila(p_in_file)
-       return
-       end

-       subroutine pvget(i_n,i_cell,i_type,value)
- c----------------------------------------------------------------------
- c-----returns the output PV calculated values
- c-----i_status = 0 means the value was not calculated
- c----------------------------------------------------------------------
-       implicit real*8 (a-h,o-z)
-       parameter (max_n_pv=100)
-       common
-      */com_int_2/ n_pv,i_cellA(max_n_pv),
-      *            i_typeA(max_n_pv),
-      *            i_status(max_n_pv)
-      */com_int_3/ pv_val(max_n_pv)
-       value = pv_val(i_n)
-       return
-       end

## FORTRAN Interface Subroutines (next)

-       subroutine pv_setup(i_cell)
-       implicit real*8 (a-h,o-z)
- c----------------------------------------------------------------------
- c-----calculates the output PV values
- c-----i_status = 0 means the value was not calculated
- c----------------------------------------------------------------------
-       parameter (max_n_pv=100)
-       common
-      */com_int_2/ n_pv,i_cellA(max_n_pv),
-      *            i_typeA(max_n_pv),
-      *            i_status(max_n_pv)
-      */com_int_3/ pv_val(max_n_pv)
-       do i = 1,n_pv
-        if(i_cellA(i) .eq. i_cell) then
-         i_type = i_typeA(i)
-         i_status(i) = 0
-          select case (i_type)
-           case (1)
- C---------this place for  pv_val(i) calculation as x BPM position
-            pv_val(i) = x_bpm_f()
-            i_status(i) = 1
-           case (2)
- C---------this place for  pv_val(i) calculation as y BPM position
-            pv_val(i) = y_bpm_f()
-            i_status(i) = 1
-          endselect
-        endif
-       enddo
-       return
-       end

## Main Program of Virtual Accelerator

** ****int counter = 0;     int i_start = 0;       double time_delay = 0.5;**
**  ****modelManager* manager = new modelManager::modelManager();**
**  ****manager->initialization( ****file_in_parmila, file_in_param, file_calc_PV****);**
**  ****manager->defineNameOfTmpFile("parmila_input.tmp");**
**  ****manager->defineNameOfStartPV("MODEL:Parmila:SimStart");**
**  ****manager->sendInitialPVtoCAS(time_delay);**
**  ****manager->disableModelOutGraph();**
**  ****manager->startModel();**
**  ****//Main loop for calculation**
**  ****while(1){ **
**   ****i_start = manager->getStartPV(time_delay);**
**    ****if(i_start == 1){**
**      ****manager->getPVfromCAS(time_delay);**
**      ****manager->startModel(); **
**      ****manager->sendPVtoCAS(time_delay);**
**             **
**      ****i_start = 0;**
**      ****manager->setStartPV(i_start,time_delay);**
**      ****counter++;**
**      ****cout << "The Parmila's run N = "<< counter <<endl;**
**    ****}**
**  ****}**

## How to Find the Source Code

- On Linux1
- /work2/ca_client_Parmila/
- Here you can find source files, Make file, and examples