OpticUtil.java

  1. // @formatter:off
  2.  /*******************************************************************************
  3.  *
  4.  * This file is part of JMad.
  5.  *
  6.  * Copyright (c) 2008-2011, CERN. All rights reserved.
  7.  *
  8.  * Licensed under the Apache License, Version 2.0 (the "License");
  9.  * you may not use this file except in compliance with the License.
  10.  * You may obtain a copy of the License at
  11.  *
  12.  *     http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing, software
  15.  * distributed under the License is distributed on an "AS IS" BASIS,
  16.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17.  * See the License for the specific language governing permissions and
  18.  * limitations under the License.
  19.  *
  20.  ******************************************************************************/
  21. // @formatter:on

  22. package cern.accsoft.steering.jmad.model;

  23. import java.util.List;

  24. import cern.accsoft.steering.jmad.domain.ex.JMadModelException;
  25. import cern.accsoft.steering.jmad.domain.optics.Optic;
  26. import cern.accsoft.steering.jmad.domain.optics.OpticImpl;
  27. import cern.accsoft.steering.jmad.domain.optics.OpticPoint;
  28. import cern.accsoft.steering.jmad.domain.optics.OpticPointImpl;
  29. import cern.accsoft.steering.jmad.domain.result.tfs.TfsResult;
  30. import cern.accsoft.steering.jmad.domain.result.tfs.TfsResultRequest;
  31. import cern.accsoft.steering.jmad.domain.result.tfs.TfsResultRequestImpl;
  32. import cern.accsoft.steering.jmad.domain.var.enums.MadxTwissVariable;

  33. /**
  34.  * collectiion of utility methods for the Model
  35.  *
  36.  * @author Kajetan Fuchsberger (kajetan.fuchsberger at cern.ch)
  37.  */
  38. public final class OpticUtil {

  39.     private OpticUtil() {
  40.         /* only static methods */
  41.     }

  42.     public static Optic calcOptic(JMadModel model) throws JMadModelException {
  43.         TfsResultRequest resultRequest = fullOpticsRequest();
  44.         TfsResult tfsResult = model.twiss(resultRequest);
  45.         List<MadxTwissVariable> variables = allOpticsVariables();
  46.         return createOptic(tfsResult, variables.toArray(new MadxTwissVariable[variables.size()]));
  47.     }

  48.     /**
  49.      * Creates a Tfs request which will contain all elements and all Madx twiss variables which are required for the
  50.      * optics.
  51.      *
  52.      * @return the result request which can be used for a twiss.
  53.      */
  54.     public static final TfsResultRequest fullOpticsRequest() {
  55.         TfsResultRequestImpl resultRequest = new TfsResultRequestImpl();
  56.         resultRequest.addElementFilter(".*");

  57.         resultRequest.addVariable(MadxTwissVariable.NAME);
  58.         List<MadxTwissVariable> variables = allOpticsVariables();
  59.         for (MadxTwissVariable var : variables) {
  60.             resultRequest.addVariable(var);
  61.         }
  62.         return resultRequest;
  63.     }

  64.     private static List<MadxTwissVariable> allOpticsVariables() {
  65.         return OpticPointImpl.MADX_VARIABLES;
  66.     }

  67.     public static Optic createOptic(TfsResult tfsResult, MadxTwissVariable... variables) {
  68.         OpticImpl optic = new OpticImpl();

  69.         /*
  70.          * store the values from the twiss result
  71.          */
  72.         for (MadxTwissVariable var : variables) {
  73.             optic.add(var, tfsResult.getDoubleData(var));
  74.         }

  75.         /*
  76.          * get the names and check, if the size is still the same.
  77.          */
  78.         List<String> names = tfsResult.getStringData(MadxTwissVariable.NAME);
  79.         optic.setNames(names);

  80.         for (int i = 0; i < names.size(); i++) {
  81.             String name = names.get(i);
  82.             OpticPoint point = new OpticPointImpl(name);
  83.             for (MadxTwissVariable var : variables) {
  84.                 ((OpticPointImpl) point).setValue(var, optic.getAllValues(var)
  85.                         .get(i));
  86.             }
  87.             optic.add(point);
  88.         }

  89.         return optic;
  90.     }
  91. }