UltraScan III
us_worker_2d.cpp
Go to the documentation of this file.
1 #include "us_worker_2d.h"
3 #include "us_util.h"
4 #include "us_settings.h"
5 #include "us_astfem_math.h"
6 #include "us_astfem_rsa.h"
7 #include "us_model.h"
8 #include "us_sleep.h"
9 #include "us_math2.h"
10 #include "us_constants.h"
11 #include "us_memory.h"
12 
13 
14 // construct worker thread
16  : QThread( parent )
17 {
19  abort = false;
20  solvesim = NULL;
21  thrn = -1;
22 DbgLv(1) << "2P(WT): Thread created";
23 }
24 
25 // worker thread destructor
27 {
28  //if ( solvesim != NULL )
29  // delete solvesim;
30 
31 DbgLv(1) << "2P(WT): Thread destroy - (1)finished?" << isFinished() << thrn;
32  if ( ! wait( 2000 ) )
33  {
34  qDebug() << "Thread destroy wait timeout(2secs) : Thread" << thrn;
35  }
36 DbgLv(1) << "2P(WT): Thread destroy - (2)finished?" << isFinished() << thrn;
37 DbgLv(1) << "2P(WT): Thread destroyed" << thrn;
38 }
39 
40 // define work for a worker thread
42 {
43 
44  llim_s = workin.ll_s;
45  llim_k = workin.ll_k;
46  thrn = workin.thrn;
47  taskx = workin.taskx;
48  depth = workin.depth;
49  iter = workin.iter;
50  menmcx = workin.menmcx;
51  noisflag = workin.noisf;
52  typeref = workin.typeref;
53 
54  solutes_i = workin.isolutes;
55 
56  //dsets << workin.dsets[ 0 ];
57  dset_wk = *(workin.dsets[ 0 ]); // local copy of data set
58  dset_wk.noise_files = workin.dsets[ 0 ]->noise_files;
59  dset_wk.run_data = workin.dsets[ 0 ]->run_data;
60  dset_wk.model = workin.dsets[ 0 ]->model;
61  dset_wk.simparams = workin.dsets[ 0 ]->simparams;
62  dset_wk.solution_rec = workin.dsets[ 0 ]->solution_rec;
63  dsets.clear();
64  dsets << &dset_wk; // save its pointer
65 
66  sim_vals = workin.sim_vals;
71 }
72 
73 // get results of a completed worker thread
75 {
76  workout.ll_s = llim_s;
77  workout.ll_k = llim_k;
78  workout.thrn = thrn;
79  workout.taskx = taskx;
80  workout.depth = depth;
81  workout.iter = iter;
82  workout.menmcx = menmcx;
83  workout.noisf = noisflag;
84 
85  workout.csolutes = solutes_c;
86  workout.ti_noise = ti_noise.values;
87  workout.ri_noise = ri_noise.values;
88  workout.sim_vals = sim_vals;
89 int nn=workout.csolutes.size();
90 int kk=nn/2;
91 int ni=solutes_i.size();
92 DbgLv(1) << "2P(WT): thr nn" << thrn << nn << "out sol0 solk soln"
93  << workout.csolutes[0].c << workout.csolutes[kk].c << workout.csolutes[nn-1].c
94  << "in sol0 soln" << ni << solutes_i[0].s*1.e13 << solutes_i[ni-1].s*1.e13
95  << solutes_i[0].c << solutes_i[ni-1].c;
96 }
97 
98 // run the worker thread
100 {
101 
102  calc_residuals(); // do all the work here
103 
104  quit();
105  exec();
106 
107  emit work_complete( this ); // signal that a thread's work is done
108 }
109 
110 // set a flag so that a worker thread will abort as soon as possible
112 {
113  solvesim->abort_work();
114 }
115 
116 // Do the real work of a thread: solution from solutes set
118 {
119 
120  if ( typeref == (-2) )
121  {
123  return;
124  }
125 
126  solvesim = new US_SolveSim( dsets, thrn, true );
127 
128  connect( solvesim, SIGNAL( work_progress( int ) ),
129  this, SLOT( forward_progress( int ) ) );
130 
132 
135  sim_vals.dbg_timing = US_Settings::debug_match( "2dsaTiming" );
136 
137  solvesim->calc_residuals( 0, 1, sim_vals );
138 
142 
143  return;
144 }
145 
146 // Slot to forward a progress signal
148 {
149  emit work_progress( steps );
150 }
151 
152 // Do thread work for model-ratio: solution from single model, find ratio
154 {
155  dset_wk = *dsets[ 0 ];
156  US_SolveSim::DataSet* dset = &dset_wk;
157 
158  solutes_c .clear();
159  ti_noise.values.clear();
160  ri_noise.values.clear();
161 
162  // Create a single model from the solutes
163  int nsolutes = solutes_i.size();
164 DbgLv(1) << "WT:CRR nsolutes" << nsolutes << "0vb" << solutes_i[0].v;
165  US_Model wmodel;
166  wmodel.components.resize( nsolutes );
167 
168  for ( int ii = 0; ii < nsolutes; ii++ )
169  {
170  wmodel.components[ ii ].s = solutes_i[ ii ].s;
171  wmodel.components[ ii ].f_f0 = solutes_i[ ii ].k;
172  wmodel.components[ ii ].vbar20 = solutes_i[ ii ].v;
173  wmodel.components[ ii ].signal_concentration = solutes_i[ ii ].c;
174  wmodel.components[ ii ].D = 0.0;
175  wmodel.components[ ii ].mw = 0.0;
176  wmodel.components[ ii ].f = 0.0;
177 
178  wmodel.calc_coefficients( wmodel.components[ ii ] );
179 
180  wmodel.components[ ii ].s /= dset->s20w_correction;
181  wmodel.components[ ii ].D /= dset->D20w_correction;
182  }
183 
185  int npoints = edata->pointCount();
186  int nscans = edata->scanCount();
187  int ntotal = nscans * npoints;
188  QVector< double > nnls_a( ntotal, 0.0 );
189  QVector< double > nnls_b( ntotal, 0.0 );
190  QVector< double > nnls_x( 1, 0.0 );
191 
192 DbgLv(1) << "WT:CRR ns np nt" << nscans << npoints << ntotal;
194 
195  US_Astfem_RSA astfem_rsa( wmodel, dset->simparams );
196 
197  astfem_rsa.calculate( sim_vals.sim_data );
198 
199  int kk = 0;
200 
201  for ( int ss = 0; ss < nscans; ss++ )
202  {
203  for ( int rr = 0; rr < npoints; rr++ )
204  {
205  nnls_a[ kk ] = sim_vals.sim_data.value( ss, rr );
206  nnls_b[ kk++ ] = edata->value( ss, rr );
207  }
208  }
209 
210  US_Math2::nnls( nnls_a.data(), ntotal, ntotal, 1,
211  nnls_b.data(), nnls_x.data() );
212 
213  double cmult = nnls_x[ 0 ];
214 DbgLv(1) << "WT:CRR CMULT" << cmult;
215  solutes_c.resize( nsolutes );
216 
217  for ( int ii = 0; ii < nsolutes; ii++ )
218  {
219  wmodel.components[ ii ].s = solutes_i[ ii ].s;
220  wmodel.components[ ii ].f_f0 = solutes_i[ ii ].k;
221  wmodel.components[ ii ].vbar20 = solutes_i[ ii ].v;
222  wmodel.components[ ii ].D = 0.0;
223  wmodel.components[ ii ].mw = 0.0;
224  wmodel.components[ ii ].f = 0.0;
225 
226  double cvali = wmodel.components[ ii ].signal_concentration;
227  double cval = cvali * cmult;
228  wmodel.components[ ii ].signal_concentration = cval;
229 
230  wmodel.calc_coefficients( wmodel.components[ ii ] );
231 
232  solutes_c[ ii ] = solutes_i[ ii ];
233  solutes_c[ ii ].c = cval;
234 DbgLv(1) << "WT:CRR ii cvali cval" << ii << cvali << cval;
235  }
236 
237  return;
238 }
239