LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
jones_optical.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #ifdef CLANG
18 #pragma clang diagnostic ignored "-Wdeprecated-register"
19 #endif
20 #include "writer/writer.h"
21 #include "eigen3/Eigen/Eigen"
22 #include "jones_optical.h"
23 #include "utils/comp_funcs.hpp"
24 #include "utils/noise.hpp"
25 #include "types/type_register.hpp"
26 #include "engine/engineimp.h"
27 
28 template class type_register<jones_optical>;
34 class jones_matrix:public _double{
35 
36  public:
37  double a1, a2, a3, ap;
38  int num_seg;
39  Eigen::Matrix<comp, 2, 2> wq, wh, wp, j1, j2, j3, jp;
40  Eigen::Matrix<comp, 2, 2> mvals;
41  Eigen::Matrix<comp, 2, 2> rmat(double alpha){
42  Eigen::Matrix<comp, 2, 2> in;
43  in(0, 0) = cos(alpha);
44  in(0, 1) = -1*sin(alpha);
45  in(1, 0)=sin(alpha);
46  in(1, 1)=cos(alpha);
47  return in;
48  }
49  virtual std::string type() const{
50  return "jones_matrix";
51  }
52  virtual void update(){
53  std::shared_ptr<writer> dat(new writer(true));
54  dat->add_data(data::create("Segment", num_seg), writer::PARAMETER);
55  double avals[4] = {a1, a2, a3, ap};
56  dat->add_data(data::create("Angles", avals, 4), writer::PARAMETER);
57  holder->add_writer(dat);
58  j1=rmat(a1)*wq*rmat(-1*a1);
59  j2=rmat(a2)*wq*rmat(-1*a2);
60  j3=rmat(a3)*wh*rmat(-1*a3);
61  jp=rmat(ap)*wp*rmat(-1*ap);
62  mvals = j1*jp*j2*j3;
63  }
65  this->holder = hold;
66  num_seg = seg;
67  setname(n);
68  }
69  void setup(std::vector<std::shared_ptr<variable> > avars){
70  wq(1, 0) = wq (0, 1) = 0;
71  wq(0, 0)=std::exp(-1.0*Id*3.14159*0.25);
72  wq(1, 1)=std::exp(1.0*Id*3.14159*0.25);
73  wh(1, 0)=wh(0, 1)=0;
74  wh(0, 0)=-1.0*Id;
75  wh(1, 1)=1.0*Id;
76  wp(0, 1)=wp(1, 0)=0;
77  wp(0, 0)=1;
78  wp(1, 1)=0;
79  avars[0]->add_ref(a1, this);
80  avars[1]->add_ref(a2, this);
81  avars[2]->add_ref(a3, this);
82  avars[3]->add_ref(ap, this);
83  update();
84  }
85 };
93 std::vector<std::string> jones_optical::dependencies() const{
94  std::string deps[] = {"num_jones_segments", "jones_int_dist"};
96 }
98 #ifdef gen_t_dat
99  func_dat=fopen("python/grad_data2.out", "w");
100  func_score = fopen("python/score_data2.out", "w");
101 #else
102  func_dat=fopen("python/grad_data.out", "w");
103  func_score = fopen("python/score_data.out", "w");
104 #endif
106 
107  if(dimension%2){
108  err("System jones_optical requires an even dimension", "jones_optical::postprocess",
109  "system/jones_optical.cpp", FATAL_ERROR);
110  }
111  int num_segments;
112  invals.retrieve(num_segments, "num_jones_segments", this);
113  if(num_segments < 0){
114  err("Number of jones segments must be greater than or equal to zero",
115  "jones_optical::postprocess", "system/jones_optical.cpp", FATAL_ERROR);
116  }
117  invals.retrieve(jones_int_dist, "jones_int_dist", this);
118  if(jones_int_dist<0){
119  err("The distance between jones segments, jones_int_dist, must be greater than or equal to zero",
120  "jones_optical::postprocess", "system/jones_optical.cpp", FATAL_ERROR);
121  }
122 
123  memp.add(dimension, &nvec1);
124  memp.add(nts, &help, &t, &kurtosis_help, &phold, &nvec2);
125  double dt = 60.0/nts;
126  gaussian_noise(ucur, dimension, 0, 0.2);
127  for(size_t i = 0; i < nts; i++){
128  t[i] = dt*(i-nts/2.0);
129  }
130  for(size_t i = 0; i < nts; i++){
131  ucur[i] = ucur[i+nts] = 1.00/cosh(t[i]/2.0);
132  help[i] = _real(ucur[i]);
133  nvec1[i] = ucur[i];
134  }
135  for(size_t i = 0; i < num_pulses; i++){
136  fft(nvec1 + i*nts, nvec1 +1*nts, nts);
137  }
138  //generate variables for the jones matrices
139  //create jones matrices
140  std::string name_base = "jones_system_vars";
141  std::string mat_base = "jones_system_matrices";
142 
143  for(int i = 0; i < num_segments; i++){
144  std::vector<std::shared_ptr<variable> > vv(4);
145  for(auto& val : vv){
146  val = std::make_shared<variable>();
147  val->setname(get_unique_name(name_base));
148  val->holder=holder;
149  val->parse("0.1");
150 #ifdef gen_t_dat
151  val->set(0*2*3.1415*(rand()*1.0/RAND_MAX));
152 #else
153  val->set(0);
154 #endif
155  invals.insert_item(val);
156  cont->addvar(val);
157  }
158  std::shared_ptr<jones_matrix> m = std::make_shared<jones_matrix>(get_unique_name(mat_base), i, holder);
159  invals.insert_item(m);
160  m->setup(vv);
161  jones_matrices.push_back(m);
162  }
163 }
164 double sign(double v){
165  if(v < 0){
166  return -1;
167  }
168  return 1;
169 }
174  if(round==0){
175  for(size_t i = 0; i < nts; i++){
176  ucur[i] = ucur[i+nts] = 1.00/cosh(t[i]/2.0);
177  help[i] = _real(ucur[i]);
178  }
179  }
180  if(round == 1){
181  for(size_t j = 0; j < num_pulses; j++){
182  fft(ucur+j*nts, ucur+j*nts, nts);
183  }
184  for(size_t i = 0; i < dimension; i++){
185  nvec1[i] = ucur[i];
186  }
187  for(size_t j = 0; j < num_pulses; j++){
188  ifft(ucur+j*nts, ucur+j*nts, nts);
189  }
190  }
191 
192 }
199  //apply the jones matrices, and integration between them
200 
201  if(!jones_matrices.empty()){
202  Eigen::Map<Eigen::Matrix<comp, 2, Eigen::Dynamic, Eigen::RowMajor>, Eigen::Aligned> dmap(ucur, 2, nts);
203  //this is a more complicated structure but will save an fft/ifft combination
204  dmap = jones_matrices[0]->mvals*dmap;
205  for(size_t i = 1; i < jones_matrices.size(); i++){
206  for(size_t j = 0; j < num_pulses; j++){
207  fft(ucur+j*nts, ucur+j*nts, nts);
208  }
210  for(size_t j = 0; j < num_pulses; j++){
211  ifft(ucur+j*nts, ucur+j*nts, nts);
212  }
214  dmap = jones_matrices[i]->mvals*dmap;
215  }
216  }
217 }
219 
224  double norm, change_norm;
225  norm = change_norm = 0;
226  for(size_t j = 0; j < nts; j++){
227  double fsq = _sqabs(ulast[j]);
228  for(size_t i = 1; i < num_pulses; i++){
229  fsq += _sqabs(ulast[j+i*nts]);
230  }
231  norm += fsq;
232  double cfsq = _sqabs(ucur[j]) ;
233  for(size_t i = 1; i < num_pulses; i++){
234  cfsq += _sqabs(ucur[j+i*nts]);
235  }
236  cfsq = sqrt(cfsq)-sqrt(fsq);
237  change_norm += cfsq*cfsq;
238  }
239  if(norm < 1e-4){
240  return 0;
241  }
242  return sqrt(change_norm/norm);
243 }
244 
247  return obj->score(ucur);
248 }
250 }
251 
253  return "jones_optical";
254 }